1 | <?php |
||
8 | abstract class BaseTokenParser extends \Twig_TokenParser |
||
9 | { |
||
10 | /** |
||
11 | * @var int |
||
12 | */ |
||
13 | const PARAMETER_TYPE_ARRAY = 0; |
||
14 | |||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | const PARAMETER_TYPE_VALUE = 1; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $attributes; |
||
24 | |||
25 | /** |
||
26 | * BaseTokenParser constructor. |
||
27 | * |
||
28 | * @param array $attributes optional attributes for the corresponding node |
||
29 | */ |
||
30 | 8 | public function __construct(array $attributes = []) |
|
34 | |||
35 | /** |
||
36 | * @param \Twig_Token $token |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | 3 | public function configureParameters(\Twig_Token $token): array |
|
44 | |||
45 | /** |
||
46 | * @return array |
||
47 | */ |
||
48 | 48 | public function getAttributes(): array |
|
52 | |||
53 | /** |
||
54 | * Create a concrete node. |
||
55 | * |
||
56 | * @param array $nodes |
||
57 | * @param int $lineNo |
||
58 | * |
||
59 | * @return \Twig_Node |
||
60 | */ |
||
61 | abstract public function createNode(array $nodes = [], int $lineNo = 0): \Twig_Node; |
||
62 | |||
63 | /** |
||
64 | * @return bool |
||
65 | */ |
||
66 | 48 | public function hasBody(): bool |
|
67 | { |
||
68 | 48 | return true; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | * |
||
74 | * @throws \InvalidArgumentException |
||
75 | */ |
||
76 | 48 | public function parse(\Twig_Token $token) |
|
77 | { |
||
78 | // parse parameters |
||
79 | 48 | $nodes = $this->parseParameters($this->configureParameters($token)); |
|
80 | |||
81 | // parse body |
||
82 | 48 | if ($this->hasBody()) { |
|
83 | 48 | $nodes['body'] = $this->parseBody(); |
|
84 | } |
||
85 | |||
86 | 48 | return $this->createNode($nodes, $token->getLine()); |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param array $parameterConfiguration |
||
91 | * |
||
92 | * @throws \InvalidArgumentException |
||
93 | * @throws \Twig_Error_Syntax |
||
94 | * |
||
95 | * @return \Twig_Node_Expression[] |
||
96 | */ |
||
97 | 48 | private function parseParameters(array $parameterConfiguration = []): array |
|
147 | |||
148 | /** |
||
149 | * @return \Twig_Node |
||
150 | */ |
||
151 | private function parseBody(): \Twig_Node |
||
165 | } |
||
166 |