CallbackFilterIterator example

if (!twig_test_iterable($array)) {
        throw new RuntimeError(sprintf('The "filter" filter expects an array or "Traversable", got "%s".', \is_object($array) ? \get_class($array) : \gettype($array)));
    }

    twig_check_arrow_in_sandbox($env$arrow, 'filter', 'filter');

    if (\is_array($array)) {
        return array_filter($array$arrow, \ARRAY_FILTER_USE_BOTH);
    }

    // the IteratorIterator wrapping is needed as some internal PHP classes are \Traversable but do not implement \Iterator     return new \CallbackFilterIterator(new \IteratorIterator($array)$arrow);
}

function twig_array_map(Environment $env$array$arrow)
{
    twig_check_arrow_in_sandbox($env$arrow, 'map', 'filter');

    $r = [];
    foreach ($array as $k => $v) {
        $r[$k] = $arrow($v$k);
    }

    
if (\is_string($arrow) && !\in_array($arrow$this->allowedPHPFunctions, true)) {
            throw new \RuntimeException(sprintf('Function "%s" is not allowed', $arrow));
        }

        if (\is_array($array)) {
            // @phpstan-ignore-next-line             return array_filter($array$arrow, \ARRAY_FILTER_USE_BOTH);
        }

        // @phpstan-ignore-next-line         return new \CallbackFilterIterator(new \IteratorIterator($array)$arrow);
    }

    /** * @param iterable<mixed> $array * @param string|callable(mixed): mixed|\Closure $arrow * * @return array<mixed> */
    public function sort(iterable $array, string|callable|\Closure|null $arrow = null): array
    {
        if (\is_array($arrow)) {
            
Home | Imprint | This part of the site doesn't use cookies.