1 | <?php |
||
17 | class Parser |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | public $variables = []; |
||
23 | |||
24 | /** |
||
25 | * @var null|mixed[] |
||
26 | */ |
||
27 | protected $values = null; |
||
28 | |||
29 | /** |
||
30 | * @var null|string |
||
31 | */ |
||
32 | protected $operator = null; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $output = ''; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $operatorRequired = false; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | protected $incompleteCondition = false; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $openParenthesis = 0; |
||
53 | |||
54 | /** |
||
55 | * @var int |
||
56 | */ |
||
57 | protected $closedParenthesis = 0; |
||
58 | |||
59 | /** |
||
60 | * @var TokenizerInterface |
||
61 | */ |
||
62 | protected $tokenizer; |
||
63 | |||
64 | /** |
||
65 | * @var Expressions\Factory |
||
66 | */ |
||
67 | protected $expressionFactory; |
||
68 | |||
69 | protected $userDefinedFunctions = []; |
||
70 | |||
71 | 230 | public function __construct(TokenizerInterface $tokenizer, Expressions\Factory $expressionFactory) |
|
76 | |||
77 | /** |
||
78 | * @throws Exceptions\ParserException |
||
79 | */ |
||
80 | 224 | public function parse(string $rule) : string |
|
119 | |||
120 | 224 | public function assignVariables(array $variables) |
|
121 | { |
||
122 | 224 | $this->variables = $variables; |
|
123 | 224 | } |
|
124 | |||
125 | /** |
||
126 | * @param Tokens\BaseToken $token |
||
127 | * @throws Exceptions\ParserException |
||
128 | */ |
||
129 | 182 | protected function assignVariableValueFromToken(BaseToken $token) |
|
130 | { |
||
131 | 182 | if ($this->operatorRequired) { |
|
132 | 2 | throw new Exceptions\ParserException(sprintf( |
|
133 | 2 | 'Missing operator at position %d on line %d', |
|
134 | 2 | $token->getPosition(), |
|
135 | 2 | $token->getLine() |
|
136 | )); |
||
137 | } |
||
138 | |||
139 | 182 | $this->operatorRequired = !$this->operatorRequired; |
|
140 | 182 | $this->incompleteCondition = false; |
|
141 | |||
142 | 182 | if (!isset($this->values)) { |
|
143 | 182 | $this->values = [$token->getValue()]; |
|
144 | } else { |
||
145 | 178 | $this->values[] = $token->getValue(); |
|
146 | } |
||
147 | 182 | } |
|
148 | |||
149 | /** |
||
150 | * @throws Exceptions\ParserException |
||
151 | */ |
||
152 | 22 | protected function assignParentheses(BaseToken $token) |
|
153 | { |
||
154 | 22 | $tokenValue = $token->getValue(); |
|
155 | |||
156 | 22 | if ($tokenValue === '(') { |
|
157 | 20 | if ($this->operatorRequired) { |
|
158 | 4 | throw new Exceptions\ParserException(sprintf( |
|
159 | 4 | 'Unexpected token "(" at position %d on line %d', |
|
160 | 4 | $token->getPosition(), |
|
161 | 4 | $token->getLine() |
|
162 | )); |
||
163 | } |
||
164 | |||
165 | 20 | $this->openParenthesis++; |
|
166 | } else { |
||
167 | 20 | if ($this->openParenthesis < 1) { |
|
168 | 2 | throw new Exceptions\ParserException(sprintf( |
|
169 | 2 | 'Missing opening parenthesis at position %d on line %d', |
|
170 | 2 | $token->getPosition(), |
|
171 | 2 | $token->getLine() |
|
172 | )); |
||
173 | } |
||
174 | |||
175 | 18 | $this->closedParenthesis++; |
|
176 | } |
||
177 | |||
178 | 20 | $this->output .= $tokenValue; |
|
179 | 20 | } |
|
180 | |||
181 | /** |
||
182 | * @throws Exceptions\ParserException |
||
183 | */ |
||
184 | 36 | protected function assignLogicalToken(BaseToken $token) |
|
185 | { |
||
186 | 36 | if (!$this->operatorRequired) { |
|
187 | 2 | throw new Exceptions\ParserException(sprintf( |
|
188 | 2 | 'Unexpected "%s" at position %d on line %d', |
|
189 | 2 | $token->getOriginalValue(), |
|
190 | 2 | $token->getPosition(), |
|
191 | 2 | $token->getLine() |
|
192 | )); |
||
193 | } |
||
194 | |||
195 | 36 | $this->output .= $token->getValue(); |
|
196 | 36 | $this->incompleteCondition = true; |
|
197 | 36 | $this->operatorRequired = false; |
|
198 | 36 | } |
|
199 | |||
200 | /** |
||
201 | * @throws Exceptions\ParserException |
||
202 | */ |
||
203 | 182 | protected function assignOperator(BaseToken $token) |
|
204 | { |
||
205 | 182 | if (isset($this->operator)) { |
|
206 | 2 | throw new Exceptions\ParserException(sprintf( |
|
207 | 2 | 'Unexpected "%s" at position %d on line %d', |
|
208 | 2 | $token->getOriginalValue(), |
|
209 | 2 | $token->getPosition(), |
|
210 | 2 | $token->getLine() |
|
211 | )); |
||
212 | 182 | } elseif (!isset($this->values)) { |
|
213 | 2 | throw new Exceptions\ParserException(sprintf( |
|
214 | 2 | 'Incomplete expression for token "%s" at position %d on line %d', |
|
215 | 2 | $token->getOriginalValue(), |
|
216 | 2 | $token->getPosition(), |
|
217 | 2 | $token->getLine() |
|
218 | )); |
||
219 | } |
||
220 | |||
221 | 180 | $this->operator = $token->getValue(); |
|
222 | 180 | $this->operatorRequired = false; |
|
223 | 180 | } |
|
224 | |||
225 | /** |
||
226 | * @throws Exceptions\ExpressionFactoryException |
||
227 | */ |
||
228 | 182 | protected function parseExpression() |
|
229 | { |
||
230 | 182 | if (!isset($this->operator) || count($this->values) <> 2) { |
|
231 | 182 | return; |
|
232 | } |
||
233 | |||
234 | 178 | $this->operatorRequired = true; |
|
235 | 178 | $expression = $this->expressionFactory->createFromOperator($this->operator); |
|
236 | 178 | $this->output .= (int) $expression->evaluate($this->values[0], $this->values[1]); |
|
237 | |||
238 | 176 | unset($this->operator, $this->values); |
|
239 | 176 | } |
|
240 | |||
241 | /** |
||
242 | * @throws Exceptions\ParserException |
||
243 | */ |
||
244 | 164 | protected function assertSyntaxSeemsOkay() |
|
260 | |||
261 | public function registerFunctionClass(string $className) |
||
262 | { |
||
263 | /** @var CallableUserFunction $class */ |
||
264 | $class = new $className(); |
||
265 | |||
266 | if (!$class instanceof CallableUserFunction) { |
||
280 | |||
281 | 2 | public function registerFunction(string $name, Closure $callback) |
|
285 | |||
286 | 2 | public function registerToken(string $token, string $regex, int $priority = 10) |
|
290 | |||
291 | /** |
||
292 | * @param string $name |
||
293 | * @return Closure|null |
||
294 | */ |
||
295 | 4 | public function getFunction(string $name) |
|
301 | } |
||
302 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: