1 | <?php |
||
47 | class ConditionParser implements SingletonInterface |
||
48 | { |
||
49 | const LOGICAL_AND = '&&'; |
||
50 | const LOGICAL_OR = '||'; |
||
51 | |||
52 | /** |
||
53 | * @var Result |
||
54 | */ |
||
55 | private $result; |
||
56 | |||
57 | /** |
||
58 | * @var ActivationInterface |
||
59 | */ |
||
60 | private $condition; |
||
61 | |||
62 | /** |
||
63 | * @var ConditionParserScope |
||
64 | */ |
||
65 | private $scope; |
||
66 | |||
67 | /** |
||
68 | * @return ConditionParser |
||
69 | */ |
||
70 | public static function get() |
||
74 | |||
75 | /** |
||
76 | * See class documentation. |
||
77 | * |
||
78 | * @param ActivationInterface $condition |
||
79 | * @return ConditionTree |
||
80 | */ |
||
81 | public function parse(ActivationInterface $condition) |
||
91 | |||
92 | /** |
||
93 | * @param ActivationInterface $condition |
||
94 | */ |
||
95 | private function resetParser(ActivationInterface $condition) |
||
103 | |||
104 | /** |
||
105 | * Recursive function to convert an array of condition data to a nodes tree. |
||
106 | * |
||
107 | * @return NodeInterface|null |
||
108 | */ |
||
109 | private function getNodeRecursive() |
||
128 | |||
129 | /** |
||
130 | * Will process a given token, which should be in the list of know tokens. |
||
131 | * |
||
132 | * @param string $token |
||
133 | * @return $this |
||
134 | */ |
||
135 | private function processToken($token) |
||
155 | |||
156 | /** |
||
157 | * Will process the opening parenthesis token `(`. |
||
158 | * |
||
159 | * A new scope will be created, containing the whole tokens list, which are |
||
160 | * located between the opening parenthesis and the closing one. The scope |
||
161 | * will be processed in a new scope, then the result is stored and the |
||
162 | * process can keep up. |
||
163 | */ |
||
164 | private function processTokenOpeningParenthesis() |
||
180 | |||
181 | /** |
||
182 | * Will process the closing parenthesis token `)`. |
||
183 | * |
||
184 | * This function should not be called, because the closing parenthesis |
||
185 | * should always be handled by the opening parenthesis token handler. |
||
186 | */ |
||
187 | private function processTokenClosingParenthesis() |
||
191 | |||
192 | /** |
||
193 | * Will process the logical operator tokens `&&` and `||`. |
||
194 | * |
||
195 | * Depending on the type of the operator, the process will change. |
||
196 | * |
||
197 | * @param string $operator |
||
198 | */ |
||
199 | private function processTokenLogicalOperator($operator) |
||
229 | |||
230 | /** |
||
231 | * Will process the condition token. |
||
232 | * |
||
233 | * The condition must exist in the list of items of the condition. |
||
234 | * |
||
235 | * @param string $condition |
||
236 | */ |
||
237 | private function processTokenCondition($condition) |
||
254 | |||
255 | /** |
||
256 | * Will check if a "logical and" node should be created, depending on which |
||
257 | * tokens were processed before. |
||
258 | * |
||
259 | * @return $this |
||
260 | */ |
||
261 | private function processLogicalAndNode() |
||
279 | |||
280 | /** |
||
281 | * Will check if a last logical operator node is remaining. |
||
282 | * |
||
283 | * @return $this |
||
284 | */ |
||
285 | private function processLastLogicalOperatorNode() |
||
299 | |||
300 | /** |
||
301 | * @param string $nodeClassName |
||
302 | * @param array $arguments |
||
303 | * @return NodeInterface |
||
304 | */ |
||
305 | private function getNode($nodeClassName, array $arguments) |
||
312 | |||
313 | /** |
||
314 | * Will fetch a group of operations in a given array: the first item must be |
||
315 | * a parenthesis. If its closing parenthesis is found, then the inner part |
||
316 | * of the group is returned. Example: |
||
317 | * |
||
318 | * Input: (cond1 && (cond2 || cond3)) && cond4 |
||
319 | * Output: cond1 && (cond2 || cond3) |
||
320 | * |
||
321 | * @param array $expression |
||
322 | * @return array |
||
323 | */ |
||
324 | private function getGroupNode(array $expression) |
||
339 | |||
340 | /** |
||
341 | * Returns the index of the closing parenthesis that matches the opening |
||
342 | * parenthesis at index 0 of the given expression. |
||
343 | * |
||
344 | * @param array $expression |
||
345 | * @return int |
||
346 | */ |
||
347 | private function getGroupNodeClosingIndex(array $expression) |
||
368 | |||
369 | /** |
||
370 | * Will split a condition expression string in an exploded array where each |
||
371 | * entry represents an operation. |
||
372 | * |
||
373 | * @param string $condition |
||
374 | * @return array |
||
375 | */ |
||
376 | private function splitConditionExpression($condition) |
||
382 | |||
383 | /** |
||
384 | * @return ConditionParserScope |
||
385 | */ |
||
386 | private function getNewScope() |
||
390 | |||
391 | /** |
||
392 | * @param string $message |
||
393 | * @param int $code |
||
394 | */ |
||
395 | private function addError($message, $code) |
||
400 | } |
||
401 |