Completed
Push — master ( 37a545...2ac5c4 )
by Edward
04:29
created

AstBuilder   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 433
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 167
dl 0
loc 433
ccs 0
cts 166
cp 0
rs 10
c 0
b 0
f 0
wmc 29

28 Methods

Rating   Name   Duplication   Size   Complexity  
A calculateIsEqual() 0 8 1
A getInput() 0 10 2
A fetchChildrenDeep() 0 8 1
A evaluateLogicalNot() 0 7 1
A matchElementStrictly() 0 7 1
A matchAnyChild() 0 7 1
A split() 0 7 1
A evaluate() 0 8 1
A __construct() 0 3 1
A evaluateLogicalAnd() 0 8 1
A evaluateLogicalOr() 0 8 1
A fetchChildren() 0 8 1
A filter() 0 8 1
A calculateIsGreater() 0 8 1
A createFilterContext() 0 7 1
A matchPropertyStrictly() 0 7 1
A setOutput() 0 11 1
A calculateIsRegExp() 0 8 1
A matchElementSlice() 0 12 1
A aggregate() 0 8 1
A appendToArray() 0 12 1
A populateArrayElements() 0 8 1
A createArray() 0 6 1
A createScalar() 0 7 1
A populateLiteral() 0 8 1
A populateIndexList() 0 8 1
A createLiteralArray() 0 8 1
A populateNameList() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Query;
5
6
use Remorhaz\UniLex\AST\Tree;
7
use Remorhaz\UniLex\Exception as UniLexException;
8
9
final class AstBuilder implements AstBuilderInterface
10
{
11
12
    private $inputId;
13
14
    private $tree;
15
16
    public function __construct(Tree $tree)
17
    {
18
        $this->tree = $tree;
19
    }
20
21
    /**
22
     * @return int
23
     */
24
    public function getInput(): int
25
    {
26
        if (!isset($this->inputId)) {
27
            $this->inputId = $this
28
                ->tree
29
                ->createNode(AstNodeType::GET_INPUT)
30
                ->getId();
31
        }
32
33
        return $this->inputId;
34
    }
35
36
    /**
37
     * @param int $id
38
     * @param bool $isDefinite
39
     * @param bool $isPath
40
     * @throws UniLexException
41
     */
42
    public function setOutput(int $id, bool $isDefinite, bool $isPath): void
43
    {
44
        $this
45
            ->tree
46
            ->setRootNode(
47
                $this
48
                    ->tree
49
                    ->createNode(AstNodeType::SET_OUTPUT)
50
                    ->addChild($this->tree->getNode($id))
51
                    ->setAttribute('is_definite', $isDefinite)
52
                    ->setAttribute('is_path', $isPath)
53
            );
54
    }
55
56
    /**
57
     * @param int $id
58
     * @return int
59
     * @throws UniLexException
60
     */
61
    public function createFilterContext(int $id): int
62
    {
63
        return $this
64
            ->tree
65
            ->createNode(AstNodeType::CREATE_FILTER_CONTEXT)
66
            ->addChild($this->tree->getNode($id))
67
            ->getId();
68
    }
69
70
    /**
71
     * @param int $id
72
     * @return int
73
     * @throws UniLexException
74
     */
75
    public function split(int $id): int
76
    {
77
        return $this
78
            ->tree
79
            ->createNode(AstNodeType::SPLIT)
80
            ->addChild($this->tree->getNode($id))
81
            ->getId();
82
    }
83
84
    /**
85
     * @param int $sourceId
86
     * @param int $id
87
     * @return int
88
     * @throws UniLexException
89
     */
90
    public function evaluate(int $sourceId, int $id): int
91
    {
92
        return $this
93
            ->tree
94
            ->createNode(AstNodeType::EVALUATE)
95
            ->addChild($this->tree->getNode($sourceId))
96
            ->addChild($this->tree->getNode($id))
97
            ->getId();
98
    }
99
100
    /**
101
     * @param int $contextId
102
     * @param int $evaluatedId
103
     * @return int
104
     * @throws UniLexException
105
     */
106
    public function filter(int $contextId, int $evaluatedId): int
107
    {
108
        return $this
109
            ->tree
110
            ->createNode(AstNodeType::FILTER)
111
            ->addChild($this->tree->getNode($contextId))
112
            ->addChild($this->tree->getNode($evaluatedId))
113
            ->getId();
114
    }
115
116
    /**
117
     * @param int $leftEvaluatedId
118
     * @param int $rightEvaluatedId
119
     * @return int
120
     * @throws UniLexException
121
     */
122
    public function evaluateLogicalOr(int $leftEvaluatedId, int $rightEvaluatedId): int
123
    {
124
        return $this
125
            ->tree
126
            ->createNode(AstNodeType::EVALUATE_LOGICAL_OR)
127
            ->addChild($this->tree->getNode($leftEvaluatedId))
128
            ->addChild($this->tree->getNode($rightEvaluatedId))
129
            ->getId();
130
    }
131
132
    /**
133
     * @param int $leftEvaluatedId
134
     * @param int $rightEvaluatedId
135
     * @return int
136
     * @throws UniLexException
137
     */
138
    public function evaluateLogicalAnd(int $leftEvaluatedId, int $rightEvaluatedId): int
139
    {
140
        return $this
141
            ->tree
142
            ->createNode(AstNodeType::EVALUATE_LOGICAL_AND)
143
            ->addChild($this->tree->getNode($leftEvaluatedId))
144
            ->addChild($this->tree->getNode($rightEvaluatedId))
145
            ->getId();
146
    }
147
148
    /**
149
     * @param int $evaluatedId
150
     * @return int
151
     * @throws UniLexException
152
     */
153
    public function evaluateLogicalNot(int $evaluatedId): int
154
    {
155
        return $this
156
            ->tree
157
            ->createNode(AstNodeType::EVALUATE_LOGICAL_NOT)
158
            ->addChild($this->tree->getNode($evaluatedId))
159
            ->getId();
160
    }
161
162
    /**
163
     * @param int $leftId
164
     * @param int $rightId
165
     * @return int
166
     * @throws UniLexException
167
     */
168
    public function calculateIsEqual(int $leftId, int $rightId): int
169
    {
170
        return $this
171
            ->tree
172
            ->createNode(AstNodeType::CALCULATE_IS_EQUAL)
173
            ->addChild($this->tree->getNode($leftId))
174
            ->addChild($this->tree->getNode($rightId))
175
            ->getId();
176
    }
177
178
    /**
179
     * @param int $leftId
180
     * @param int $rightId
181
     * @return int
182
     * @throws UniLexException
183
     */
184
    public function calculateIsGreater(int $leftId, int $rightId): int
185
    {
186
        return $this
187
            ->tree
188
            ->createNode(AstNodeType::CALCULATE_IS_GREATER)
189
            ->addChild($this->tree->getNode($leftId))
190
            ->addChild($this->tree->getNode($rightId))
191
            ->getId();
192
    }
193
194
    /**
195
     * @param string $pattern
196
     * @param int $id
197
     * @return int
198
     * @throws UniLexException
199
     */
200
    public function calculateIsRegExp(string $pattern, int $id): int
201
    {
202
        return $this
203
            ->tree
204
            ->createNode(AstNodeType::CALCULATE_IS_REGEXP)
205
            ->addChild($this->tree->getNode($id))
206
            ->setAttribute('pattern', $pattern)
207
            ->getId();
208
    }
209
210
    /**
211
     * @param int $id
212
     * @param int $matcherId
213
     * @return int
214
     * @throws UniLexException
215
     */
216
    public function fetchChildren(int $id, int $matcherId): int
217
    {
218
        return $this
219
            ->tree
220
            ->createNode(AstNodeType::FETCH_CHILDREN)
221
            ->addChild($this->tree->getNode($id))
222
            ->addChild($this->tree->getNode($matcherId))
223
            ->getId();
224
    }
225
226
    /**
227
     * @param int $id
228
     * @param int $matcherId
229
     * @return int
230
     * @throws UniLexException
231
     */
232
    public function fetchChildrenDeep(int $id, int $matcherId): int
233
    {
234
        return $this
235
            ->tree
236
            ->createNode(AstNodeType::FETCH_CHILDREN_DEEP)
237
            ->addChild($this->tree->getNode($id))
238
            ->addChild($this->tree->getNode($matcherId))
239
            ->getId();
240
    }
241
242
    /**
243
     * @param int $sourceId
244
     * @return int
245
     * @throws UniLexException
246
     */
247
    public function matchAnyChild(int $sourceId): int
248
    {
249
        return $this
250
            ->tree
251
            ->createNode(AstNodeType::MATCH_ANY_CHILD)
252
            ->addChild($this->tree->getNode($sourceId))
253
            ->getId();
254
    }
255
256
    /**
257
     * @param int $nameListId
258
     * @return int
259
     * @throws UniLexException
260
     */
261
    public function matchPropertyStrictly(int $nameListId): int
262
    {
263
        return $this
264
            ->tree
265
            ->createNode(AstNodeType::MATCH_PROPERTY_STRICTLY)
266
            ->addChild($this->tree->getNode($nameListId))
267
            ->getId();
268
    }
269
270
    /**
271
     * @param int $indexListId
272
     * @return int
273
     * @throws UniLexException
274
     */
275
    public function matchElementStrictly(int $indexListId): int
276
    {
277
        return $this
278
            ->tree
279
            ->createNode(AstNodeType::MATCH_ELEMENT_STRICTLY)
280
            ->addChild($this->tree->getNode($indexListId))
281
            ->getId();
282
    }
283
284
    /**
285
     * @param int $sourceId
286
     * @param int|null $start
287
     * @param int|null $end
288
     * @param int|null $step
289
     * @return int
290
     * @throws UniLexException
291
     */
292
    public function matchElementSlice(int $sourceId, ?int $start, ?int $end, ?int $step): int
293
    {
294
        return $this
295
            ->tree
296
            ->createNode(AstNodeType::MATCH_ELEMENT_SLICE)
297
            ->addChild($this->tree->getNode($sourceId))
298
            ->setAttribute('hasStart', isset($start))
299
            ->setAttribute('start', $start)
300
            ->setAttribute('hasEnd', isset($end))
301
            ->setAttribute('end', $end)
302
            ->setAttribute('step', $step ?? 1)
303
            ->getId();
304
    }
305
306
    /**
307
     * @param string $name
308
     * @param int $id
309
     * @return int
310
     * @throws UniLexException
311
     */
312
    public function aggregate(string $name, int $id): int
313
    {
314
        return $this
315
            ->tree
316
            ->createNode(AstNodeType::AGGREGATE)
317
            ->addChild($this->tree->getNode($id))
318
            ->setAttribute('name', $name)
319
            ->getId();
320
    }
321
322
    /**
323
     * @param int $sourceId
324
     * @param int $valueId
325
     * @return int
326
     * @throws UniLexException
327
     */
328
    public function populateLiteral(int $sourceId, int $valueId): int
329
    {
330
        return $this
331
            ->tree
332
            ->createNode(AstNodeType::POPULATE_LITERAL)
333
            ->addChild($this->tree->getNode($sourceId))
334
            ->addChild($this->tree->getNode($valueId))
335
            ->getId();
336
    }
337
338
    /**
339
     * @param int $sourceId
340
     * @param int $arrayId
341
     * @return int
342
     * @throws UniLexException
343
     */
344
    public function populateArrayElements(int $sourceId, int $arrayId): int
345
    {
346
        return $this
347
            ->tree
348
            ->createNode(AstNodeType::POPULATE_ARRAY_ELEMENTS)
349
            ->addChild($this->tree->getNode($sourceId))
350
            ->addChild($this->tree->getNode($arrayId))
351
            ->getId();
352
    }
353
354
    /**
355
     * @param int $sourceId
356
     * @param int $elementsId
357
     * @return int
358
     * @throws UniLexException
359
     */
360
    public function createLiteralArray(int $sourceId, int $elementsId): int
361
    {
362
        return $this
363
            ->tree
364
            ->createNode(AstNodeType::CREATE_LITERAL_ARRAY)
365
            ->addChild($this->tree->getNode($sourceId))
366
            ->addChild($this->tree->getNode($elementsId))
367
            ->getId();
368
    }
369
370
    /**
371
     * @param int $sourceId
372
     * @param int ...$indexList
373
     * @return int
374
     * @throws UniLexException
375
     */
376
    public function populateIndexList(int $sourceId, int ...$indexList): int
377
    {
378
        return $this
379
            ->tree
380
            ->createNode(AstNodeType::POPULATE_INDEX_LIST)
381
            ->addChild($this->tree->getNode($sourceId))
382
            ->setAttribute('indexList', $indexList)
383
            ->getId();
384
    }
385
386
    /**
387
     * @param int $sourceId
388
     * @param string ...$nameList
389
     * @return int
390
     * @throws UniLexException
391
     */
392
    public function populateNameList(int $sourceId, string ...$nameList): int
393
    {
394
        return $this
395
            ->tree
396
            ->createNode(AstNodeType::POPULATE_NAME_LIST)
397
            ->addChild($this->tree->getNode($sourceId))
398
            ->setAttribute('nameList', $nameList)
399
            ->getId();
400
    }
401
402
    /**
403
     * @param $value
404
     * @return int
405
     * @throws UniLexException
406
     */
407
    public function createScalar($value): int
408
    {
409
        return $this
410
            ->tree
411
            ->createNode(AstNodeType::CREATE_SCALAR)
412
            ->setAttribute('value', $value)
413
            ->getId();
414
    }
415
416
    public function createArray(): int
417
    {
418
        return $this
419
            ->tree
420
            ->createNode(AstNodeType::CREATE_ARRAY)
421
            ->getId();
422
    }
423
424
    /**
425
     * @param int $arrayId
426
     * @param int $valueId
427
     * @return int
428
     * @throws UniLexException
429
     */
430
    public function appendToArray(int $arrayId, int $valueId): int
431
    {
432
        $appendNode = $this
433
            ->tree
434
            ->createNode(AstNodeType::APPEND_TO_ARRAY)
435
            ->addChild($this->tree->getNode($valueId));
436
437
        return $this
438
            ->tree
439
            ->getNode($arrayId)
440
            ->addChild($appendNode)
441
            ->getId();
442
    }
443
}
444