1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MyWheels\TwigSpreadsheetBundle\Twig\TokenParser; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class BaseTokenParser. |
7
|
|
|
*/ |
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 = []) |
31
|
|
|
{ |
32
|
8 |
|
$this->attributes = $attributes; |
33
|
8 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param \Twig_Token $token |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
3 |
|
public function configureParameters(\Twig_Token $token): array |
41
|
|
|
{ |
42
|
3 |
|
return []; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
50 |
|
public function getAttributes(): array |
49
|
|
|
{ |
50
|
50 |
|
return $this->attributes; |
51
|
|
|
} |
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
|
50 |
|
public function hasBody(): bool |
67
|
|
|
{ |
68
|
50 |
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
* |
74
|
|
|
* @throws \Exception |
75
|
|
|
* @throws \InvalidArgumentException |
76
|
|
|
*/ |
77
|
50 |
|
public function parse(\Twig_Token $token) |
78
|
|
|
{ |
79
|
|
|
// parse parameters |
80
|
50 |
|
$nodes = $this->parseParameters($this->configureParameters($token)); |
81
|
|
|
|
82
|
|
|
// parse body |
83
|
50 |
|
if ($this->hasBody()) { |
84
|
50 |
|
$nodes['body'] = $this->parseBody(); |
85
|
|
|
} |
86
|
|
|
|
87
|
50 |
|
return $this->createNode($nodes, $token->getLine()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $parameterConfiguration |
92
|
|
|
* |
93
|
|
|
* @throws \Exception |
94
|
|
|
* @throws \InvalidArgumentException |
95
|
|
|
* @throws \Twig_Error_Syntax |
96
|
|
|
* |
97
|
|
|
* @return \Twig_Node_Expression[] |
98
|
|
|
*/ |
99
|
50 |
|
private function parseParameters(array $parameterConfiguration = []): array |
100
|
|
|
{ |
101
|
|
|
// parse expressions |
102
|
50 |
|
$expressions = []; |
103
|
50 |
|
while (!$this->parser->getStream()->test(\Twig_Token::BLOCK_END_TYPE)) { |
104
|
48 |
|
$expressions[] = $this->parser->getExpressionParser()->parseExpression(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// end of expressions |
108
|
50 |
|
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); |
109
|
|
|
|
110
|
|
|
// map expressions to parameters |
111
|
50 |
|
$parameters = []; |
112
|
50 |
|
foreach ($parameterConfiguration as $parameterName => $parameterOptions) { |
113
|
|
|
// try mapping expression |
114
|
50 |
|
$expression = reset($expressions); |
115
|
50 |
|
if ($expression !== false) { |
116
|
48 |
|
switch ($parameterOptions['type']) { |
117
|
48 |
|
case self::PARAMETER_TYPE_ARRAY: |
118
|
|
|
// check if expression is valid array |
119
|
14 |
|
$valid = $expression instanceof \Twig_Node_Expression_Array; |
120
|
14 |
|
break; |
121
|
46 |
|
case self::PARAMETER_TYPE_VALUE: |
122
|
|
|
// check if expression is valid value |
123
|
46 |
|
$valid = !($expression instanceof \Twig_Node_Expression_Array); |
124
|
46 |
|
break; |
125
|
|
|
default: |
126
|
|
|
throw new \InvalidArgumentException('Invalid parameter type'); |
127
|
|
|
} |
128
|
|
|
|
129
|
48 |
|
if ($valid) { |
130
|
|
|
// set expression as parameter and remove it from expressions list |
131
|
48 |
|
$parameters[$parameterName] = array_shift($expressions); |
132
|
48 |
|
continue; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// set default as parameter otherwise or throw exception if default is false |
137
|
48 |
|
if ($parameterOptions['default'] === false) { |
138
|
|
|
throw new \Twig_Error_Syntax('A required parameter is missing'); |
|
|
|
|
139
|
|
|
} |
140
|
48 |
|
$parameters[$parameterName] = $parameterOptions['default']; |
141
|
|
|
} |
142
|
|
|
|
143
|
50 |
|
if (\count($expressions) > 0) { |
144
|
|
|
throw new \Twig_Error_Syntax('Too many parameters'); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
50 |
|
return $parameters; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return \Twig_Node |
152
|
|
|
* @throws \Twig_Error_Syntax |
153
|
|
|
*/ |
154
|
|
|
private function parseBody(): \Twig_Node |
155
|
|
|
{ |
156
|
|
|
// parse till matching end tag is found |
157
|
|
|
$body = $this->parser->subparse(function (\Twig_Token $token) { return $token->test('end'.$this->getTag()); }, true); |
158
|
50 |
|
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); |
159
|
50 |
|
return $body; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.