DigiComp.FluidRenderFunctions/Classes/ViewHelpers/ApplyRenderFunctionViewHelper.php
Ferdinand Kuhl 050e6e119f
All checks were successful
ci/woodpecker/push/code-style Pipeline was successful
ci/woodpecker/push/functional-tests/1 Pipeline was successful
ci/woodpecker/push/functional-tests/2 Pipeline was successful
ci/woodpecker/push/functional-tests/3 Pipeline was successful
ci/woodpecker/push/functional-tests/4 Pipeline was successful
storing the render function in ViewHelperVariableContainer
2024-06-02 04:04:48 +02:00

57 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace DigiComp\FluidRenderFunctions\ViewHelpers;
use DigiComp\FluidRenderFunctions\InvokeRenderFunctionInterface;
use DigiComp\FluidRenderFunctions\Utils\GeneratorClosureIterator;
use DigiComp\FluidRenderFunctions\Utils\RenderableProxy;
use DigiComp\FluidRenderFunctions\ViewHelpers\Traits\ValidateRenderFunctionTrait;
use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
class ApplyRenderFunctionViewHelper extends AbstractViewHelper
{
use ValidateRenderFunctionTrait;
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument('in', 'mixed', 'subject to apply the render function to');
$this->registerArgument('function', 'string', 'render function to use', true);
$this->registerArgument(
'force',
'bool',
'if set, it will be applied to the provided in, if not, it will be applied to each item for '
. 'iterables instead',
false,
false
);
}
public function validateArguments()
{
parent::validateArguments();
$this->validateRenderFunctionArgument('function');
}
public function render()
{
$in = $this->arguments['in'];
if ($in === null) {
$in = $this->renderChildren();
}
if (\is_iterable($in) && $this->arguments['force'] === false) {
return new GeneratorClosureIterator(fn () => $this->getProxyGenerator($this->arguments['function'], $in));
} else {
return new RenderableProxy($this->arguments['function'], $in);
}
}
protected function getProxyGenerator(InvokeRenderFunctionInterface $function, iterable $objects): \Generator
{
foreach ($objects as $object) {
yield new RenderableProxy($function, $object);
}
}
}