1 | <?php |
||
22 | class Interpreter |
||
23 | { |
||
24 | /** |
||
25 | * @var UqlExtensionContainer |
||
26 | */ |
||
27 | private $extensionContainer; |
||
28 | |||
29 | /** |
||
30 | * @var DataSourceInterface |
||
31 | */ |
||
32 | private $dataSource; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $dataSourceElements; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | private $caseSensitive; |
||
43 | |||
44 | /** |
||
45 | * @var FilterConditionFactory |
||
46 | */ |
||
47 | private $filterConditionFactory; |
||
48 | |||
49 | /** |
||
50 | * Constructor needs the columns descriptor to figure out appropriate filtering methods |
||
51 | * and translate identifiers. |
||
52 | * |
||
53 | * @param UqlExtensionContainer $extensionContainer |
||
54 | * @param DataSourceInterface $dataSource |
||
55 | * @param FilterConditionFactory $filterConditionFactory |
||
56 | * @param bool $caseSensitive |
||
57 | */ |
||
58 | public function __construct( |
||
80 | |||
81 | /** |
||
82 | * Helper method: matches filtering operators to valid UQL operators |
||
83 | * in order to do Filter to UQL transformations |
||
84 | * |
||
85 | * @param $method |
||
86 | * |
||
87 | * @throws Exception\UQLInterpreterException |
||
88 | * @return |
||
89 | */ |
||
90 | public static function methodToUQLOperator($method) |
||
119 | |||
120 | /** |
||
121 | * Generate the filter objects corresponding to a UQL string. |
||
122 | * |
||
123 | * @param string $uql |
||
124 | * |
||
125 | * @return Filter |
||
126 | */ |
||
127 | public function interpret($uql) |
||
138 | |||
139 | /** |
||
140 | * Transforms a subtree of the AST into a concrete filter definition. |
||
141 | * This function recursively builds all sub-trees. |
||
142 | * |
||
143 | * @param ASTGroup|ASTAssertion|mixed $astSubtree |
||
144 | * |
||
145 | * TODO: make private |
||
146 | * |
||
147 | * @return Filter|FilterCondition |
||
148 | * @throws \Exception |
||
149 | */ |
||
150 | public function buildFilter($astSubtree) |
||
151 | { |
||
152 | if ($astSubtree instanceof ASTGroup) { |
||
153 | return $this->buildFilterFromASTGroup($astSubtree); |
||
154 | } |
||
155 | |||
156 | if ($astSubtree instanceof ASTAssertion) { |
||
157 | $filterCondition = $this->buildFilterConditionFromASTAssertion($astSubtree); |
||
158 | // Single filter. Wrap into dummy filter collection for consistency. |
||
159 | $filter = new Filter(); |
||
160 | $filter[] = $filterCondition; |
||
161 | return $filter; |
||
162 | } |
||
163 | |||
164 | throw new UQLInterpreterException('Unexpected Abstract Syntax Tree element'); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Translate <operator> tokens into Filter Methods. |
||
169 | * |
||
170 | * @param string $token |
||
171 | * @param FieldInterface $dataSourceElement |
||
172 | * |
||
173 | * @throws Exception\UQLInterpreterException |
||
174 | * @return mixed |
||
175 | */ |
||
176 | public function translateOperator($token, FieldInterface $dataSourceElement) |
||
235 | |||
236 | /** |
||
237 | * Translate Lexer <logic> tokens into Filter Condition Types. |
||
238 | * |
||
239 | * @param $token |
||
240 | * |
||
241 | * @return string |
||
242 | * @throws \Exception |
||
243 | */ |
||
244 | private function translateLogic($token) |
||
258 | |||
259 | /** |
||
260 | * Trim and clean up the value to be set in the filter. |
||
261 | * |
||
262 | * @param $value |
||
263 | * |
||
264 | * @return mixed |
||
265 | */ |
||
266 | private function parseValue($value) |
||
274 | |||
275 | /** |
||
276 | * @param ASTFunctionCall $functionCall |
||
277 | * |
||
278 | * @return mixed |
||
279 | * @throws Exception\UQLInterpreterException |
||
280 | */ |
||
281 | private function callFunction(ASTFunctionCall $functionCall) |
||
289 | |||
290 | /** |
||
291 | * @param array $elements |
||
292 | * |
||
293 | * @return array |
||
294 | */ |
||
295 | private function parseArray($elements) |
||
304 | |||
305 | /** |
||
306 | * @param string $identifier |
||
307 | * |
||
308 | * @return Field |
||
309 | * @throws UQLInterpreterException |
||
310 | */ |
||
311 | private function matchDataSourceElement($identifier) |
||
323 | |||
324 | /** |
||
325 | * @param ASTAssertion $astSubtree |
||
326 | * |
||
327 | * @throws UQLInterpreterException |
||
328 | * |
||
329 | * @return array|mixed |
||
330 | */ |
||
331 | private function getValue(ASTAssertion $astSubtree) |
||
347 | |||
348 | /** |
||
349 | * @param ASTAssertion $astSubtree |
||
350 | * |
||
351 | * @throws UQLInterpreterException |
||
352 | * |
||
353 | * @return FilterCondition |
||
354 | */ |
||
355 | private function buildFilterConditionFromASTAssertion(ASTAssertion $astSubtree) |
||
363 | |||
364 | /** |
||
365 | * @param ASTGroup $astSubtree |
||
366 | * |
||
367 | * @throws UQLInterpreterException |
||
368 | * @throws \Exception |
||
369 | * |
||
370 | * @return Filter |
||
371 | */ |
||
372 | private function buildFilterFromASTGroup(ASTGroup $astSubtree) |
||
388 | } |
||
389 |