<?php

namespace Opentalent\OtTemplating\ViewHelpers\Utilities;

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

/**
 *   Make sure the given url is an absolute one by prepending the 'https' prefix
 *
 *     {namespace ot=Opentalent\OtTemplating\ViewHelpers}
 *
 *     {ot:utilities.absoluteUrl(url: url)}
 *
 * @package Opentalent\OtTemplating\ViewHelpers
 */
class AbsoluteUrlViewHelper extends OtAbstractViewHelper {

    /**
     * -- This method is expected by Fluid --
     * Declares the viewhelper's parameters
     */
    public function initializeArguments()
    {
        $this->registerArgument('url',
            'string',
            "An URL",
            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
    ) {
        $url = $arguments['url'];
        return 'https://' . preg_replace("/(?:https?:\/\/)?(.*)/", "$1", $url);
    }
}
