custom/plugins/SwagPlatformSecurity/src/Fixes/NEXT24667/SecurityExtension.php line 120

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\Security\Fixes\NEXT24667;
  3. use Twig\Extension\AbstractExtension;
  4. use Twig\TwigFilter;
  5. /**
  6.  * @internal
  7.  */
  8. class SecurityExtension extends AbstractExtension
  9. {
  10.     /**
  11.      * @var array<string>
  12.      */
  13.     private $allowedPHPFunctions;
  14.     /**
  15.      * @param array<string> $allowedPHPFunctions
  16.      */
  17.     public function __construct(array $allowedPHPFunctions)
  18.     {
  19.         $this->allowedPHPFunctions $allowedPHPFunctions;
  20.     }
  21.     /**
  22.      * @return TwigFilter[]
  23.      */
  24.     public function getFilters(): array
  25.     {
  26.         return [
  27.             new TwigFilter('map', [$this'map']),
  28.             new TwigFilter('reduce', [$this'reduce']),
  29.             new TwigFilter('filter', [$this'filter']),
  30.             new TwigFilter('sort', [$this'sort']),
  31.         ];
  32.     }
  33.     /**
  34.      * @param iterable<mixed> $array
  35.      * @param string|callable|\Closure $function
  36.      *
  37.      * @return array<mixed>
  38.      */
  39.     public function map(iterable $array$function): array
  40.     {
  41.         if (\is_array($function)) {
  42.             $function implode('::'$function);
  43.         }
  44.         if (\is_string($function) && !\in_array($function$this->allowedPHPFunctionstrue)) {
  45.             throw new \RuntimeException(sprintf('Function "%s" is not allowed'$function));
  46.         }
  47.         $result = [];
  48.         foreach ($array as $key => $value) {
  49.             // @phpstan-ignore-next-line
  50.             $result[$key] = $function($value);
  51.         }
  52.         return $result;
  53.     }
  54.     /**
  55.      * @param iterable<mixed> $array
  56.      * @param string|callable|\Closure $function
  57.      * @param mixed $initial
  58.      *
  59.      * @return mixed
  60.      */
  61.     public function reduce(iterable $array$function$initial null)
  62.     {
  63.         if (\is_array($function)) {
  64.             $function implode('::'$function);
  65.         }
  66.         if (\is_string($function) && !\in_array($function$this->allowedPHPFunctionstrue)) {
  67.             throw new \RuntimeException(sprintf('Function "%s" is not allowed'$function));
  68.         }
  69.         if (!\is_array($array)) {
  70.             $array iterator_to_array($array);
  71.         }
  72.         // @phpstan-ignore-next-line
  73.         return array_reduce($array$function$initial);
  74.     }
  75.     /**
  76.      * @param iterable<mixed> $array
  77.      * @param string|callable|\Closure $arrow
  78.      *
  79.      * @return iterable<mixed>
  80.      */
  81.     public function filter(iterable $array$arrow): iterable
  82.     {
  83.         if (\is_array($arrow)) {
  84.             $arrow implode('::'$arrow);
  85.         }
  86.         if (\is_string($arrow) && !\in_array($arrow$this->allowedPHPFunctionstrue)) {
  87.             throw new \RuntimeException(sprintf('Function "%s" is not allowed'$arrow));
  88.         }
  89.         if (\is_array($array)) {
  90.             // @phpstan-ignore-next-line
  91.             return array_filter($array$arrow\ARRAY_FILTER_USE_BOTH);
  92.         }
  93.         // @phpstan-ignore-next-line
  94.         return new \CallbackFilterIterator(new \IteratorIterator($array), $arrow);
  95.     }
  96.     /**
  97.      * @param iterable<mixed> $array
  98.      * @param string|callable|\Closure|null $arrow
  99.      *
  100.      * @return array<mixed>
  101.      */
  102.     public function sort(iterable $array$arrow null): array
  103.     {
  104.         if (\is_array($arrow)) {
  105.             $arrow implode('::'$arrow);
  106.         }
  107.         if (\is_string($arrow) && !\in_array($arrow$this->allowedPHPFunctionstrue)) {
  108.             throw new \RuntimeException(sprintf('Function "%s" is not allowed'$arrow));
  109.         }
  110.         if ($array instanceof \Traversable) {
  111.             $array iterator_to_array($array);
  112.         }
  113.         if ($arrow !== null) {
  114.             // @phpstan-ignore-next-line
  115.             uasort($array$arrow);
  116.         } else {
  117.             asort($array);
  118.         }
  119.         return $array;
  120.     }
  121. }