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) |
||
221 | |||
222 | /** |
||
223 | * Will process the condition token. |
||
224 | * |
||
225 | * The condition must exist in the list of items of the condition. |
||
226 | * |
||
227 | * @param string $condition |
||
228 | */ |
||
229 | private function processTokenCondition($condition) |
||
240 | |||
241 | /** |
||
242 | * Will check if a "logical and" node should be created, depending on which |
||
243 | * tokens were processed before. |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | private function processLogicalAndNode() |
||
262 | |||
263 | /** |
||
264 | * Will check if a last logical operator node is remaining. |
||
265 | * |
||
266 | * @return $this |
||
267 | */ |
||
268 | private function processLastLogicalOperatorNode() |
||
279 | |||
280 | /** |
||
281 | * Will fetch a group of operations in a given array: the first item must be |
||
282 | * a parenthesis. If its closing parenthesis is found, then the inner part |
||
283 | * of the group is returned. Example: |
||
284 | * |
||
285 | * Input: (cond1 && (cond2 || cond3)) && cond4 |
||
286 | * Output: cond1 && (cond2 || cond3) |
||
287 | * |
||
288 | * @param array $expression |
||
289 | * @return array |
||
290 | */ |
||
291 | private function getGroupNode(array $expression) |
||
306 | |||
307 | /** |
||
308 | * Returns the index of the closing parenthesis that matches the opening |
||
309 | * parenthesis at index 0 of the given expression. |
||
310 | * |
||
311 | * @param array $expression |
||
312 | * @return int |
||
313 | */ |
||
314 | private function getGroupNodeClosingIndex(array $expression) |
||
335 | |||
336 | /** |
||
337 | * Will split a condition expression string in an exploded array where each |
||
338 | * entry represents an operation. |
||
339 | * |
||
340 | * @param string $condition |
||
341 | * @return array |
||
342 | */ |
||
343 | private function splitConditionExpression($condition) |
||
349 | |||
350 | /** |
||
351 | * @return ConditionParserScope |
||
352 | */ |
||
353 | private function getNewScope() |
||
357 | |||
358 | /** |
||
359 | * @param string $message |
||
360 | * @param int $code |
||
361 | */ |
||
362 | private function addError($message, $code) |
||
367 | } |
||
368 |
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: