1 | <?php |
||
26 | class Interpreter |
||
27 | { |
||
28 | /** |
||
29 | * @var UqlExtensionContainer |
||
30 | */ |
||
31 | private $extensionContainer; |
||
32 | |||
33 | /** |
||
34 | * @var DataSourceInterface |
||
35 | */ |
||
36 | private $dataSource; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $dataSourceElements; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $caseSensitive; |
||
47 | |||
48 | /** |
||
49 | * @var FilterConditionFactory |
||
50 | */ |
||
51 | private $filterConditionFactory; |
||
52 | |||
53 | /** |
||
54 | * @var ContextFactory |
||
55 | */ |
||
56 | private $contextFactory; |
||
57 | |||
58 | /** |
||
59 | * Constructor needs the columns descriptor to figure out appropriate filtering methods |
||
60 | * and translate identifiers. |
||
61 | * |
||
62 | * @param UqlExtensionContainer $extensionContainer |
||
63 | * @param DataSourceInterface $dataSource |
||
64 | * @param FilterConditionFactory $filterConditionFactory |
||
65 | * @param ContextFactory $contextFactory |
||
66 | * @param bool $caseSensitive |
||
67 | */ |
||
68 | public function __construct( |
||
69 | UqlExtensionContainer $extensionContainer, |
||
70 | DataSourceInterface $dataSource, |
||
71 | FilterConditionFactory $filterConditionFactory, |
||
72 | ContextFactory $contextFactory, |
||
73 | $caseSensitive = true |
||
74 | ) { |
||
75 | $this->extensionContainer = $extensionContainer; |
||
76 | $this->dataSource = $dataSource; |
||
77 | $this->filterConditionFactory = $filterConditionFactory; |
||
78 | $this->contextFactory = $contextFactory; |
||
79 | $this->caseSensitive = $caseSensitive; |
||
80 | |||
81 | // Cache an array of data sources (name => object pairs) for reference during the interpretation |
||
82 | $this->dataSourceElements = array_combine( |
||
83 | array_map( |
||
84 | function (FieldInterface $element) use ($caseSensitive) { |
||
85 | return $caseSensitive ? $element->getUniqueName() : strtolower($element->getUniqueName()); |
||
86 | }, |
||
87 | $this->dataSource->getFields() |
||
88 | ), |
||
89 | $this->dataSource->getFields() |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Generate the filter objects corresponding to a UQL string. |
||
95 | * |
||
96 | * @param string $uql |
||
97 | * |
||
98 | * @return Filter |
||
99 | */ |
||
100 | public function interpret($uql) |
||
101 | { |
||
102 | if (empty(trim($uql))) { |
||
103 | return new Filter(); |
||
104 | } |
||
105 | |||
106 | $parser = new Parser(); |
||
107 | $AST = $parser->parse($uql); |
||
108 | |||
109 | return $this->buildFilter($AST); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Helper method: matches filtering operators to valid UQL operators |
||
114 | * in order to do Filter to UQL transformations |
||
115 | * |
||
116 | * @param string $method |
||
117 | * |
||
118 | * @throws UQLInterpreterException |
||
119 | * @return string |
||
120 | */ |
||
121 | public static function methodToUQLOperator($method) |
||
150 | |||
151 | /** |
||
152 | * Transforms a subtree of the AST into a concrete filter definition. |
||
153 | * This function recursively builds all sub-trees. |
||
154 | * |
||
155 | * @param ASTGroup|ASTAssertion|mixed $astSubtree |
||
156 | * |
||
157 | * TODO: This looks like it should not be public (it is only used in tests). |
||
158 | * We could move it and it's dependencies to its own class so that it can be tested |
||
159 | * |
||
160 | * @return Filter |
||
161 | * @throws \Exception |
||
162 | */ |
||
163 | public function buildFilter($astSubtree) |
||
164 | { |
||
165 | if ($astSubtree instanceof ASTGroup) { |
||
166 | return $this->buildFilterFromASTGroup($astSubtree); |
||
167 | } |
||
168 | |||
169 | if ($astSubtree instanceof ASTAssertion) { |
||
170 | $filterCondition = $this->buildFilterConditionFromASTAssertion($astSubtree); |
||
171 | // Single filter. Wrap into dummy filter collection for consistency. |
||
172 | $filter = new Filter(); |
||
173 | $filter[] = $filterCondition; |
||
174 | |||
175 | return $filter; |
||
176 | } |
||
177 | |||
178 | throw new UQLInterpreterException('Unexpected Abstract Syntax Tree element'); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Translate <operator> tokens into Filter Methods. |
||
183 | * |
||
184 | * @param string $token |
||
185 | * @param FieldInterface $dataSourceElement |
||
186 | * |
||
187 | * @throws UQLInterpreterException |
||
188 | * @return mixed |
||
189 | */ |
||
190 | public function translateOperator($token, FieldInterface $dataSourceElement) |
||
250 | |||
251 | /** |
||
252 | * Translate Lexer <logic> tokens into Filter Condition Types. |
||
253 | * |
||
254 | * @param $token |
||
255 | * |
||
256 | * @return string |
||
257 | * @throws \Exception |
||
258 | */ |
||
259 | private function translateLogic($token) |
||
273 | |||
274 | /** |
||
275 | * Trim and clean up the value to be set in the filter. |
||
276 | * |
||
277 | * @param mixed $value |
||
278 | * |
||
279 | * @return mixed |
||
280 | */ |
||
281 | private function parseValue($value) |
||
289 | |||
290 | /** |
||
291 | * @param ASTFunctionCall $functionCall |
||
292 | * |
||
293 | * @return mixed |
||
294 | * @throws FunctionNotFoundException |
||
295 | * @throws UQLInterpreterException |
||
296 | */ |
||
297 | private function callFunction(ASTFunctionCall $functionCall) |
||
298 | { |
||
299 | $functionName = $functionCall->getFunctionName(); |
||
300 | $function = $this->extensionContainer->getFunction($functionName); |
||
301 | $arguments = $this->getFunctionArguments($functionCall, $function); |
||
302 | |||
303 | try { |
||
304 | return $function->call($arguments); |
||
305 | } catch (\Exception $e) { |
||
306 | throw new UQLInterpreterException("The execution of function '$functionName' failed. Please check the arguments are valid. (" . $e->getMessage() . ")"); |
||
307 | } |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @param array $elements |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | private function parseArray($elements) |
||
324 | |||
325 | /** |
||
326 | * @param string $identifier |
||
327 | * |
||
328 | * @return Field |
||
329 | * @throws UQLInterpreterException |
||
330 | */ |
||
331 | private function matchDataSourceElement($identifier) |
||
343 | |||
344 | /** |
||
345 | * @param ASTAssertion $astSubtree |
||
346 | * |
||
347 | * @throws UQLInterpreterException |
||
348 | * |
||
349 | * @return array|mixed |
||
350 | */ |
||
351 | private function getValue(ASTAssertion $astSubtree) |
||
367 | |||
368 | /** |
||
369 | * @param ASTAssertion $astSubtree |
||
370 | * |
||
371 | * @throws UQLInterpreterException |
||
372 | * |
||
373 | * @return FilterCondition |
||
374 | */ |
||
375 | private function buildFilterConditionFromASTAssertion(ASTAssertion $astSubtree) |
||
383 | |||
384 | /** |
||
385 | * @param ASTGroup $astSubtree |
||
386 | * |
||
387 | * @throws UQLInterpreterException |
||
388 | * @throws \Exception |
||
389 | * |
||
390 | * @return Filter |
||
391 | */ |
||
392 | private function buildFilterFromASTGroup(ASTGroup $astSubtree) |
||
408 | |||
409 | /** |
||
410 | * @param ASTFunctionCall $functionCall |
||
411 | * @param UqlFunctionInterface $function |
||
412 | * |
||
413 | * @return array |
||
414 | */ |
||
415 | private function getFunctionArguments(ASTFunctionCall $functionCall, $function) |
||
426 | } |
||
427 |