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