vendor/twig/twig/src/TwigFilter.php line 31

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;
  11. use Twig\Node\Expression\FilterExpression;
  12. use Twig\Node\Node;
  13. /**
  14. * Represents a template filter.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. *
  18. * @see https://twig.symfony.com/doc/templates.html#filters
  19. */
  20. final class TwigFilter extends AbstractTwigCallable
  21. {
  22. /**
  23. * @param callable|array{class-string, string}|null $callable A callable implementing the filter. If null, you need to overwrite the "node_class" option to customize compilation.
  24. */
  25. public function __construct(string $name, $callable = null, array $options = [])
  26. {
  27. parent::__construct($name, $callable, $options);
  28. $this->options = array_merge([
  29. 'is_safe' => null,
  30. 'is_safe_callback' => null,
  31. 'pre_escape' => null,
  32. 'preserves_safety' => null,
  33. 'node_class' => FilterExpression::class,
  34. ], $this->options);
  35. }
  36. public function getType(): string
  37. {
  38. return 'filter';
  39. }
  40. public function getSafe(Node $filterArgs): ?array
  41. {
  42. if (null !== $this->options['is_safe']) {
  43. return $this->options['is_safe'];
  44. }
  45. if (null !== $this->options['is_safe_callback']) {
  46. return $this->options['is_safe_callback']($filterArgs);
  47. }
  48. return [];
  49. }
  50. public function getPreservesSafety(): array
  51. {
  52. return $this->options['preserves_safety'] ?? [];
  53. }
  54. public function getPreEscape(): ?string
  55. {
  56. return $this->options['pre_escape'];
  57. }
  58. public function getMinimalNumberOfRequiredArguments(): int
  59. {
  60. return parent::getMinimalNumberOfRequiredArguments() + 1;
  61. }
  62. }