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