vendor/twig/twig/src/Extension/CoreExtension.php line 1909

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Twig\Extension;
  11. use Twig\DeprecatedCallableInfo;
  12. use Twig\Environment;
  13. use Twig\Error\LoaderError;
  14. use Twig\Error\RuntimeError;
  15. use Twig\Error\SyntaxError;
  16. use Twig\ExpressionParser\Infix\ArrowExpressionParser;
  17. use Twig\ExpressionParser\Infix\BinaryOperatorExpressionParser;
  18. use Twig\ExpressionParser\Infix\ConditionalTernaryExpressionParser;
  19. use Twig\ExpressionParser\Infix\DotExpressionParser;
  20. use Twig\ExpressionParser\Infix\FilterExpressionParser;
  21. use Twig\ExpressionParser\Infix\FunctionExpressionParser;
  22. use Twig\ExpressionParser\Infix\IsExpressionParser;
  23. use Twig\ExpressionParser\Infix\IsNotExpressionParser;
  24. use Twig\ExpressionParser\Infix\SquareBracketExpressionParser;
  25. use Twig\ExpressionParser\InfixAssociativity;
  26. use Twig\ExpressionParser\PrecedenceChange;
  27. use Twig\ExpressionParser\Prefix\GroupingExpressionParser;
  28. use Twig\ExpressionParser\Prefix\LiteralExpressionParser;
  29. use Twig\ExpressionParser\Prefix\UnaryOperatorExpressionParser;
  30. use Twig\Markup;
  31. use Twig\Node\Expression\AbstractExpression;
  32. use Twig\Node\Expression\Binary\AddBinary;
  33. use Twig\Node\Expression\Binary\AndBinary;
  34. use Twig\Node\Expression\Binary\BitwiseAndBinary;
  35. use Twig\Node\Expression\Binary\BitwiseOrBinary;
  36. use Twig\Node\Expression\Binary\BitwiseXorBinary;
  37. use Twig\Node\Expression\Binary\ConcatBinary;
  38. use Twig\Node\Expression\Binary\DivBinary;
  39. use Twig\Node\Expression\Binary\ElvisBinary;
  40. use Twig\Node\Expression\Binary\EndsWithBinary;
  41. use Twig\Node\Expression\Binary\EqualBinary;
  42. use Twig\Node\Expression\Binary\FloorDivBinary;
  43. use Twig\Node\Expression\Binary\GreaterBinary;
  44. use Twig\Node\Expression\Binary\GreaterEqualBinary;
  45. use Twig\Node\Expression\Binary\HasEveryBinary;
  46. use Twig\Node\Expression\Binary\HasSomeBinary;
  47. use Twig\Node\Expression\Binary\InBinary;
  48. use Twig\Node\Expression\Binary\LessBinary;
  49. use Twig\Node\Expression\Binary\LessEqualBinary;
  50. use Twig\Node\Expression\Binary\MatchesBinary;
  51. use Twig\Node\Expression\Binary\ModBinary;
  52. use Twig\Node\Expression\Binary\MulBinary;
  53. use Twig\Node\Expression\Binary\NotEqualBinary;
  54. use Twig\Node\Expression\Binary\NotInBinary;
  55. use Twig\Node\Expression\Binary\NullCoalesceBinary;
  56. use Twig\Node\Expression\Binary\OrBinary;
  57. use Twig\Node\Expression\Binary\PowerBinary;
  58. use Twig\Node\Expression\Binary\RangeBinary;
  59. use Twig\Node\Expression\Binary\SpaceshipBinary;
  60. use Twig\Node\Expression\Binary\StartsWithBinary;
  61. use Twig\Node\Expression\Binary\SubBinary;
  62. use Twig\Node\Expression\Binary\XorBinary;
  63. use Twig\Node\Expression\BlockReferenceExpression;
  64. use Twig\Node\Expression\Filter\DefaultFilter;
  65. use Twig\Node\Expression\FunctionNode\EnumCasesFunction;
  66. use Twig\Node\Expression\FunctionNode\EnumFunction;
  67. use Twig\Node\Expression\GetAttrExpression;
  68. use Twig\Node\Expression\ParentExpression;
  69. use Twig\Node\Expression\Test\ConstantTest;
  70. use Twig\Node\Expression\Test\DefinedTest;
  71. use Twig\Node\Expression\Test\DivisiblebyTest;
  72. use Twig\Node\Expression\Test\EvenTest;
  73. use Twig\Node\Expression\Test\NullTest;
  74. use Twig\Node\Expression\Test\OddTest;
  75. use Twig\Node\Expression\Test\SameasTest;
  76. use Twig\Node\Expression\Test\TrueTest;
  77. use Twig\Node\Expression\Unary\NegUnary;
  78. use Twig\Node\Expression\Unary\NotUnary;
  79. use Twig\Node\Expression\Unary\PosUnary;
  80. use Twig\Node\Expression\Unary\SpreadUnary;
  81. use Twig\Node\Node;
  82. use Twig\Parser;
  83. use Twig\Sandbox\SecurityNotAllowedMethodError;
  84. use Twig\Sandbox\SecurityNotAllowedPropertyError;
  85. use Twig\Source;
  86. use Twig\Template;
  87. use Twig\TemplateWrapper;
  88. use Twig\TokenParser\ApplyTokenParser;
  89. use Twig\TokenParser\BlockTokenParser;
  90. use Twig\TokenParser\DeprecatedTokenParser;
  91. use Twig\TokenParser\DoTokenParser;
  92. use Twig\TokenParser\EmbedTokenParser;
  93. use Twig\TokenParser\ExtendsTokenParser;
  94. use Twig\TokenParser\FlushTokenParser;
  95. use Twig\TokenParser\ForTokenParser;
  96. use Twig\TokenParser\FromTokenParser;
  97. use Twig\TokenParser\GuardTokenParser;
  98. use Twig\TokenParser\IfTokenParser;
  99. use Twig\TokenParser\ImportTokenParser;
  100. use Twig\TokenParser\IncludeTokenParser;
  101. use Twig\TokenParser\MacroTokenParser;
  102. use Twig\TokenParser\SetTokenParser;
  103. use Twig\TokenParser\TypesTokenParser;
  104. use Twig\TokenParser\UseTokenParser;
  105. use Twig\TokenParser\WithTokenParser;
  106. use Twig\TwigFilter;
  107. use Twig\TwigFunction;
  108. use Twig\TwigTest;
  109. use Twig\Util\CallableArgumentsExtractor;
  110. final class CoreExtension extends AbstractExtension
  111. {
  112.     public const ARRAY_LIKE_CLASSES = [
  113.         'ArrayIterator',
  114.         'ArrayObject',
  115.         'CachingIterator',
  116.         'RecursiveArrayIterator',
  117.         'RecursiveCachingIterator',
  118.         'SplDoublyLinkedList',
  119.         'SplFixedArray',
  120.         'SplObjectStorage',
  121.         'SplQueue',
  122.         'SplStack',
  123.         'WeakMap',
  124.     ];
  125.     private const DEFAULT_TRIM_CHARS " \t\n\r\0\x0B";
  126.     private $dateFormats = ['F j, Y H:i''%d days'];
  127.     private $numberFormat = [0'.'','];
  128.     private $timezone null;
  129.     /**
  130.      * Sets the default format to be used by the date filter.
  131.      *
  132.      * @param string|null $format             The default date format string
  133.      * @param string|null $dateIntervalFormat The default date interval format string
  134.      */
  135.     public function setDateFormat($format null$dateIntervalFormat null)
  136.     {
  137.         if (null !== $format) {
  138.             $this->dateFormats[0] = $format;
  139.         }
  140.         if (null !== $dateIntervalFormat) {
  141.             $this->dateFormats[1] = $dateIntervalFormat;
  142.         }
  143.     }
  144.     /**
  145.      * Gets the default format to be used by the date filter.
  146.      *
  147.      * @return array The default date format string and the default date interval format string
  148.      */
  149.     public function getDateFormat()
  150.     {
  151.         return $this->dateFormats;
  152.     }
  153.     /**
  154.      * Sets the default timezone to be used by the date filter.
  155.      *
  156.      * @param \DateTimeZone|string $timezone The default timezone string or a \DateTimeZone object
  157.      */
  158.     public function setTimezone($timezone)
  159.     {
  160.         $this->timezone $timezone instanceof \DateTimeZone $timezone : new \DateTimeZone($timezone);
  161.     }
  162.     /**
  163.      * Gets the default timezone to be used by the date filter.
  164.      *
  165.      * @return \DateTimeZone The default timezone currently in use
  166.      */
  167.     public function getTimezone()
  168.     {
  169.         if (null === $this->timezone) {
  170.             $this->timezone = new \DateTimeZone(date_default_timezone_get());
  171.         }
  172.         return $this->timezone;
  173.     }
  174.     /**
  175.      * Sets the default format to be used by the number_format filter.
  176.      *
  177.      * @param int    $decimal      the number of decimal places to use
  178.      * @param string $decimalPoint the character(s) to use for the decimal point
  179.      * @param string $thousandSep  the character(s) to use for the thousands separator
  180.      */
  181.     public function setNumberFormat($decimal$decimalPoint$thousandSep)
  182.     {
  183.         $this->numberFormat = [$decimal$decimalPoint$thousandSep];
  184.     }
  185.     /**
  186.      * Get the default format used by the number_format filter.
  187.      *
  188.      * @return array The arguments for number_format()
  189.      */
  190.     public function getNumberFormat()
  191.     {
  192.         return $this->numberFormat;
  193.     }
  194.     public function getTokenParsers(): array
  195.     {
  196.         return [
  197.             new ApplyTokenParser(),
  198.             new ForTokenParser(),
  199.             new IfTokenParser(),
  200.             new ExtendsTokenParser(),
  201.             new IncludeTokenParser(),
  202.             new BlockTokenParser(),
  203.             new UseTokenParser(),
  204.             new MacroTokenParser(),
  205.             new ImportTokenParser(),
  206.             new FromTokenParser(),
  207.             new SetTokenParser(),
  208.             new TypesTokenParser(),
  209.             new FlushTokenParser(),
  210.             new DoTokenParser(),
  211.             new EmbedTokenParser(),
  212.             new WithTokenParser(),
  213.             new DeprecatedTokenParser(),
  214.             new GuardTokenParser(),
  215.         ];
  216.     }
  217.     public function getFilters(): array
  218.     {
  219.         return [
  220.             // formatting filters
  221.             new TwigFilter('date', [$this'formatDate']),
  222.             new TwigFilter('date_modify', [$this'modifyDate']),
  223.             new TwigFilter('format', [self::class, 'sprintf']),
  224.             new TwigFilter('replace', [self::class, 'replace']),
  225.             new TwigFilter('number_format', [$this'formatNumber']),
  226.             new TwigFilter('abs''abs'),
  227.             new TwigFilter('round', [self::class, 'round']),
  228.             // encoding
  229.             new TwigFilter('url_encode', [self::class, 'urlencode']),
  230.             new TwigFilter('json_encode''json_encode'),
  231.             new TwigFilter('convert_encoding', [self::class, 'convertEncoding']),
  232.             // string filters
  233.             new TwigFilter('title', [self::class, 'titleCase'], ['needs_charset' => true]),
  234.             new TwigFilter('capitalize', [self::class, 'capitalize'], ['needs_charset' => true]),
  235.             new TwigFilter('upper', [self::class, 'upper'], ['needs_charset' => true]),
  236.             new TwigFilter('lower', [self::class, 'lower'], ['needs_charset' => true]),
  237.             new TwigFilter('striptags', [self::class, 'striptags']),
  238.             new TwigFilter('trim', [self::class, 'trim']),
  239.             new TwigFilter('nl2br', [self::class, 'nl2br'], ['pre_escape' => 'html''is_safe' => ['html']]),
  240.             new TwigFilter('spaceless', [self::class, 'spaceless'], ['is_safe' => ['html'], 'deprecation_info' => new DeprecatedCallableInfo('twig/twig''3.12')]),
  241.             // array helpers
  242.             new TwigFilter('join', [self::class, 'join']),
  243.             new TwigFilter('split', [self::class, 'split'], ['needs_charset' => true]),
  244.             new TwigFilter('sort', [self::class, 'sort'], ['needs_environment' => true]),
  245.             new TwigFilter('merge', [self::class, 'merge']),
  246.             new TwigFilter('batch', [self::class, 'batch']),
  247.             new TwigFilter('column', [self::class, 'column']),
  248.             new TwigFilter('filter', [self::class, 'filter'], ['needs_environment' => true]),
  249.             new TwigFilter('map', [self::class, 'map'], ['needs_environment' => true]),
  250.             new TwigFilter('reduce', [self::class, 'reduce'], ['needs_environment' => true]),
  251.             new TwigFilter('find', [self::class, 'find'], ['needs_environment' => true]),
  252.             // string/array filters
  253.             new TwigFilter('reverse', [self::class, 'reverse'], ['needs_charset' => true]),
  254.             new TwigFilter('shuffle', [self::class, 'shuffle'], ['needs_charset' => true]),
  255.             new TwigFilter('length', [self::class, 'length'], ['needs_charset' => true]),
  256.             new TwigFilter('slice', [self::class, 'slice'], ['needs_charset' => true]),
  257.             new TwigFilter('first', [self::class, 'first'], ['needs_charset' => true]),
  258.             new TwigFilter('last', [self::class, 'last'], ['needs_charset' => true]),
  259.             // iteration and runtime
  260.             new TwigFilter('default', [self::class, 'default'], ['node_class' => DefaultFilter::class]),
  261.             new TwigFilter('keys', [self::class, 'keys']),
  262.             new TwigFilter('invoke', [self::class, 'invoke']),
  263.         ];
  264.     }
  265.     public function getFunctions(): array
  266.     {
  267.         return [
  268.             new TwigFunction('parent'null, ['parser_callable' => [self::class, 'parseParentFunction']]),
  269.             new TwigFunction('block'null, ['parser_callable' => [self::class, 'parseBlockFunction']]),
  270.             new TwigFunction('attribute'null, ['parser_callable' => [self::class, 'parseAttributeFunction']]),
  271.             new TwigFunction('max''max'),
  272.             new TwigFunction('min''min'),
  273.             new TwigFunction('range''range'),
  274.             new TwigFunction('constant', [self::class, 'constant']),
  275.             new TwigFunction('cycle', [self::class, 'cycle']),
  276.             new TwigFunction('random', [self::class, 'random'], ['needs_charset' => true]),
  277.             new TwigFunction('date', [$this'convertDate']),
  278.             new TwigFunction('include', [self::class, 'include'], ['needs_environment' => true'needs_context' => true'is_safe' => ['all']]),
  279.             new TwigFunction('source', [self::class, 'source'], ['needs_environment' => true'is_safe' => ['all']]),
  280.             new TwigFunction('enum_cases', [self::class, 'enumCases'], ['node_class' => EnumCasesFunction::class]),
  281.             new TwigFunction('enum', [self::class, 'enum'], ['node_class' => EnumFunction::class]),
  282.         ];
  283.     }
  284.     public function getTests(): array
  285.     {
  286.         return [
  287.             new TwigTest('even'null, ['node_class' => EvenTest::class]),
  288.             new TwigTest('odd'null, ['node_class' => OddTest::class]),
  289.             new TwigTest('defined'null, ['node_class' => DefinedTest::class]),
  290.             new TwigTest('same as'null, ['node_class' => SameasTest::class, 'one_mandatory_argument' => true]),
  291.             new TwigTest('none'null, ['node_class' => NullTest::class]),
  292.             new TwigTest('null'null, ['node_class' => NullTest::class]),
  293.             new TwigTest('divisible by'null, ['node_class' => DivisiblebyTest::class, 'one_mandatory_argument' => true]),
  294.             new TwigTest('constant'null, ['node_class' => ConstantTest::class]),
  295.             new TwigTest('empty', [self::class, 'testEmpty']),
  296.             new TwigTest('iterable''is_iterable'),
  297.             new TwigTest('sequence', [self::class, 'testSequence']),
  298.             new TwigTest('mapping', [self::class, 'testMapping']),
  299.             new TwigTest('true'null, ['node_class' => TrueTest::class]),
  300.         ];
  301.     }
  302.     public function getNodeVisitors(): array
  303.     {
  304.         return [];
  305.     }
  306.     public function getExpressionParsers(): array
  307.     {
  308.         return [
  309.             // unary operators
  310.             new UnaryOperatorExpressionParser(NotUnary::class, 'not'50, new PrecedenceChange('twig/twig''3.15'70)),
  311.             new UnaryOperatorExpressionParser(SpreadUnary::class, '...'512description'Spread operator'),
  312.             new UnaryOperatorExpressionParser(NegUnary::class, '-'500),
  313.             new UnaryOperatorExpressionParser(PosUnary::class, '+'500),
  314.             // binary operators
  315.             new BinaryOperatorExpressionParser(ElvisBinary::class, '?:'5InfixAssociativity::Rightdescription'Elvis operator (a ?: b)'aliases: ['? :']),
  316.             new BinaryOperatorExpressionParser(NullCoalesceBinary::class, '??'300InfixAssociativity::Right, new PrecedenceChange('twig/twig''3.15'5), description'Null coalescing operator (a ?? b)'),
  317.             new BinaryOperatorExpressionParser(OrBinary::class, 'or'10),
  318.             new BinaryOperatorExpressionParser(XorBinary::class, 'xor'12),
  319.             new BinaryOperatorExpressionParser(AndBinary::class, 'and'15),
  320.             new BinaryOperatorExpressionParser(BitwiseOrBinary::class, 'b-or'16),
  321.             new BinaryOperatorExpressionParser(BitwiseXorBinary::class, 'b-xor'17),
  322.             new BinaryOperatorExpressionParser(BitwiseAndBinary::class, 'b-and'18),
  323.             new BinaryOperatorExpressionParser(EqualBinary::class, '=='20),
  324.             new BinaryOperatorExpressionParser(NotEqualBinary::class, '!='20),
  325.             new BinaryOperatorExpressionParser(SpaceshipBinary::class, '<=>'20),
  326.             new BinaryOperatorExpressionParser(LessBinary::class, '<'20),
  327.             new BinaryOperatorExpressionParser(GreaterBinary::class, '>'20),
  328.             new BinaryOperatorExpressionParser(GreaterEqualBinary::class, '>='20),
  329.             new BinaryOperatorExpressionParser(LessEqualBinary::class, '<='20),
  330.             new BinaryOperatorExpressionParser(NotInBinary::class, 'not in'20),
  331.             new BinaryOperatorExpressionParser(InBinary::class, 'in'20),
  332.             new BinaryOperatorExpressionParser(MatchesBinary::class, 'matches'20),
  333.             new BinaryOperatorExpressionParser(StartsWithBinary::class, 'starts with'20),
  334.             new BinaryOperatorExpressionParser(EndsWithBinary::class, 'ends with'20),
  335.             new BinaryOperatorExpressionParser(HasSomeBinary::class, 'has some'20),
  336.             new BinaryOperatorExpressionParser(HasEveryBinary::class, 'has every'20),
  337.             new BinaryOperatorExpressionParser(RangeBinary::class, '..'25),
  338.             new BinaryOperatorExpressionParser(AddBinary::class, '+'30),
  339.             new BinaryOperatorExpressionParser(SubBinary::class, '-'30),
  340.             new BinaryOperatorExpressionParser(ConcatBinary::class, '~'40precedenceChange: new PrecedenceChange('twig/twig''3.15'27)),
  341.             new BinaryOperatorExpressionParser(MulBinary::class, '*'60),
  342.             new BinaryOperatorExpressionParser(DivBinary::class, '/'60),
  343.             new BinaryOperatorExpressionParser(FloorDivBinary::class, '//'60description'Floor division'),
  344.             new BinaryOperatorExpressionParser(ModBinary::class, '%'60),
  345.             new BinaryOperatorExpressionParser(PowerBinary::class, '**'200InfixAssociativity::Rightdescription'Exponentiation operator'),
  346.             // ternary operator
  347.             new ConditionalTernaryExpressionParser(),
  348.             // Twig callables
  349.             new IsExpressionParser(),
  350.             new IsNotExpressionParser(),
  351.             new FilterExpressionParser(),
  352.             new FunctionExpressionParser(),
  353.             // get attribute operators
  354.             new DotExpressionParser(),
  355.             new SquareBracketExpressionParser(),
  356.             // group expression
  357.             new GroupingExpressionParser(),
  358.             // arrow function
  359.             new ArrowExpressionParser(),
  360.             // all literals
  361.             new LiteralExpressionParser(),
  362.         ];
  363.     }
  364.     /**
  365.      * Cycles over a sequence.
  366.      *
  367.      * @param array|\ArrayAccess $values   A non-empty sequence of values
  368.      * @param int<0, max>        $position The position of the value to return in the cycle
  369.      *
  370.      * @return mixed The value at the given position in the sequence, wrapping around as needed
  371.      *
  372.      * @internal
  373.      */
  374.     public static function cycle($values$position): mixed
  375.     {
  376.         if (!\is_array($values)) {
  377.             if (!$values instanceof \ArrayAccess) {
  378.                 throw new RuntimeError('The "cycle" function expects an array or "ArrayAccess" as first argument.');
  379.             }
  380.             if (!is_countable($values)) {
  381.                 // To be uncommented in 4.0
  382.                 // throw new RuntimeError('The "cycle" function expects a countable sequence as first argument.');
  383.                 trigger_deprecation('twig/twig''3.12''Passing a non-countable sequence of values to "%s()" is deprecated.'__METHOD__);
  384.                 return $values;
  385.             }
  386.             $values self::toArray($valuesfalse);
  387.         }
  388.         if (!$count \count($values)) {
  389.             throw new RuntimeError('The "cycle" function expects a non-empty sequence.');
  390.         }
  391.         return $values[$position $count];
  392.     }
  393.     /**
  394.      * Returns a random value depending on the supplied parameter type:
  395.      * - a random item from a \Traversable or array
  396.      * - a random character from a string
  397.      * - a random integer between 0 and the integer parameter.
  398.      *
  399.      * @param \Traversable|array|int|float|string $values The values to pick a random item from
  400.      * @param int|null                            $max    Maximum value used when $values is an int
  401.      *
  402.      * @return mixed A random value from the given sequence
  403.      *
  404.      * @throws RuntimeError when $values is an empty array (does not apply to an empty string which is returned as is)
  405.      *
  406.      * @internal
  407.      */
  408.     public static function random(string $charset$values null$max null)
  409.     {
  410.         if (null === $values) {
  411.             return null === $max mt_rand() : mt_rand(0, (int) $max);
  412.         }
  413.         if (\is_int($values) || \is_float($values)) {
  414.             if (null === $max) {
  415.                 if ($values 0) {
  416.                     $max 0;
  417.                     $min $values;
  418.                 } else {
  419.                     $max $values;
  420.                     $min 0;
  421.                 }
  422.             } else {
  423.                 $min $values;
  424.             }
  425.             return mt_rand((int) $min, (int) $max);
  426.         }
  427.         if (\is_string($values)) {
  428.             if ('' === $values) {
  429.                 return '';
  430.             }
  431.             if ('UTF-8' !== $charset) {
  432.                 $values self::convertEncoding($values'UTF-8'$charset);
  433.             }
  434.             // unicode version of str_split()
  435.             // split at all positions, but not after the start and not before the end
  436.             $values preg_split('/(?<!^)(?!$)/u'$values);
  437.             if ('UTF-8' !== $charset) {
  438.                 foreach ($values as $i => $value) {
  439.                     $values[$i] = self::convertEncoding($value$charset'UTF-8');
  440.                 }
  441.             }
  442.         }
  443.         if (!is_iterable($values)) {
  444.             return $values;
  445.         }
  446.         $values self::toArray($values);
  447.         if (=== \count($values)) {
  448.             throw new RuntimeError('The "random" function cannot pick from an empty sequence or mapping.');
  449.         }
  450.         return $values[array_rand($values1)];
  451.     }
  452.     /**
  453.      * Formats a date.
  454.      *
  455.      *   {{ post.published_at|date("m/d/Y") }}
  456.      *
  457.      * @param \DateTimeInterface|\DateInterval|string|int|null $date     A date, a timestamp or null to use the current time
  458.      * @param string|null                                      $format   The target format, null to use the default
  459.      * @param \DateTimeZone|string|false|null                  $timezone The target timezone, null to use the default, false to leave unchanged
  460.      */
  461.     public function formatDate($date$format null$timezone null): string
  462.     {
  463.         if (null === $format) {
  464.             $formats $this->getDateFormat();
  465.             $format $date instanceof \DateInterval $formats[1] : $formats[0];
  466.         }
  467.         if ($date instanceof \DateInterval) {
  468.             return $date->format($format);
  469.         }
  470.         return $this->convertDate($date$timezone)->format($format);
  471.     }
  472.     /**
  473.      * Returns a new date object modified.
  474.      *
  475.      *   {{ post.published_at|date_modify("-1day")|date("m/d/Y") }}
  476.      *
  477.      * @param \DateTimeInterface|string|int|null $date     A date, a timestamp or null to use the current time
  478.      * @param string                             $modifier A modifier string
  479.      *
  480.      * @return \DateTime|\DateTimeImmutable
  481.      *
  482.      * @internal
  483.      */
  484.     public function modifyDate($date$modifier)
  485.     {
  486.         return $this->convertDate($datefalse)->modify($modifier);
  487.     }
  488.     /**
  489.      * Returns a formatted string.
  490.      *
  491.      * @param string|null $format
  492.      *
  493.      * @internal
  494.      */
  495.     public static function sprintf($format, ...$values): string
  496.     {
  497.         return \sprintf($format ?? '', ...$values);
  498.     }
  499.     /**
  500.      * @internal
  501.      */
  502.     public static function dateConverter(Environment $env$date$format null$timezone null): string
  503.     {
  504.         return $env->getExtension(self::class)->formatDate($date$format$timezone);
  505.     }
  506.     /**
  507.      * Converts an input to a \DateTime instance.
  508.      *
  509.      *    {% if date(user.created_at) < date('+2days') %}
  510.      *      {# do something #}
  511.      *    {% endif %}
  512.      *
  513.      * @param \DateTimeInterface|string|int|null $date     A date, a timestamp or null to use the current time
  514.      * @param \DateTimeZone|string|false|null    $timezone The target timezone, null to use the default, false to leave unchanged
  515.      *
  516.      * @return \DateTime|\DateTimeImmutable
  517.      */
  518.     public function convertDate($date null$timezone null)
  519.     {
  520.         // determine the timezone
  521.         if (false !== $timezone) {
  522.             if (null === $timezone) {
  523.                 $timezone $this->getTimezone();
  524.             } elseif (!$timezone instanceof \DateTimeZone) {
  525.                 $timezone = new \DateTimeZone($timezone);
  526.             }
  527.         }
  528.         // immutable dates
  529.         if ($date instanceof \DateTimeImmutable) {
  530.             return false !== $timezone $date->setTimezone($timezone) : $date;
  531.         }
  532.         if ($date instanceof \DateTime) {
  533.             $date = clone $date;
  534.             if (false !== $timezone) {
  535.                 $date->setTimezone($timezone);
  536.             }
  537.             return $date;
  538.         }
  539.         if (null === $date || 'now' === $date) {
  540.             if (null === $date) {
  541.                 $date 'now';
  542.             }
  543.             return new \DateTime($datefalse !== $timezone $timezone $this->getTimezone());
  544.         }
  545.         $asString = (string) $date;
  546.         if (ctype_digit($asString) || ('' !== $asString && '-' === $asString[0] && ctype_digit(substr($asString1)))) {
  547.             $date = new \DateTime('@'.$date);
  548.         } else {
  549.             $date = new \DateTime($date);
  550.         }
  551.         if (false !== $timezone) {
  552.             $date->setTimezone($timezone);
  553.         }
  554.         return $date;
  555.     }
  556.     /**
  557.      * Replaces strings within a string.
  558.      *
  559.      * @param string|null        $str  String to replace in
  560.      * @param array|\Traversable $from Replace values
  561.      *
  562.      * @internal
  563.      */
  564.     public static function replace($str$from): string
  565.     {
  566.         if (!is_iterable($from)) {
  567.             throw new RuntimeError(\sprintf('The "replace" filter expects a sequence or a mapping, got "%s".'get_debug_type($from)));
  568.         }
  569.         return strtr($str ?? ''self::toArray($from));
  570.     }
  571.     /**
  572.      * Rounds a number.
  573.      *
  574.      * @param int|float|string|null   $value     The value to round
  575.      * @param int|float               $precision The rounding precision
  576.      * @param 'common'|'ceil'|'floor' $method    The method to use for rounding
  577.      *
  578.      * @return float The rounded number
  579.      *
  580.      * @internal
  581.      */
  582.     public static function round($value$precision 0$method 'common')
  583.     {
  584.         $value = (float) $value;
  585.         if ('common' === $method) {
  586.             return round($value$precision);
  587.         }
  588.         if ('ceil' !== $method && 'floor' !== $method) {
  589.             throw new RuntimeError('The "round" filter only supports the "common", "ceil", and "floor" methods.');
  590.         }
  591.         return $method($value 10 ** $precision) / 10 ** $precision;
  592.     }
  593.     /**
  594.      * Formats a number.
  595.      *
  596.      * All of the formatting options can be left null, in that case the defaults will
  597.      * be used. Supplying any of the parameters will override the defaults set in the
  598.      * environment object.
  599.      *
  600.      * @param mixed       $number       A float/int/string of the number to format
  601.      * @param int|null    $decimal      the number of decimal points to display
  602.      * @param string|null $decimalPoint the character(s) to use for the decimal point
  603.      * @param string|null $thousandSep  the character(s) to use for the thousands separator
  604.      */
  605.     public function formatNumber($number$decimal null$decimalPoint null$thousandSep null): string
  606.     {
  607.         $defaults $this->getNumberFormat();
  608.         if (null === $decimal) {
  609.             $decimal $defaults[0];
  610.         }
  611.         if (null === $decimalPoint) {
  612.             $decimalPoint $defaults[1];
  613.         }
  614.         if (null === $thousandSep) {
  615.             $thousandSep $defaults[2];
  616.         }
  617.         return number_format((float) $number$decimal$decimalPoint$thousandSep);
  618.     }
  619.     /**
  620.      * URL encodes (RFC 3986) a string as a path segment or an array as a query string.
  621.      *
  622.      * @param string|array|null $url A URL or an array of query parameters
  623.      *
  624.      * @internal
  625.      */
  626.     public static function urlencode($url): string
  627.     {
  628.         if (\is_array($url)) {
  629.             return http_build_query($url'''&'\PHP_QUERY_RFC3986);
  630.         }
  631.         return rawurlencode($url ?? '');
  632.     }
  633.     /**
  634.      * Merges any number of arrays or Traversable objects.
  635.      *
  636.      *  {% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}
  637.      *
  638.      *  {% set items = items|merge({ 'peugeot': 'car' }, { 'banana': 'fruit' }) %}
  639.      *
  640.      *  {# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car', 'banana': 'fruit' } #}
  641.      *
  642.      * @param array|\Traversable ...$arrays Any number of arrays or Traversable objects to merge
  643.      *
  644.      * @internal
  645.      */
  646.     public static function merge(...$arrays): array
  647.     {
  648.         $result = [];
  649.         foreach ($arrays as $argNumber => $array) {
  650.             if (!is_iterable($array)) {
  651.                 throw new RuntimeError(\sprintf('The "merge" filter expects a sequence or a mapping, got "%s" for argument %d.'get_debug_type($array), $argNumber 1));
  652.             }
  653.             $result array_merge($resultself::toArray($array));
  654.         }
  655.         return $result;
  656.     }
  657.     /**
  658.      * Slices a variable.
  659.      *
  660.      * @param mixed $item         A variable
  661.      * @param int   $start        Start of the slice
  662.      * @param int   $length       Size of the slice
  663.      * @param bool  $preserveKeys Whether to preserve key or not (when the input is an array)
  664.      *
  665.      * @return mixed The sliced variable
  666.      *
  667.      * @internal
  668.      */
  669.     public static function slice(string $charset$item$start$length null$preserveKeys false)
  670.     {
  671.         if ($item instanceof \Traversable) {
  672.             while ($item instanceof \IteratorAggregate) {
  673.                 $item $item->getIterator();
  674.             }
  675.             if ($start >= && $length >= && $item instanceof \Iterator) {
  676.                 try {
  677.                     return iterator_to_array(new \LimitIterator($item$start$length ?? -1), $preserveKeys);
  678.                 } catch (\OutOfBoundsException $e) {
  679.                     return [];
  680.                 }
  681.             }
  682.             $item iterator_to_array($item$preserveKeys);
  683.         }
  684.         if (\is_array($item)) {
  685.             return \array_slice($item$start$length$preserveKeys);
  686.         }
  687.         return mb_substr((string) $item$start$length$charset);
  688.     }
  689.     /**
  690.      * Returns the first element of the item.
  691.      *
  692.      * @param mixed $item A variable
  693.      *
  694.      * @return mixed The first element of the item
  695.      *
  696.      * @internal
  697.      */
  698.     public static function first(string $charset$item)
  699.     {
  700.         $elements self::slice($charset$item01false);
  701.         return \is_string($elements) ? $elements current($elements);
  702.     }
  703.     /**
  704.      * Returns the last element of the item.
  705.      *
  706.      * @param mixed $item A variable
  707.      *
  708.      * @return mixed The last element of the item
  709.      *
  710.      * @internal
  711.      */
  712.     public static function last(string $charset$item)
  713.     {
  714.         $elements self::slice($charset$item, -11false);
  715.         return \is_string($elements) ? $elements current($elements);
  716.     }
  717.     /**
  718.      * Joins the values to a string.
  719.      *
  720.      * The separators between elements are empty strings per default, you can define them with the optional parameters.
  721.      *
  722.      *  {{ [1, 2, 3]|join(', ', ' and ') }}
  723.      *  {# returns 1, 2 and 3 #}
  724.      *
  725.      *  {{ [1, 2, 3]|join('|') }}
  726.      *  {# returns 1|2|3 #}
  727.      *
  728.      *  {{ [1, 2, 3]|join }}
  729.      *  {# returns 123 #}
  730.      *
  731.      * @param iterable|array|string|float|int|bool|null $value An array
  732.      * @param string                                    $glue  The separator
  733.      * @param string|null                               $and   The separator for the last pair
  734.      *
  735.      * @internal
  736.      */
  737.     public static function join($value$glue ''$and null): string
  738.     {
  739.         if (!is_iterable($value)) {
  740.             $value = (array) $value;
  741.         }
  742.         $value self::toArray($valuefalse);
  743.         if (=== \count($value)) {
  744.             return '';
  745.         }
  746.         if (null === $and || $and === $glue) {
  747.             return implode($glue$value);
  748.         }
  749.         if (=== \count($value)) {
  750.             return $value[0];
  751.         }
  752.         return implode($glue\array_slice($value0, -1)).$and.$value[\count($value) - 1];
  753.     }
  754.     /**
  755.      * Splits the string into an array.
  756.      *
  757.      *  {{ "one,two,three"|split(',') }}
  758.      *  {# returns [one, two, three] #}
  759.      *
  760.      *  {{ "one,two,three,four,five"|split(',', 3) }}
  761.      *  {# returns [one, two, "three,four,five"] #}
  762.      *
  763.      *  {{ "123"|split('') }}
  764.      *  {# returns [1, 2, 3] #}
  765.      *
  766.      *  {{ "aabbcc"|split('', 2) }}
  767.      *  {# returns [aa, bb, cc] #}
  768.      *
  769.      * @param string|null $value     A string
  770.      * @param string      $delimiter The delimiter
  771.      * @param int|null    $limit     The limit
  772.      *
  773.      * @internal
  774.      */
  775.     public static function split(string $charset$value$delimiter$limit null): array
  776.     {
  777.         $value $value ?? '';
  778.         if ('' !== $delimiter) {
  779.             return null === $limit explode($delimiter$value) : explode($delimiter$value$limit);
  780.         }
  781.         if ($limit <= 1) {
  782.             return preg_split('/(?<!^)(?!$)/u'$value);
  783.         }
  784.         $length mb_strlen($value$charset);
  785.         if ($length $limit) {
  786.             return [$value];
  787.         }
  788.         $r = [];
  789.         for ($i 0$i $length$i += $limit) {
  790.             $r[] = mb_substr($value$i$limit$charset);
  791.         }
  792.         return $r;
  793.     }
  794.     /**
  795.      * @internal
  796.      */
  797.     public static function default($value$default '')
  798.     {
  799.         if (self::testEmpty($value)) {
  800.             return $default;
  801.         }
  802.         return $value;
  803.     }
  804.     /**
  805.      * Returns the keys for the given array.
  806.      *
  807.      * It is useful when you want to iterate over the keys of an array:
  808.      *
  809.      *  {% for key in array|keys %}
  810.      *      {# ... #}
  811.      *  {% endfor %}
  812.      *
  813.      * @internal
  814.      */
  815.     public static function keys($array): array
  816.     {
  817.         if ($array instanceof \Traversable) {
  818.             while ($array instanceof \IteratorAggregate) {
  819.                 $array $array->getIterator();
  820.             }
  821.             $keys = [];
  822.             if ($array instanceof \Iterator) {
  823.                 $array->rewind();
  824.                 while ($array->valid()) {
  825.                     $keys[] = $array->key();
  826.                     $array->next();
  827.                 }
  828.                 return $keys;
  829.             }
  830.             foreach ($array as $key => $item) {
  831.                 $keys[] = $key;
  832.             }
  833.             return $keys;
  834.         }
  835.         if (!\is_array($array)) {
  836.             return [];
  837.         }
  838.         return array_keys($array);
  839.     }
  840.     /**
  841.      * Invokes a callable.
  842.      *
  843.      * @internal
  844.      */
  845.     public static function invoke(\Closure $arrow, ...$arguments): mixed
  846.     {
  847.         return $arrow(...$arguments);
  848.     }
  849.     /**
  850.      * Reverses a variable.
  851.      *
  852.      * @param array|\Traversable|string|null $item         An array, a \Traversable instance, or a string
  853.      * @param bool                           $preserveKeys Whether to preserve key or not
  854.      *
  855.      * @return mixed The reversed input
  856.      *
  857.      * @internal
  858.      */
  859.     public static function reverse(string $charset$item$preserveKeys false)
  860.     {
  861.         if ($item instanceof \Traversable) {
  862.             return array_reverse(iterator_to_array($item), $preserveKeys);
  863.         }
  864.         if (\is_array($item)) {
  865.             return array_reverse($item$preserveKeys);
  866.         }
  867.         $string = (string) $item;
  868.         if ('UTF-8' !== $charset) {
  869.             $string self::convertEncoding($string'UTF-8'$charset);
  870.         }
  871.         preg_match_all('/./us'$string$matches);
  872.         $string implode(''array_reverse($matches[0]));
  873.         if ('UTF-8' !== $charset) {
  874.             $string self::convertEncoding($string$charset'UTF-8');
  875.         }
  876.         return $string;
  877.     }
  878.     /**
  879.      * Shuffles an array, a \Traversable instance, or a string.
  880.      * The function does not preserve keys.
  881.      *
  882.      * @param array|\Traversable|string|null $item
  883.      *
  884.      * @internal
  885.      */
  886.     public static function shuffle(string $charset$item)
  887.     {
  888.         if (\is_string($item)) {
  889.             if ('UTF-8' !== $charset) {
  890.                 $item self::convertEncoding($item'UTF-8'$charset);
  891.             }
  892.             $item preg_split('/(?<!^)(?!$)/u'$item, -1);
  893.             shuffle($item);
  894.             $item implode(''$item);
  895.             if ('UTF-8' !== $charset) {
  896.                 $item self::convertEncoding($item$charset'UTF-8');
  897.             }
  898.             return $item;
  899.         }
  900.         if (is_iterable($item)) {
  901.             $item self::toArray($itemfalse);
  902.             shuffle($item);
  903.         }
  904.         return $item;
  905.     }
  906.     /**
  907.      * Sorts an array.
  908.      *
  909.      * @param array|\Traversable $array
  910.      * @param ?\Closure          $arrow
  911.      *
  912.      * @internal
  913.      */
  914.     public static function sort(Environment $env$array$arrow null): array
  915.     {
  916.         if ($array instanceof \Traversable) {
  917.             $array iterator_to_array($array);
  918.         } elseif (!\is_array($array)) {
  919.             throw new RuntimeError(\sprintf('The "sort" filter expects a sequence or a mapping, got "%s".'get_debug_type($array)));
  920.         }
  921.         if (null !== $arrow) {
  922.             self::checkArrow($env$arrow'sort''filter');
  923.             uasort($array$arrow);
  924.         } else {
  925.             asort($array);
  926.         }
  927.         return $array;
  928.     }
  929.     /**
  930.      * @internal
  931.      */
  932.     public static function inFilter($value$compare)
  933.     {
  934.         if ($value instanceof Markup) {
  935.             $value = (string) $value;
  936.         }
  937.         if ($compare instanceof Markup) {
  938.             $compare = (string) $compare;
  939.         }
  940.         if (\is_string($compare)) {
  941.             if (\is_string($value) || \is_int($value) || \is_float($value)) {
  942.                 return '' === $value || str_contains($compare, (string) $value);
  943.             }
  944.             return false;
  945.         }
  946.         if (!is_iterable($compare)) {
  947.             return false;
  948.         }
  949.         if (\is_object($value) || \is_resource($value)) {
  950.             if (!\is_array($compare)) {
  951.                 foreach ($compare as $item) {
  952.                     if ($item === $value) {
  953.                         return true;
  954.                     }
  955.                 }
  956.                 return false;
  957.             }
  958.             return \in_array($value$comparetrue);
  959.         }
  960.         foreach ($compare as $item) {
  961.             if (=== self::compare($value$item)) {
  962.                 return true;
  963.             }
  964.         }
  965.         return false;
  966.     }
  967.     /**
  968.      * Compares two values using a more strict version of the PHP non-strict comparison operator.
  969.      *
  970.      * @see https://wiki.php.net/rfc/string_to_number_comparison
  971.      * @see https://wiki.php.net/rfc/trailing_whitespace_numerics
  972.      *
  973.      * @internal
  974.      */
  975.     public static function compare($a$b)
  976.     {
  977.         // int <=> string
  978.         if (\is_int($a) && \is_string($b)) {
  979.             $bTrim trim($b" \t\n\r\v\f");
  980.             if (!is_numeric($bTrim)) {
  981.                 return (string) $a <=> $b;
  982.             }
  983.             if ((int) $bTrim == $bTrim) {
  984.                 return $a <=> (int) $bTrim;
  985.             } else {
  986.                 return (float) $a <=> (float) $bTrim;
  987.             }
  988.         }
  989.         if (\is_string($a) && \is_int($b)) {
  990.             $aTrim trim($a" \t\n\r\v\f");
  991.             if (!is_numeric($aTrim)) {
  992.                 return $a <=> (string) $b;
  993.             }
  994.             if ((int) $aTrim == $aTrim) {
  995.                 return (int) $aTrim <=> $b;
  996.             } else {
  997.                 return (float) $aTrim <=> (float) $b;
  998.             }
  999.         }
  1000.         // float <=> string
  1001.         if (\is_float($a) && \is_string($b)) {
  1002.             if (is_nan($a)) {
  1003.                 return 1;
  1004.             }
  1005.             $bTrim trim($b" \t\n\r\v\f");
  1006.             if (!is_numeric($bTrim)) {
  1007.                 return (string) $a <=> $b;
  1008.             }
  1009.             return $a <=> (float) $bTrim;
  1010.         }
  1011.         if (\is_string($a) && \is_float($b)) {
  1012.             if (is_nan($b)) {
  1013.                 return 1;
  1014.             }
  1015.             $aTrim trim($a" \t\n\r\v\f");
  1016.             if (!is_numeric($aTrim)) {
  1017.                 return $a <=> (string) $b;
  1018.             }
  1019.             return (float) $aTrim <=> $b;
  1020.         }
  1021.         // fallback to <=>
  1022.         return $a <=> $b;
  1023.     }
  1024.     /**
  1025.      * @throws RuntimeError When an invalid pattern is used
  1026.      *
  1027.      * @internal
  1028.      */
  1029.     public static function matches(string $regexp, ?string $str): int
  1030.     {
  1031.         set_error_handler(function ($t$m) use ($regexp) {
  1032.             throw new RuntimeError(\sprintf('Regexp "%s" passed to "matches" is not valid'$regexp).substr($m12));
  1033.         });
  1034.         try {
  1035.             return preg_match($regexp$str ?? '');
  1036.         } finally {
  1037.             restore_error_handler();
  1038.         }
  1039.     }
  1040.     /**
  1041.      * Returns a trimmed string.
  1042.      *
  1043.      * @param string|\Stringable|null $string
  1044.      * @param string|null             $characterMask
  1045.      * @param string                  $side          left, right, or both
  1046.      *
  1047.      * @throws RuntimeError When an invalid trimming side is used
  1048.      *
  1049.      * @internal
  1050.      */
  1051.     public static function trim($string$characterMask null$side 'both'): string|\Stringable
  1052.     {
  1053.         if (null === $characterMask) {
  1054.             $characterMask self::DEFAULT_TRIM_CHARS;
  1055.         }
  1056.         $trimmed = match ($side) {
  1057.             'both' => trim($string ?? ''$characterMask),
  1058.             'left' => ltrim($string ?? ''$characterMask),
  1059.             'right' => rtrim($string ?? ''$characterMask),
  1060.             default => throw new RuntimeError('Trimming side must be "left", "right" or "both".'),
  1061.         };
  1062.         // trimming a safe string with the default character mask always returns a safe string (independently of the context)
  1063.         return $string instanceof Markup && self::DEFAULT_TRIM_CHARS === $characterMask ? new Markup($trimmed$string->getCharset()) : $trimmed;
  1064.     }
  1065.     /**
  1066.      * Inserts HTML line breaks before all newlines in a string.
  1067.      *
  1068.      * @param string|null $string
  1069.      *
  1070.      * @internal
  1071.      */
  1072.     public static function nl2br($string): string
  1073.     {
  1074.         return nl2br($string ?? '');
  1075.     }
  1076.     /**
  1077.      * Removes whitespaces between HTML tags.
  1078.      *
  1079.      * @param string|null $content
  1080.      *
  1081.      * @internal
  1082.      */
  1083.     public static function spaceless($content): string
  1084.     {
  1085.         return trim(preg_replace('/>\s+</''><'$content ?? ''));
  1086.     }
  1087.     /**
  1088.      * @param string|null $string
  1089.      * @param string      $to
  1090.      * @param string      $from
  1091.      *
  1092.      * @internal
  1093.      */
  1094.     public static function convertEncoding($string$to$from): string
  1095.     {
  1096.         if (!\function_exists('iconv')) {
  1097.             throw new RuntimeError('Unable to convert encoding: required function iconv() does not exist. You should install ext-iconv or symfony/polyfill-iconv.');
  1098.         }
  1099.         return iconv($from$to$string ?? '');
  1100.     }
  1101.     /**
  1102.      * Returns the length of a variable.
  1103.      *
  1104.      * @param mixed $thing A variable
  1105.      *
  1106.      * @internal
  1107.      */
  1108.     public static function length(string $charset$thing): int
  1109.     {
  1110.         if (null === $thing) {
  1111.             return 0;
  1112.         }
  1113.         if (\is_scalar($thing)) {
  1114.             return mb_strlen($thing$charset);
  1115.         }
  1116.         if ($thing instanceof \Countable || \is_array($thing) || $thing instanceof \SimpleXMLElement) {
  1117.             return \count($thing);
  1118.         }
  1119.         if ($thing instanceof \Traversable) {
  1120.             return iterator_count($thing);
  1121.         }
  1122.         if ($thing instanceof \Stringable) {
  1123.             return mb_strlen((string) $thing$charset);
  1124.         }
  1125.         return 1;
  1126.     }
  1127.     /**
  1128.      * Converts a string to uppercase.
  1129.      *
  1130.      * @param string|null $string A string
  1131.      *
  1132.      * @internal
  1133.      */
  1134.     public static function upper(string $charset$string): string
  1135.     {
  1136.         return mb_strtoupper($string ?? ''$charset);
  1137.     }
  1138.     /**
  1139.      * Converts a string to lowercase.
  1140.      *
  1141.      * @param string|null $string A string
  1142.      *
  1143.      * @internal
  1144.      */
  1145.     public static function lower(string $charset$string): string
  1146.     {
  1147.         return mb_strtolower($string ?? ''$charset);
  1148.     }
  1149.     /**
  1150.      * Strips HTML and PHP tags from a string.
  1151.      *
  1152.      * @param string|null          $string
  1153.      * @param string[]|string|null $allowable_tags
  1154.      *
  1155.      * @internal
  1156.      */
  1157.     public static function striptags($string$allowable_tags null): string
  1158.     {
  1159.         return strip_tags($string ?? ''$allowable_tags);
  1160.     }
  1161.     /**
  1162.      * Returns a titlecased string.
  1163.      *
  1164.      * @param string|null $string A string
  1165.      *
  1166.      * @internal
  1167.      */
  1168.     public static function titleCase(string $charset$string): string
  1169.     {
  1170.         return mb_convert_case($string ?? ''\MB_CASE_TITLE$charset);
  1171.     }
  1172.     /**
  1173.      * Returns a capitalized string.
  1174.      *
  1175.      * @param string|null $string A string
  1176.      *
  1177.      * @internal
  1178.      */
  1179.     public static function capitalize(string $charset$string): string
  1180.     {
  1181.         return mb_strtoupper(mb_substr($string ?? ''01$charset), $charset).mb_strtolower(mb_substr($string ?? ''1null$charset), $charset);
  1182.     }
  1183.     /**
  1184.      * @internal
  1185.      *
  1186.      * to be removed in 4.0
  1187.      */
  1188.     public static function callMacro(Template $templatestring $method, array $argsint $lineno, array $contextSource $source)
  1189.     {
  1190.         if (!method_exists($template$method)) {
  1191.             $parent $template;
  1192.             while ($parent $parent->getParent($context)) {
  1193.                 if (method_exists($parent$method)) {
  1194.                     return $parent->$method(...$args);
  1195.                 }
  1196.             }
  1197.             throw new RuntimeError(\sprintf('Macro "%s" is not defined in template "%s".'substr($method\strlen('macro_')), $template->getTemplateName()), $lineno$source);
  1198.         }
  1199.         return $template->$method(...$args);
  1200.     }
  1201.     /**
  1202.      * @template TSequence
  1203.      *
  1204.      * @param TSequence $seq
  1205.      *
  1206.      * @return ($seq is iterable ? TSequence : array{})
  1207.      *
  1208.      * @internal
  1209.      */
  1210.     public static function ensureTraversable($seq)
  1211.     {
  1212.         if (is_iterable($seq)) {
  1213.             return $seq;
  1214.         }
  1215.         return [];
  1216.     }
  1217.     /**
  1218.      * @internal
  1219.      */
  1220.     public static function toArray($seq$preserveKeys true)
  1221.     {
  1222.         if ($seq instanceof \Traversable) {
  1223.             return iterator_to_array($seq$preserveKeys);
  1224.         }
  1225.         if (!\is_array($seq)) {
  1226.             return $seq;
  1227.         }
  1228.         return $preserveKeys $seq array_values($seq);
  1229.     }
  1230.     /**
  1231.      * Checks if a variable is empty.
  1232.      *
  1233.      *    {# evaluates to true if the foo variable is null, false, or the empty string #}
  1234.      *    {% if foo is empty %}
  1235.      *        {# ... #}
  1236.      *    {% endif %}
  1237.      *
  1238.      * @param mixed $value A variable
  1239.      *
  1240.      * @internal
  1241.      */
  1242.     public static function testEmpty($value): bool
  1243.     {
  1244.         if ($value instanceof \Countable) {
  1245.             return === \count($value);
  1246.         }
  1247.         if ($value instanceof \Traversable) {
  1248.             return !iterator_count($value);
  1249.         }
  1250.         if ($value instanceof \Stringable) {
  1251.             return '' === (string) $value;
  1252.         }
  1253.         return '' === $value || false === $value || null === $value || [] === $value;
  1254.     }
  1255.     /**
  1256.      * Checks if a variable is a sequence.
  1257.      *
  1258.      *    {# evaluates to true if the foo variable is a sequence #}
  1259.      *    {% if foo is sequence %}
  1260.      *        {# ... #}
  1261.      *    {% endif %}
  1262.      *
  1263.      * @internal
  1264.      */
  1265.     public static function testSequence($value): bool
  1266.     {
  1267.         if ($value instanceof \ArrayObject) {
  1268.             $value $value->getArrayCopy();
  1269.         }
  1270.         if ($value instanceof \Traversable) {
  1271.             $value iterator_to_array($value);
  1272.         }
  1273.         return \is_array($value) && array_is_list($value);
  1274.     }
  1275.     /**
  1276.      * Checks if a variable is a mapping.
  1277.      *
  1278.      *    {# evaluates to true if the foo variable is a mapping #}
  1279.      *    {% if foo is mapping %}
  1280.      *        {# ... #}
  1281.      *    {% endif %}
  1282.      *
  1283.      * @internal
  1284.      */
  1285.     public static function testMapping($value): bool
  1286.     {
  1287.         if ($value instanceof \ArrayObject) {
  1288.             $value $value->getArrayCopy();
  1289.         }
  1290.         if ($value instanceof \Traversable) {
  1291.             $value iterator_to_array($value);
  1292.         }
  1293.         return (\is_array($value) && !array_is_list($value)) || \is_object($value);
  1294.     }
  1295.     /**
  1296.      * Renders a template.
  1297.      *
  1298.      * @param array                        $context
  1299.      * @param string|array|TemplateWrapper $template      The template to render or an array of templates to try consecutively
  1300.      * @param array                        $variables     The variables to pass to the template
  1301.      * @param bool                         $withContext
  1302.      * @param bool                         $ignoreMissing Whether to ignore missing templates or not
  1303.      * @param bool                         $sandboxed     Whether to sandbox the template or not
  1304.      *
  1305.      * @internal
  1306.      */
  1307.     public static function include(Environment $env$context$template$variables = [], $withContext true$ignoreMissing false$sandboxed false): string
  1308.     {
  1309.         $alreadySandboxed false;
  1310.         $sandbox null;
  1311.         if ($withContext) {
  1312.             $variables array_merge($context$variables);
  1313.         }
  1314.         if ($isSandboxed $sandboxed && $env->hasExtension(SandboxExtension::class)) {
  1315.             $sandbox $env->getExtension(SandboxExtension::class);
  1316.             if (!$alreadySandboxed $sandbox->isSandboxed()) {
  1317.                 $sandbox->enableSandbox();
  1318.             }
  1319.         }
  1320.         try {
  1321.             $loaded null;
  1322.             try {
  1323.                 $loaded $env->resolveTemplate($template);
  1324.             } catch (LoaderError $e) {
  1325.                 if (!$ignoreMissing) {
  1326.                     throw $e;
  1327.                 }
  1328.                 return '';
  1329.             }
  1330.             if ($isSandboxed) {
  1331.                 $loaded->unwrap()->checkSecurity();
  1332.             }
  1333.             return $loaded->render($variables);
  1334.         } finally {
  1335.             if ($isSandboxed && !$alreadySandboxed) {
  1336.                 $sandbox->disableSandbox();
  1337.             }
  1338.         }
  1339.     }
  1340.     /**
  1341.      * Returns a template content without rendering it.
  1342.      *
  1343.      * @param string $name          The template name
  1344.      * @param bool   $ignoreMissing Whether to ignore missing templates or not
  1345.      *
  1346.      * @internal
  1347.      */
  1348.     public static function source(Environment $env$name$ignoreMissing false): string
  1349.     {
  1350.         $loader $env->getLoader();
  1351.         try {
  1352.             return $loader->getSourceContext($name)->getCode();
  1353.         } catch (LoaderError $e) {
  1354.             if (!$ignoreMissing) {
  1355.                 throw $e;
  1356.             }
  1357.             return '';
  1358.         }
  1359.     }
  1360.     /**
  1361.      * Returns the list of cases of the enum.
  1362.      *
  1363.      * @template T of \UnitEnum
  1364.      *
  1365.      * @param class-string<T> $enum
  1366.      *
  1367.      * @return list<T>
  1368.      *
  1369.      * @internal
  1370.      */
  1371.     public static function enumCases(string $enum): array
  1372.     {
  1373.         if (!enum_exists($enum)) {
  1374.             throw new RuntimeError(\sprintf('Enum "%s" does not exist.'$enum));
  1375.         }
  1376.         return $enum::cases();
  1377.     }
  1378.     /**
  1379.      * Provides the ability to access enums by their class names.
  1380.      *
  1381.      * @template T of \UnitEnum
  1382.      *
  1383.      * @param class-string<T> $enum
  1384.      *
  1385.      * @return T
  1386.      *
  1387.      * @internal
  1388.      */
  1389.     public static function enum(string $enum): \UnitEnum
  1390.     {
  1391.         if (!enum_exists($enum)) {
  1392.             throw new RuntimeError(\sprintf('"%s" is not an enum.'$enum));
  1393.         }
  1394.         if (!$cases $enum::cases()) {
  1395.             throw new RuntimeError(\sprintf('"%s" is an empty enum.'$enum));
  1396.         }
  1397.         return $cases[0];
  1398.     }
  1399.     /**
  1400.      * Provides the ability to get constants from instances as well as class/global constants.
  1401.      *
  1402.      * @param string      $constant     The name of the constant
  1403.      * @param object|null $object       The object to get the constant from
  1404.      * @param bool        $checkDefined Whether to check if the constant is defined or not
  1405.      *
  1406.      * @return mixed Class constants can return many types like scalars, arrays, and
  1407.      *               objects depending on the PHP version (\BackedEnum, \UnitEnum, etc.)
  1408.      *               When $checkDefined is true, returns true when the constant is defined, false otherwise
  1409.      *
  1410.      * @internal
  1411.      */
  1412.     public static function constant($constant$object nullbool $checkDefined false)
  1413.     {
  1414.         if (null !== $object) {
  1415.             if ('class' === $constant) {
  1416.                 return $checkDefined true $object::class;
  1417.             }
  1418.             $constant $object::class.'::'.$constant;
  1419.         }
  1420.         if (!\defined($constant)) {
  1421.             if ($checkDefined) {
  1422.                 return false;
  1423.             }
  1424.             if ('::class' === strtolower(substr($constant, -7))) {
  1425.                 throw new RuntimeError(\sprintf('You cannot use the Twig function "constant" to access "%s". You could provide an object and call constant("class", $object) or use the class name directly as a string.'$constant));
  1426.             }
  1427.             throw new RuntimeError(\sprintf('Constant "%s" is undefined.'$constant));
  1428.         }
  1429.         return $checkDefined true \constant($constant);
  1430.     }
  1431.     /**
  1432.      * Batches item.
  1433.      *
  1434.      * @param array $items An array of items
  1435.      * @param int   $size  The size of the batch
  1436.      * @param mixed $fill  A value used to fill missing items
  1437.      *
  1438.      * @internal
  1439.      */
  1440.     public static function batch($items$size$fill null$preserveKeys true): array
  1441.     {
  1442.         if (!is_iterable($items)) {
  1443.             throw new RuntimeError(\sprintf('The "batch" filter expects a sequence or a mapping, got "%s".'get_debug_type($items)));
  1444.         }
  1445.         $size = (int) ceil($size);
  1446.         $result array_chunk(self::toArray($items$preserveKeys), $size$preserveKeys);
  1447.         if (null !== $fill && $result) {
  1448.             $last \count($result) - 1;
  1449.             if ($fillCount $size \count($result[$last])) {
  1450.                 for ($i 0$i $fillCount; ++$i) {
  1451.                     $result[$last][] = $fill;
  1452.                 }
  1453.             }
  1454.         }
  1455.         return $result;
  1456.     }
  1457.     /**
  1458.      * Returns the attribute value for a given array/object.
  1459.      *
  1460.      * @param mixed  $object            The object or array from where to get the item
  1461.      * @param mixed  $item              The item to get from the array or object
  1462.      * @param array  $arguments         An array of arguments to pass if the item is an object method
  1463.      * @param string $type              The type of attribute (@see \Twig\Template constants)
  1464.      * @param bool   $isDefinedTest     Whether this is only a defined check
  1465.      * @param bool   $ignoreStrictCheck Whether to ignore the strict attribute check or not
  1466.      * @param int    $lineno            The template line where the attribute was called
  1467.      *
  1468.      * @return mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true
  1469.      *
  1470.      * @throws RuntimeError if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false
  1471.      *
  1472.      * @internal
  1473.      */
  1474.     public static function getAttribute(Environment $envSource $source$object$item, array $arguments = [], $type Template::ANY_CALL$isDefinedTest false$ignoreStrictCheck false$sandboxed falseint $lineno = -1)
  1475.     {
  1476.         $propertyNotAllowedError null;
  1477.         // array
  1478.         if (Template::METHOD_CALL !== $type) {
  1479.             $arrayItem \is_bool($item) || \is_float($item) ? (int) $item $item;
  1480.             if ($sandboxed && $object instanceof \ArrayAccess && !\in_array($object::class, self::ARRAY_LIKE_CLASSEStrue)) {
  1481.                 try {
  1482.                     $env->getExtension(SandboxExtension::class)->checkPropertyAllowed($object$arrayItem$lineno$source);
  1483.                 } catch (SecurityNotAllowedPropertyError $propertyNotAllowedError) {
  1484.                     goto methodCheck;
  1485.                 }
  1486.             }
  1487.             if (match (true) {
  1488.                 \is_array($object) => \array_key_exists($arrayItem$object),
  1489.                 $object instanceof \ArrayAccess => $object->offsetExists($arrayItem),
  1490.                 default => false,
  1491.             }) {
  1492.                 if ($isDefinedTest) {
  1493.                     return true;
  1494.                 }
  1495.                 return $object[$arrayItem];
  1496.             }
  1497.             if (Template::ARRAY_CALL === $type || !\is_object($object)) {
  1498.                 if ($isDefinedTest) {
  1499.                     return false;
  1500.                 }
  1501.                 if ($ignoreStrictCheck || !$env->isStrictVariables()) {
  1502.                     return;
  1503.                 }
  1504.                 if ($object instanceof \ArrayAccess) {
  1505.                     $message \sprintf('Key "%s" in object with ArrayAccess of class "%s" does not exist.'$arrayItem$object::class);
  1506.                 } elseif (\is_object($object)) {
  1507.                     $message \sprintf('Impossible to access a key "%s" on an object of class "%s" that does not implement ArrayAccess interface.'$item$object::class);
  1508.                 } elseif (\is_array($object)) {
  1509.                     if (!$object) {
  1510.                         $message \sprintf('Key "%s" does not exist as the sequence/mapping is empty.'$arrayItem);
  1511.                     } else {
  1512.                         $message \sprintf('Key "%s" for sequence/mapping with keys "%s" does not exist.'$arrayItemimplode(', 'array_keys($object)));
  1513.                     }
  1514.                 } elseif (Template::ARRAY_CALL === $type) {
  1515.                     if (null === $object) {
  1516.                         $message \sprintf('Impossible to access a key ("%s") on a null variable.'$item);
  1517.                     } else {
  1518.                         $message \sprintf('Impossible to access a key ("%s") on a %s variable ("%s").'$itemget_debug_type($object), $object);
  1519.                     }
  1520.                 } elseif (null === $object) {
  1521.                     $message \sprintf('Impossible to access an attribute ("%s") on a null variable.'$item);
  1522.                 } else {
  1523.                     $message \sprintf('Impossible to access an attribute ("%s") on a %s variable ("%s").'$itemget_debug_type($object), $object);
  1524.                 }
  1525.                 throw new RuntimeError($message$lineno$source);
  1526.             }
  1527.         }
  1528.         $item = (string) $item;
  1529.         if (!\is_object($object)) {
  1530.             if ($isDefinedTest) {
  1531.                 return false;
  1532.             }
  1533.             if ($ignoreStrictCheck || !$env->isStrictVariables()) {
  1534.                 return;
  1535.             }
  1536.             if (null === $object) {
  1537.                 $message \sprintf('Impossible to invoke a method ("%s") on a null variable.'$item);
  1538.             } elseif (\is_array($object)) {
  1539.                 $message \sprintf('Impossible to invoke a method ("%s") on a sequence/mapping.'$item);
  1540.             } else {
  1541.                 $message \sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s").'$itemget_debug_type($object), $object);
  1542.             }
  1543.             throw new RuntimeError($message$lineno$source);
  1544.         }
  1545.         if ($object instanceof Template) {
  1546.             throw new RuntimeError('Accessing \Twig\Template attributes is forbidden.'$lineno$source);
  1547.         }
  1548.         // object property
  1549.         if (Template::METHOD_CALL !== $type) {
  1550.             if ($sandboxed) {
  1551.                 try {
  1552.                     $env->getExtension(SandboxExtension::class)->checkPropertyAllowed($object$item$lineno$source);
  1553.                 } catch (SecurityNotAllowedPropertyError $propertyNotAllowedError) {
  1554.                     goto methodCheck;
  1555.                 }
  1556.             }
  1557.             static $propertyCheckers = [];
  1558.             if ($object instanceof \Closure && '__invoke' === $item) {
  1559.                 return $isDefinedTest true $object();
  1560.             }
  1561.             if (isset($object->$item)
  1562.                 || ($propertyCheckers[$object::class][$item] ??= self::getPropertyChecker($object::class, $item))($object$item)
  1563.             ) {
  1564.                 if ($isDefinedTest) {
  1565.                     return true;
  1566.                 }
  1567.                 return $object->$item;
  1568.             }
  1569.             if ($object instanceof \DateTimeInterface && \in_array($item, ['date''timezone''timezone_type'], true)) {
  1570.                 if ($isDefinedTest) {
  1571.                     return true;
  1572.                 }
  1573.                 return ((array) $object)[$item];
  1574.             }
  1575.             if (\defined($object::class.'::'.$item)) {
  1576.                 if ($isDefinedTest) {
  1577.                     return true;
  1578.                 }
  1579.                 return \constant($object::class.'::'.$item);
  1580.             }
  1581.         }
  1582.         methodCheck:
  1583.         static $cache = [];
  1584.         $class $object::class;
  1585.         // object method
  1586.         // precedence: getXxx() > isXxx() > hasXxx()
  1587.         if (!isset($cache[$class])) {
  1588.             $methods get_class_methods($object);
  1589.             if ($object instanceof \Closure) {
  1590.                 $methods[] = '__invoke';
  1591.             }
  1592.             sort($methods);
  1593.             $lcMethods array_map('strtolower'$methods);
  1594.             $classCache = [];
  1595.             foreach ($methods as $i => $method) {
  1596.                 $classCache[$method] = $method;
  1597.                 $classCache[$lcName $lcMethods[$i]] = $method;
  1598.                 if ('g' === $lcName[0] && str_starts_with($lcName'get')) {
  1599.                     $name substr($method3);
  1600.                     $lcName substr($lcName3);
  1601.                 } elseif ('i' === $lcName[0] && str_starts_with($lcName'is')) {
  1602.                     $name substr($method2);
  1603.                     $lcName substr($lcName2);
  1604.                 } elseif ('h' === $lcName[0] && str_starts_with($lcName'has')) {
  1605.                     $name substr($method3);
  1606.                     $lcName substr($lcName3);
  1607.                     if (\in_array('is'.$lcName$lcMethodstrue)) {
  1608.                         continue;
  1609.                     }
  1610.                 } else {
  1611.                     continue;
  1612.                 }
  1613.                 // skip get() and is() methods (in which case, $name is empty)
  1614.                 if ($name) {
  1615.                     if (!isset($classCache[$name])) {
  1616.                         $classCache[$name] = $method;
  1617.                     }
  1618.                     if (!isset($classCache[$lcName])) {
  1619.                         $classCache[$lcName] = $method;
  1620.                     }
  1621.                 }
  1622.             }
  1623.             $cache[$class] = $classCache;
  1624.         }
  1625.         $call false;
  1626.         if (isset($cache[$class][$item])) {
  1627.             $method $cache[$class][$item];
  1628.         } elseif (isset($cache[$class][$lcItem strtolower($item)])) {
  1629.             $method $cache[$class][$lcItem];
  1630.         } elseif (isset($cache[$class]['__call'])) {
  1631.             $method $item;
  1632.             $call true;
  1633.         } else {
  1634.             if ($isDefinedTest) {
  1635.                 return false;
  1636.             }
  1637.             if ($propertyNotAllowedError) {
  1638.                 throw $propertyNotAllowedError;
  1639.             }
  1640.             if ($ignoreStrictCheck || !$env->isStrictVariables()) {
  1641.                 return;
  1642.             }
  1643.             throw new RuntimeError(\sprintf('Neither the property "%1$s" nor one of the methods "%1$s()", "get%1$s()"/"is%1$s()"/"has%1$s()" or "__call()" exist and have public access in class "%2$s".'$item$class), $lineno$source);
  1644.         }
  1645.         if ($sandboxed) {
  1646.             try {
  1647.                 $env->getExtension(SandboxExtension::class)->checkMethodAllowed($object$method$lineno$source);
  1648.             } catch (SecurityNotAllowedMethodError $e) {
  1649.                 if ($isDefinedTest) {
  1650.                     return false;
  1651.                 }
  1652.                 if ($propertyNotAllowedError) {
  1653.                     throw $propertyNotAllowedError;
  1654.                 }
  1655.                 throw $e;
  1656.             }
  1657.         }
  1658.         if ($isDefinedTest) {
  1659.             return true;
  1660.         }
  1661.         // Some objects throw exceptions when they have __call, and the method we try
  1662.         // to call is not supported. If ignoreStrictCheck is true, we should return null.
  1663.         try {
  1664.             $ret $object->$method(...$arguments);
  1665.         } catch (\BadMethodCallException $e) {
  1666.             if ($call && ($ignoreStrictCheck || !$env->isStrictVariables())) {
  1667.                 return;
  1668.             }
  1669.             throw $e;
  1670.         }
  1671.         return $ret;
  1672.     }
  1673.     /**
  1674.      * Returns the values from a single column in the input array.
  1675.      *
  1676.      * <pre>
  1677.      *  {% set items = [{ 'fruit' : 'apple'}, {'fruit' : 'orange' }] %}
  1678.      *
  1679.      *  {% set fruits = items|column('fruit') %}
  1680.      *
  1681.      *  {# fruits now contains ['apple', 'orange'] #}
  1682.      * </pre>
  1683.      *
  1684.      * @param array|\Traversable $array An array
  1685.      * @param int|string         $name  The column name
  1686.      * @param int|string|null    $index The column to use as the index/keys for the returned array
  1687.      *
  1688.      * @return array The array of values
  1689.      *
  1690.      * @internal
  1691.      */
  1692.     public static function column($array$name$index null): array
  1693.     {
  1694.         if (!is_iterable($array)) {
  1695.             throw new RuntimeError(\sprintf('The "column" filter expects a sequence or a mapping, got "%s".'get_debug_type($array)));
  1696.         }
  1697.         if ($array instanceof \Traversable) {
  1698.             $array iterator_to_array($array);
  1699.         }
  1700.         return array_column($array$name$index);
  1701.     }
  1702.     /**
  1703.      * @param \Closure $arrow
  1704.      *
  1705.      * @internal
  1706.      */
  1707.     public static function filter(Environment $env$array$arrow)
  1708.     {
  1709.         if (!is_iterable($array)) {
  1710.             throw new RuntimeError(\sprintf('The "filter" filter expects a sequence/mapping or "Traversable", got "%s".'get_debug_type($array)));
  1711.         }
  1712.         self::checkArrow($env$arrow'filter''filter');
  1713.         if (\is_array($array)) {
  1714.             return array_filter($array$arrow\ARRAY_FILTER_USE_BOTH);
  1715.         }
  1716.         // the IteratorIterator wrapping is needed as some internal PHP classes are \Traversable but do not implement \Iterator
  1717.         return new \CallbackFilterIterator(new \IteratorIterator($array), $arrow);
  1718.     }
  1719.     /**
  1720.      * @param \Closure $arrow
  1721.      *
  1722.      * @internal
  1723.      */
  1724.     public static function find(Environment $env$array$arrow)
  1725.     {
  1726.         if (!is_iterable($array)) {
  1727.             throw new RuntimeError(\sprintf('The "find" filter expects a sequence or a mapping, got "%s".'get_debug_type($array)));
  1728.         }
  1729.         self::checkArrow($env$arrow'find''filter');
  1730.         foreach ($array as $k => $v) {
  1731.             if ($arrow($v$k)) {
  1732.                 return $v;
  1733.             }
  1734.         }
  1735.         return null;
  1736.     }
  1737.     /**
  1738.      * @param \Closure $arrow
  1739.      *
  1740.      * @internal
  1741.      */
  1742.     public static function map(Environment $env$array$arrow)
  1743.     {
  1744.         if (!is_iterable($array)) {
  1745.             throw new RuntimeError(\sprintf('The "map" filter expects a sequence or a mapping, got "%s".'get_debug_type($array)));
  1746.         }
  1747.         self::checkArrow($env$arrow'map''filter');
  1748.         $r = [];
  1749.         foreach ($array as $k => $v) {
  1750.             $r[$k] = $arrow($v$k);
  1751.         }
  1752.         return $r;
  1753.     }
  1754.     /**
  1755.      * @param \Closure $arrow
  1756.      *
  1757.      * @internal
  1758.      */
  1759.     public static function reduce(Environment $env$array$arrow$initial null)
  1760.     {
  1761.         if (!is_iterable($array)) {
  1762.             throw new RuntimeError(\sprintf('The "reduce" filter expects a sequence or a mapping, got "%s".'get_debug_type($array)));
  1763.         }
  1764.         self::checkArrow($env$arrow'reduce''filter');
  1765.         $accumulator $initial;
  1766.         foreach ($array as $key => $value) {
  1767.             $accumulator $arrow($accumulator$value$key);
  1768.         }
  1769.         return $accumulator;
  1770.     }
  1771.     /**
  1772.      * @param \Closure $arrow
  1773.      *
  1774.      * @internal
  1775.      */
  1776.     public static function arraySome(Environment $env$array$arrow)
  1777.     {
  1778.         if (!is_iterable($array)) {
  1779.             throw new RuntimeError(\sprintf('The "has some" test expects a sequence or a mapping, got "%s".'get_debug_type($array)));
  1780.         }
  1781.         self::checkArrow($env$arrow'has some''operator');
  1782.         foreach ($array as $k => $v) {
  1783.             if ($arrow($v$k)) {
  1784.                 return true;
  1785.             }
  1786.         }
  1787.         return false;
  1788.     }
  1789.     /**
  1790.      * @param \Closure $arrow
  1791.      *
  1792.      * @internal
  1793.      */
  1794.     public static function arrayEvery(Environment $env$array$arrow)
  1795.     {
  1796.         if (!is_iterable($array)) {
  1797.             throw new RuntimeError(\sprintf('The "has every" test expects a sequence or a mapping, got "%s".'get_debug_type($array)));
  1798.         }
  1799.         self::checkArrow($env$arrow'has every''operator');
  1800.         foreach ($array as $k => $v) {
  1801.             if (!$arrow($v$k)) {
  1802.                 return false;
  1803.             }
  1804.         }
  1805.         return true;
  1806.     }
  1807.     /**
  1808.      * @internal
  1809.      */
  1810.     public static function checkArrow(Environment $env$arrow$thing$type)
  1811.     {
  1812.         if ($arrow instanceof \Closure) {
  1813.             return;
  1814.         }
  1815.         if ($env->hasExtension(SandboxExtension::class) && $env->getExtension(SandboxExtension::class)->isSandboxed()) {
  1816.             throw new RuntimeError(\sprintf('The callable passed to the "%s" %s must be a Closure in sandbox mode.'$thing$type));
  1817.         }
  1818.         trigger_deprecation('twig/twig''3.15''Passing a callable that is not a PHP \Closure as an argument to the "%s" %s is deprecated.'$thing$type);
  1819.     }
  1820.     /**
  1821.      * @internal to be removed in Twig 4
  1822.      */
  1823.     public static function captureOutput(iterable $body): string
  1824.     {
  1825.         $level ob_get_level();
  1826.         ob_start();
  1827.         try {
  1828.             foreach ($body as $data) {
  1829.                 echo $data;
  1830.             }
  1831.         } catch (\Throwable $e) {
  1832.             while (ob_get_level() > $level) {
  1833.                 ob_end_clean();
  1834.             }
  1835.             throw $e;
  1836.         }
  1837.         return ob_get_clean();
  1838.     }
  1839.     /**
  1840.      * @internal
  1841.      */
  1842.     public static function parseParentFunction(Parser $parserNode $fakeNode$argsint $line): AbstractExpression
  1843.     {
  1844.         if (!$blockName $parser->peekBlockStack()) {
  1845.             throw new SyntaxError('Calling the "parent" function outside of a block is forbidden.'$line$parser->getStream()->getSourceContext());
  1846.         }
  1847.         if (!$parser->hasInheritance()) {
  1848.             throw new SyntaxError('Calling the "parent" function on a template that does not call "extends" or "use" is forbidden.'$line$parser->getStream()->getSourceContext());
  1849.         }
  1850.         return new ParentExpression($blockName$line);
  1851.     }
  1852.     /**
  1853.      * @internal
  1854.      */
  1855.     public static function parseBlockFunction(Parser $parserNode $fakeNode$argsint $line): AbstractExpression
  1856.     {
  1857.         $fakeFunction = new TwigFunction('block', fn ($name$template null) => null);
  1858.         $args = (new CallableArgumentsExtractor($fakeNode$fakeFunction))->extractArguments($args);
  1859.         return new BlockReferenceExpression($args[0], $args[1] ?? null$line);
  1860.     }
  1861.     /**
  1862.      * @internal
  1863.      */
  1864.     public static function parseAttributeFunction(Parser $parserNode $fakeNode$argsint $line): AbstractExpression
  1865.     {
  1866.         $fakeFunction = new TwigFunction('attribute', fn ($variable$attribute$arguments null) => null);
  1867.         $args = (new CallableArgumentsExtractor($fakeNode$fakeFunction))->extractArguments($args);
  1868.         /*
  1869.         Deprecation to uncomment sometimes during the lifetime of the 4.x branch
  1870.         $src = $parser->getStream()->getSourceContext();
  1871.         $dep = new DeprecatedCallableInfo('twig/twig', '3.15', 'The "attribute" function is deprecated, use the "." notation instead.');
  1872.         $dep->setName('attribute');
  1873.         $dep->setType('function');
  1874.         $dep->triggerDeprecation($src->getPath() ?: $src->getName(), $line);
  1875.         */
  1876.         return new GetAttrExpression($args[0], $args[1], $args[2] ?? nullTemplate::ANY_CALL$line);
  1877.     }
  1878.     private static function getPropertyChecker(string $classstring $property): \Closure
  1879.     {
  1880.         static $classReflectors = [];
  1881.         $class $classReflectors[$class] ??= new \ReflectionClass($class);
  1882.         if (!$class->hasProperty($property)) {
  1883.             static $propertyExists;
  1884.             return $propertyExists ??= \Closure::fromCallable('property_exists');
  1885.         }
  1886.         $property $class->getProperty($property);
  1887.         if (!$property->isPublic() || $property->isStatic()) {
  1888.             static $false;
  1889.             return $false ??= static fn () => false;
  1890.         }
  1891.         return static fn ($object) => $property->isInitialized($object);
  1892.     }
  1893. }