1 | <?php |
||
48 | class ConditionParser implements SingletonInterface |
||
49 | { |
||
50 | const LOGICAL_AND = '&&'; |
||
51 | const LOGICAL_OR = '||'; |
||
52 | |||
53 | /** |
||
54 | * @var Result |
||
55 | */ |
||
56 | private $result; |
||
57 | |||
58 | /** |
||
59 | * @var ActivationInterface |
||
60 | */ |
||
61 | private $condition; |
||
62 | |||
63 | /** |
||
64 | * @var ConditionParserScope |
||
65 | */ |
||
66 | private $scope; |
||
67 | |||
68 | /** |
||
69 | * @return ConditionParser |
||
70 | */ |
||
71 | public static function get() |
||
75 | |||
76 | /** |
||
77 | * See class documentation. |
||
78 | * |
||
79 | * @param ActivationInterface $condition |
||
80 | * @return ConditionTree |
||
81 | */ |
||
82 | public function parse(ActivationInterface $condition) |
||
92 | |||
93 | /** |
||
94 | * @param ActivationInterface $condition |
||
95 | */ |
||
96 | private function resetParser(ActivationInterface $condition) |
||
104 | |||
105 | /** |
||
106 | * Recursive function to convert an array of condition data to a nodes tree. |
||
107 | * |
||
108 | * @return NodeInterface|null |
||
109 | */ |
||
110 | private function getNodeRecursive() |
||
150 | |||
151 | /** |
||
152 | * Will process a given token, which should be in the list of know tokens. |
||
153 | * |
||
154 | * @param string $token |
||
155 | */ |
||
156 | private function processToken($token) |
||
174 | |||
175 | /** |
||
176 | * Will process the opening parenthesis token `(`. |
||
177 | * |
||
178 | * A new scope will be created, containing the whole tokens list, which are |
||
179 | * located between the opening parenthesis and the closing one. The scope |
||
180 | * will be processed in a new scope, then the result is stored and the |
||
181 | * process can keep up. |
||
182 | */ |
||
183 | private function processTokenOpeningParenthesis() |
||
201 | |||
202 | /** |
||
203 | * Will process the closing parenthesis token `)`. |
||
204 | * |
||
205 | * This function should not be called, because the closing parenthesis |
||
206 | * should always be handled by the opening parenthesis token handler. |
||
207 | */ |
||
208 | private function processTokenClosingParenthesis() |
||
212 | |||
213 | /** |
||
214 | * Will process the logical operator tokens `&&` and `||`. |
||
215 | * |
||
216 | * Depending on the type of the operator, the process will change. |
||
217 | * |
||
218 | * @param string $operator |
||
219 | */ |
||
220 | private function processTokenLogicalOperator($operator) |
||
250 | |||
251 | /** |
||
252 | * Will process the condition token. |
||
253 | * |
||
254 | * The condition must exist in the list of items of the condition. |
||
255 | * |
||
256 | * @param string $condition |
||
257 | */ |
||
258 | private function processTokenCondition($condition) |
||
275 | |||
276 | /** |
||
277 | * @param string $nodeClassName |
||
278 | * @param array $arguments |
||
279 | * @return NodeInterface |
||
280 | */ |
||
281 | private function getNode($nodeClassName, array $arguments) |
||
288 | |||
289 | /** |
||
290 | * Will fetch a group of operations in a given array: the first item must be |
||
291 | * a parenthesis. If its closing parenthesis is found, then the inner part |
||
292 | * of the group is returned. Example: |
||
293 | * |
||
294 | * Input: (cond1 && (cond2 || cond3)) && cond4 |
||
295 | * Output: cond1 && (cond2 || cond3) |
||
296 | * |
||
297 | * @param array $expression |
||
298 | * @return array |
||
299 | */ |
||
300 | private function getGroupNode(array $expression) |
||
315 | |||
316 | /** |
||
317 | * Returns the index of the closing parenthesis that matches the opening |
||
318 | * parenthesis at index 0 of the given expression. |
||
319 | * |
||
320 | * @param array $expression |
||
321 | * @return int |
||
322 | */ |
||
323 | private function getGroupNodeClosingIndex(array $expression) |
||
344 | |||
345 | /** |
||
346 | * Will split a condition expression string in an exploded array where each |
||
347 | * entry represents an operation. |
||
348 | * |
||
349 | * @param string $condition |
||
350 | * @return array |
||
351 | */ |
||
352 | private function splitConditionExpression($condition) |
||
358 | |||
359 | /** |
||
360 | * @return ConditionParserScope |
||
361 | */ |
||
362 | private function getNewScope() |
||
366 | |||
367 | /** |
||
368 | * @param string $message |
||
369 | * @param int $code |
||
370 | */ |
||
371 | private function addError($message, $code) |
||
376 | } |
||
377 |