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

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. * @final since Twig 2.4.0
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. *
  20. * @see https://twig.symfony.com/doc/templates.html#filters
  21. */
  22. class TwigFilter
  23. {
  24. private $name;
  25. private $callable;
  26. private $options;
  27. private $arguments = [];
  28. /**
  29. * Creates a template filter.
  30. *
  31. * @param string $name Name of this filter
  32. * @param callable|null $callable A callable implementing the filter. If null, you need to overwrite the "node_class" option to customize compilation.
  33. * @param array $options Options array
  34. */
  35. public function __construct(string $name, $callable = null, array $options = [])
  36. {
  37. if (__CLASS__ !== static::class) {
  38. @trigger_error('Overriding '.__CLASS__.' is deprecated since Twig 2.4.0 and the class will be final in 3.0.', \E_USER_DEPRECATED);
  39. }
  40. $this->name = $name;
  41. $this->callable = $callable;
  42. $this->options = array_merge([
  43. 'needs_environment' => false,
  44. 'needs_context' => false,
  45. 'is_variadic' => false,
  46. 'is_safe' => null,
  47. 'is_safe_callback' => null,
  48. 'pre_escape' => null,
  49. 'preserves_safety' => null,
  50. 'node_class' => FilterExpression::class,
  51. 'deprecated' => false,
  52. 'alternative' => null,
  53. ], $options);
  54. }
  55. public function getName()
  56. {
  57. return $this->name;
  58. }
  59. /**
  60. * Returns the callable to execute for this filter.
  61. *
  62. * @return callable|null
  63. */
  64. public function getCallable()
  65. {
  66. return $this->callable;
  67. }
  68. public function getNodeClass()
  69. {
  70. return $this->options['node_class'];
  71. }
  72. public function setArguments($arguments)
  73. {
  74. $this->arguments = $arguments;
  75. }
  76. public function getArguments()
  77. {
  78. return $this->arguments;
  79. }
  80. public function needsEnvironment()
  81. {
  82. return $this->options['needs_environment'];
  83. }
  84. public function needsContext()
  85. {
  86. return $this->options['needs_context'];
  87. }
  88. public function getSafe(Node $filterArgs)
  89. {
  90. if (null !== $this->options['is_safe']) {
  91. return $this->options['is_safe'];
  92. }
  93. if (null !== $this->options['is_safe_callback']) {
  94. return $this->options['is_safe_callback']($filterArgs);
  95. }
  96. }
  97. public function getPreservesSafety()
  98. {
  99. return $this->options['preserves_safety'];
  100. }
  101. public function getPreEscape()
  102. {
  103. return $this->options['pre_escape'];
  104. }
  105. public function isVariadic()
  106. {
  107. return $this->options['is_variadic'];
  108. }
  109. public function isDeprecated()
  110. {
  111. return (bool) $this->options['deprecated'];
  112. }
  113. public function getDeprecatedVersion()
  114. {
  115. return $this->options['deprecated'];
  116. }
  117. public function getAlternative()
  118. {
  119. return $this->options['alternative'];
  120. }
  121. }
  122. // For Twig 1.x compatibility
  123. class_alias('Twig\TwigFilter', 'Twig_SimpleFilter', false);
  124. class_alias('Twig\TwigFilter', 'Twig_Filter');
  125. // Ensure that the aliased name is loaded to keep BC for classes implementing the typehint with the old aliased name.
  126. class_exists('Twig\Node\Node');