1 | <?php |
||
48 | class ConditionParser implements SingletonInterface |
||
49 | { |
||
50 | use FacadeInstanceTrait; |
||
51 | |||
52 | const LOGICAL_AND = '&&'; |
||
53 | const LOGICAL_OR = '||'; |
||
54 | |||
55 | /** |
||
56 | * @var Result |
||
57 | */ |
||
58 | private $result; |
||
59 | |||
60 | /** |
||
61 | * @var ActivationInterface |
||
62 | */ |
||
63 | private $condition; |
||
64 | |||
65 | /** |
||
66 | * @var ConditionParserScope |
||
67 | */ |
||
68 | private $scope; |
||
69 | |||
70 | /** |
||
71 | * See class documentation. |
||
72 | * |
||
73 | * @param ActivationInterface $condition |
||
74 | * @return ConditionTree |
||
75 | */ |
||
76 | public function parse(ActivationInterface $condition) |
||
86 | |||
87 | /** |
||
88 | * @param ActivationInterface $condition |
||
89 | */ |
||
90 | private function resetParser(ActivationInterface $condition) |
||
98 | |||
99 | /** |
||
100 | * Recursive function to convert an array of condition data to a nodes tree. |
||
101 | * |
||
102 | * @return NodeInterface|null |
||
103 | */ |
||
104 | private function getNodeRecursive() |
||
123 | |||
124 | /** |
||
125 | * Will process a given token, which should be in the list of know tokens. |
||
126 | * |
||
127 | * @param string $token |
||
128 | * @return $this |
||
129 | */ |
||
130 | private function processToken($token) |
||
150 | |||
151 | /** |
||
152 | * Will process the opening parenthesis token `(`. |
||
153 | * |
||
154 | * A new scope will be created, containing the whole tokens list, which are |
||
155 | * located between the opening parenthesis and the closing one. The scope |
||
156 | * will be processed in a new scope, then the result is stored and the |
||
157 | * process can keep up. |
||
158 | */ |
||
159 | private function processTokenOpeningParenthesis() |
||
175 | |||
176 | /** |
||
177 | * Will process the closing parenthesis token `)`. |
||
178 | * |
||
179 | * This function should not be called, because the closing parenthesis |
||
180 | * should always be handled by the opening parenthesis token handler. |
||
181 | */ |
||
182 | private function processTokenClosingParenthesis() |
||
186 | |||
187 | /** |
||
188 | * Will process the logical operator tokens `&&` and `||`. |
||
189 | * |
||
190 | * Depending on the type of the operator, the process will change. |
||
191 | * |
||
192 | * @param string $operator |
||
193 | */ |
||
194 | private function processTokenLogicalOperator($operator) |
||
224 | |||
225 | /** |
||
226 | * Will process the condition token. |
||
227 | * |
||
228 | * The condition must exist in the list of items of the condition. |
||
229 | * |
||
230 | * @param string $condition |
||
231 | */ |
||
232 | private function processTokenCondition($condition) |
||
249 | |||
250 | /** |
||
251 | * Will check if a "logical and" node should be created, depending on which |
||
252 | * tokens were processed before. |
||
253 | * |
||
254 | * @return $this |
||
255 | */ |
||
256 | private function processLogicalAndNode() |
||
274 | |||
275 | /** |
||
276 | * Will check if a last logical operator node is remaining. |
||
277 | * |
||
278 | * @return $this |
||
279 | */ |
||
280 | private function processLastLogicalOperatorNode() |
||
294 | |||
295 | /** |
||
296 | * @param string $nodeClassName |
||
297 | * @param array $arguments |
||
298 | * @return NodeInterface |
||
299 | */ |
||
300 | private function getNode($nodeClassName, array $arguments) |
||
307 | |||
308 | /** |
||
309 | * Will fetch a group of operations in a given array: the first item must be |
||
310 | * a parenthesis. If its closing parenthesis is found, then the inner part |
||
311 | * of the group is returned. Example: |
||
312 | * |
||
313 | * Input: (cond1 && (cond2 || cond3)) && cond4 |
||
314 | * Output: cond1 && (cond2 || cond3) |
||
315 | * |
||
316 | * @param array $expression |
||
317 | * @return array |
||
318 | */ |
||
319 | private function getGroupNode(array $expression) |
||
334 | |||
335 | /** |
||
336 | * Returns the index of the closing parenthesis that matches the opening |
||
337 | * parenthesis at index 0 of the given expression. |
||
338 | * |
||
339 | * @param array $expression |
||
340 | * @return int |
||
341 | */ |
||
342 | private function getGroupNodeClosingIndex(array $expression) |
||
363 | |||
364 | /** |
||
365 | * Will split a condition expression string in an exploded array where each |
||
366 | * entry represents an operation. |
||
367 | * |
||
368 | * @param string $condition |
||
369 | * @return array |
||
370 | */ |
||
371 | private function splitConditionExpression($condition) |
||
377 | |||
378 | /** |
||
379 | * @return ConditionParserScope |
||
380 | */ |
||
381 | private function getNewScope() |
||
385 | |||
386 | /** |
||
387 | * @param string $message |
||
388 | * @param int $code |
||
389 | */ |
||
390 | private function addError($message, $code) |
||
395 | } |
||
396 |