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 * @author Fabien Potencier <fabien@symfony.com>
20 *
21 * @see https://twig.symfony.com/doc/templates.html#test-operator
22 */
23final class TwigTest extends AbstractTwigCallable
24{
25    /**
26     * @param callable|array{class-string, string}|null $callable A callable implementing the test. If null, you need to overwrite the "node_class" option to customize compilation.
27     */
28    public function __construct(string $name, $callable = null, array $options = [])
29    {
30        parent::__construct($name, $callable, $options);
31
32        $this->options = array_merge([
33            'node_class' => TestExpression::class,
34            'one_mandatory_argument' => false,
35        ], $this->options);
36    }
37
38    public function getType(): string
39    {
40        return 'test';
41    }
42
43    public function needsCharset(): bool
44    {
45        return false;
46    }
47
48    public function needsEnvironment(): bool
49    {
50        return false;
51    }
52
53    public function needsContext(): bool
54    {
55        return false;
56    }
57
58    public function hasOneMandatoryArgument(): bool
59    {
60        return (bool) $this->options['one_mandatory_argument'];
61    }
62
63    public function getMinimalNumberOfRequiredArguments(): int
64    {
65        return parent::getMinimalNumberOfRequiredArguments() + 1;
66    }
67}
68