blob: 4c18632f5599dac0088ab82e71c7b3b75cfc44d4 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001<?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
24{
25 private $name;
26 private $callable;
27 private $options;
28 private $arguments = [];
29
30 /**
31 * @param callable|null $callable A callable implementing the test. If null, you need to overwrite the "node_class" option to customize compilation.
32 */
33 public function __construct(string $name, $callable = null, array $options = [])
34 {
35 $this->name = $name;
36 $this->callable = $callable;
37 $this->options = array_merge([
38 'is_variadic' => false,
39 'node_class' => TestExpression::class,
40 'deprecated' => false,
41 'alternative' => null,
42 'one_mandatory_argument' => false,
43 ], $options);
44 }
45
46 public function getName(): string
47 {
48 return $this->name;
49 }
50
51 /**
52 * Returns the callable to execute for this test.
53 *
54 * @return callable|null
55 */
56 public function getCallable()
57 {
58 return $this->callable;
59 }
60
61 public function getNodeClass(): string
62 {
63 return $this->options['node_class'];
64 }
65
66 public function setArguments(array $arguments): void
67 {
68 $this->arguments = $arguments;
69 }
70
71 public function getArguments(): array
72 {
73 return $this->arguments;
74 }
75
76 public function isVariadic(): bool
77 {
78 return (bool) $this->options['is_variadic'];
79 }
80
81 public function isDeprecated(): bool
82 {
83 return (bool) $this->options['deprecated'];
84 }
85
86 public function getDeprecatedVersion(): string
87 {
88 return \is_bool($this->options['deprecated']) ? '' : $this->options['deprecated'];
89 }
90
91 public function getAlternative(): ?string
92 {
93 return $this->options['alternative'];
94 }
95
96 public function hasOneMandatoryArgument(): bool
97 {
98 return (bool) $this->options['one_mandatory_argument'];
99 }
100}