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

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