<?php

namespace Opentalent\OtTemplating\ViewHelpers\Request;

use Closure;
use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

/**
 *   Checks if a parameter with this name was passed trough the GET request.
 *   If this is the case, returns this parameter; if not returns null.
 *
 *     {namespace ot=Opentalent\OtTemplating\ViewHelpers}
 *
 *     {ot:request.getParameter('name')}
 *
 * @package Opentalent\OtTemplating\ViewHelpers
 */
class GetParameterViewHelper extends OtAbstractViewHelper {

    /**
     * -- This method is expected by Fluid --
     * Declares the viewhelper's parameters
     */
    public function initializeArguments()
    {
        $this->registerArgument('param',
            'string',
            "The parameter's name",
            true);
    }

    /**
     * -- This method is expected by Fluid --
     * Renders the content as html
     *
     * @param array $arguments
     * @param Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     * @return string|null
     */
    public static function renderStatic(
        array $arguments,
        Closure $renderChildrenClosure,
        RenderingContextInterface $renderingContext
    ) {
        $param = $arguments['param'];
        if (isset($_REQUEST[$param])) {
            return $_REQUEST[$param];
        }
        return null;
    }
}
