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

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