Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of Twig. |
| 5 | * |
| 6 | * (c) Fabien Potencier |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Twig; |
| 13 | |
| 14 | use Twig\Node\Expression\FilterExpression; |
| 15 | use Twig\Node\Node; |
| 16 | |
| 17 | /** |
| 18 | * Represents a template filter. |
| 19 | * |
| 20 | * @author Fabien Potencier <fabien@symfony.com> |
| 21 | * |
| 22 | * @see https://twig.symfony.com/doc/templates.html#filters |
| 23 | */ |
| 24 | final class TwigFilter |
| 25 | { |
| 26 | private $name; |
| 27 | private $callable; |
| 28 | private $options; |
| 29 | private $arguments = []; |
| 30 | |
| 31 | /** |
| 32 | * @param callable|null $callable A callable implementing the filter. If null, you need to overwrite the "node_class" option to customize compilation. |
| 33 | */ |
| 34 | public function __construct(string $name, $callable = null, array $options = []) |
| 35 | { |
| 36 | $this->name = $name; |
| 37 | $this->callable = $callable; |
| 38 | $this->options = array_merge([ |
| 39 | 'needs_environment' => false, |
| 40 | 'needs_context' => false, |
| 41 | 'is_variadic' => false, |
| 42 | 'is_safe' => null, |
| 43 | 'is_safe_callback' => null, |
| 44 | 'pre_escape' => null, |
| 45 | 'preserves_safety' => null, |
| 46 | 'node_class' => FilterExpression::class, |
| 47 | 'deprecated' => false, |
| 48 | 'alternative' => null, |
| 49 | ], $options); |
| 50 | } |
| 51 | |
| 52 | public function getName(): string |
| 53 | { |
| 54 | return $this->name; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the callable to execute for this filter. |
| 59 | * |
| 60 | * @return callable|null |
| 61 | */ |
| 62 | public function getCallable() |
| 63 | { |
| 64 | return $this->callable; |
| 65 | } |
| 66 | |
| 67 | public function getNodeClass(): string |
| 68 | { |
| 69 | return $this->options['node_class']; |
| 70 | } |
| 71 | |
| 72 | public function setArguments(array $arguments): void |
| 73 | { |
| 74 | $this->arguments = $arguments; |
| 75 | } |
| 76 | |
| 77 | public function getArguments(): array |
| 78 | { |
| 79 | return $this->arguments; |
| 80 | } |
| 81 | |
| 82 | public function needsEnvironment(): bool |
| 83 | { |
| 84 | return $this->options['needs_environment']; |
| 85 | } |
| 86 | |
| 87 | public function needsContext(): bool |
| 88 | { |
| 89 | return $this->options['needs_context']; |
| 90 | } |
| 91 | |
| 92 | public function getSafe(Node $filterArgs): ?array |
| 93 | { |
| 94 | if (null !== $this->options['is_safe']) { |
| 95 | return $this->options['is_safe']; |
| 96 | } |
| 97 | |
| 98 | if (null !== $this->options['is_safe_callback']) { |
| 99 | return $this->options['is_safe_callback']($filterArgs); |
| 100 | } |
| 101 | |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | public function getPreservesSafety(): ?array |
| 106 | { |
| 107 | return $this->options['preserves_safety']; |
| 108 | } |
| 109 | |
| 110 | public function getPreEscape(): ?string |
| 111 | { |
| 112 | return $this->options['pre_escape']; |
| 113 | } |
| 114 | |
| 115 | public function isVariadic(): bool |
| 116 | { |
| 117 | return $this->options['is_variadic']; |
| 118 | } |
| 119 | |
| 120 | public function isDeprecated(): bool |
| 121 | { |
| 122 | return (bool) $this->options['deprecated']; |
| 123 | } |
| 124 | |
| 125 | public function getDeprecatedVersion(): string |
| 126 | { |
| 127 | return \is_bool($this->options['deprecated']) ? '' : $this->options['deprecated']; |
| 128 | } |
| 129 | |
| 130 | public function getAlternative(): ?string |
| 131 | { |
| 132 | return $this->options['alternative']; |
| 133 | } |
| 134 | } |