1 | <?php |
||
49 | class ConditionParser implements SingletonInterface |
||
50 | { |
||
51 | use SelfInstantiateTrait; |
||
52 | |||
53 | const LOGICAL_AND = '&&'; |
||
54 | const LOGICAL_OR = '||'; |
||
55 | |||
56 | const ERROR_CODE_INVALID_CLOSING_PARENTHESIS = 1457969163; |
||
57 | const ERROR_CODE_CLOSING_PARENTHESIS_NOT_FOUND = 1457544856; |
||
58 | const ERROR_CODE_CONDITION_NOT_FOUND = 1457628378; |
||
59 | const ERROR_CODE_LOGICAL_OPERATOR_PRECEDED = 1457544986; |
||
60 | const ERROR_CODE_LOGICAL_OPERATOR_FOLLOWED = 1457545071; |
||
61 | |||
62 | /** |
||
63 | * @var Result |
||
64 | */ |
||
65 | protected $result; |
||
66 | |||
67 | /** |
||
68 | * @var ActivationInterface |
||
69 | */ |
||
70 | protected $condition; |
||
71 | |||
72 | /** |
||
73 | * @var ConditionParserScope |
||
74 | */ |
||
75 | protected $scope; |
||
76 | |||
77 | /** |
||
78 | * See class documentation. |
||
79 | * |
||
80 | * @param ActivationInterface $condition |
||
81 | * @return ConditionTree |
||
82 | */ |
||
83 | public function parse(ActivationInterface $condition) |
||
104 | |||
105 | /** |
||
106 | * @param ActivationInterface $condition |
||
107 | */ |
||
108 | protected function resetParser(ActivationInterface $condition) |
||
116 | |||
117 | /** |
||
118 | * Recursive function to convert an array of condition data to a nodes tree. |
||
119 | * |
||
120 | * @return NodeInterface|null |
||
121 | */ |
||
122 | protected function getNodeRecursive() |
||
137 | |||
138 | /** |
||
139 | * Will process a given token, which should be in the list of know tokens. |
||
140 | * |
||
141 | * @param string $token |
||
142 | * @return $this |
||
143 | */ |
||
144 | protected function processToken($token) |
||
164 | |||
165 | /** |
||
166 | * Will process the opening parenthesis token `(`. |
||
167 | * |
||
168 | * A new scope will be created, containing the whole tokens list, which are |
||
169 | * located between the opening parenthesis and the closing one. The scope |
||
170 | * will be processed in a new scope, then the result is stored and the |
||
171 | * process can keep up. |
||
172 | */ |
||
173 | protected function processTokenOpeningParenthesis() |
||
189 | |||
190 | /** |
||
191 | * Will process the closing parenthesis token `)`. |
||
192 | * |
||
193 | * This function should not be called, because the closing parenthesis |
||
194 | * should always be handled by the opening parenthesis token handler. |
||
195 | */ |
||
196 | protected function processTokenClosingParenthesis() |
||
200 | |||
201 | /** |
||
202 | * Will process the logical operator tokens `&&` and `||`. |
||
203 | * |
||
204 | * Depending on the type of the operator, the process will change. |
||
205 | * |
||
206 | * @param string $operator |
||
207 | */ |
||
208 | protected function processTokenLogicalOperator($operator) |
||
235 | |||
236 | /** |
||
237 | * Will process the condition token. |
||
238 | * |
||
239 | * The condition must exist in the list of items of the condition. |
||
240 | * |
||
241 | * @param string $condition |
||
242 | */ |
||
243 | protected 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 | protected function processLogicalAndNode() |
||
276 | |||
277 | /** |
||
278 | * Will check if a last logical operator node is remaining. |
||
279 | * |
||
280 | * @return $this |
||
281 | */ |
||
282 | protected function processLastLogicalOperatorNode() |
||
293 | |||
294 | /** |
||
295 | * Will fetch a group of operations in a given array: the first item must be |
||
296 | * a parenthesis. If its closing parenthesis is found, then the inner part |
||
297 | * of the group is returned. Example: |
||
298 | * |
||
299 | * Input: (cond1 && (cond2 || cond3)) && cond4 |
||
300 | * Output: cond1 && (cond2 || cond3) |
||
301 | * |
||
302 | * @param array $expression |
||
303 | * @return array |
||
304 | */ |
||
305 | protected function getGroupNode(array $expression) |
||
316 | |||
317 | /** |
||
318 | * Returns the index of the closing parenthesis that matches the opening |
||
319 | * parenthesis at index 0 of the given expression. |
||
320 | * |
||
321 | * @param array $expression |
||
322 | * @return int |
||
323 | */ |
||
324 | protected 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 | protected function splitConditionExpression($condition) |
||
358 | |||
359 | /** |
||
360 | * @return ConditionParserScope |
||
361 | */ |
||
362 | protected function getNewScope() |
||
366 | |||
367 | /** |
||
368 | * @param string $message |
||
369 | * @param int $code |
||
370 | * @throws ConditionParserException |
||
371 | */ |
||
372 | protected function addError($message, $code) |
||
376 | } |
||
377 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: