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\Node;
15
16/**
17 * Represents a template filter.
18 *
19 * @final
20 *
21 * @author Fabien Potencier <fabien@symfony.com>
22 */
23class TwigFilter
24{
25    protected $name;
26    protected $callable;
27    protected $options;
28    protected $arguments = [];
29
30    public function __construct($name, $callable, array $options = [])
31    {
32        $this->name = $name;
33        $this->callable = $callable;
34        $this->options = array_merge([
35            'needs_environment' => false,
36            'needs_context' => false,
37            'is_variadic' => false,
38            'is_safe' => null,
39            'is_safe_callback' => null,
40            'pre_escape' => null,
41            'preserves_safety' => null,
42            'node_class' => '\Twig\Node\Expression\FilterExpression',
43            'deprecated' => false,
44            'alternative' => null,
45        ], $options);
46    }
47
48    public function getName()
49    {
50        return $this->name;
51    }
52
53    public function getCallable()
54    {
55        return $this->callable;
56    }
57
58    public function getNodeClass()
59    {
60        return $this->options['node_class'];
61    }
62
63    public function setArguments($arguments)
64    {
65        $this->arguments = $arguments;
66    }
67
68    public function getArguments()
69    {
70        return $this->arguments;
71    }
72
73    public function needsEnvironment()
74    {
75        return $this->options['needs_environment'];
76    }
77
78    public function needsContext()
79    {
80        return $this->options['needs_context'];
81    }
82
83    public function getSafe(Node $filterArgs)
84    {
85        if (null !== $this->options['is_safe']) {
86            return $this->options['is_safe'];
87        }
88
89        if (null !== $this->options['is_safe_callback']) {
90            return \call_user_func($this->options['is_safe_callback'], $filterArgs);
91        }
92    }
93
94    public function getPreservesSafety()
95    {
96        return $this->options['preserves_safety'];
97    }
98
99    public function getPreEscape()
100    {
101        return $this->options['pre_escape'];
102    }
103
104    public function isVariadic()
105    {
106        return $this->options['is_variadic'];
107    }
108
109    public function isDeprecated()
110    {
111        return (bool) $this->options['deprecated'];
112    }
113
114    public function getDeprecatedVersion()
115    {
116        return $this->options['deprecated'];
117    }
118
119    public function getAlternative()
120    {
121        return $this->options['alternative'];
122    }
123}
124
125class_alias('Twig\TwigFilter', 'Twig_SimpleFilter');
126
127// Ensure that the aliased name is loaded to keep BC for classes implementing the typehint with the old aliased name.
128class_exists('Twig\Node\Node');
129