Passed
Push — master ( bae1fe...e6b663 )
by Edward
04:30
created

AstBuilder::createArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
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 splitFilterContext(int $id): int
76
    {
77
        return $this
78
            ->tree
79
            ->createNode(AstNodeType::SPLIT_FILTER_CONTEXT)
80
            ->addChild($this->tree->getNode($id))
81
            ->getId();
82
    }
83
84
    /**
85
     * @param int $evaluatedId
86
     * @param int $contextId
87
     * @return int
88
     * @throws UniLexException
89
     */
90
    public function joinFilterResults(int $evaluatedId, int $contextId): int
91
    {
92
        return $this
93
            ->tree
94
            ->createNode(AstNodeType::JOIN_FILTER_RESULTS)
95
            ->addChild($this->tree->getNode($evaluatedId))
96
            ->addChild($this->tree->getNode($contextId))
97
            ->getId();
98
    }
99
100
    /**
101
     * @param int $sourceId
102
     * @param int $id
103
     * @return int
104
     * @throws UniLexException
105
     */
106
    public function evaluate(int $sourceId, int $id): int
107
    {
108
        return $this
109
            ->tree
110
            ->createNode(AstNodeType::EVALUATE)
111
            ->addChild($this->tree->getNode($sourceId))
112
            ->addChild($this->tree->getNode($id))
113
            ->getId();
114
    }
115
116
    /**
117
     * @param int $contextId
118
     * @param int $evaluatedId
119
     * @return int
120
     * @throws UniLexException
121
     */
122
    public function filter(int $contextId, int $evaluatedId): int
123
    {
124
        return $this
125
            ->tree
126
            ->createNode(AstNodeType::FILTER)
127
            ->addChild($this->tree->getNode($contextId))
128
            ->addChild($this->tree->getNode($evaluatedId))
129
            ->getId();
130
    }
131
132
    /**
133
     * @param int $leftEvaluatedId
134
     * @param int $rightEvaluatedId
135
     * @return int
136
     * @throws UniLexException
137
     */
138
    public function evaluateLogicalOr(int $leftEvaluatedId, int $rightEvaluatedId): int
139
    {
140
        return $this
141
            ->tree
142
            ->createNode(AstNodeType::EVALUATE_LOGICAL_OR)
143
            ->addChild($this->tree->getNode($leftEvaluatedId))
144
            ->addChild($this->tree->getNode($rightEvaluatedId))
145
            ->getId();
146
    }
147
148
    /**
149
     * @param int $leftEvaluatedId
150
     * @param int $rightEvaluatedId
151
     * @return int
152
     * @throws UniLexException
153
     */
154
    public function evaluateLogicalAnd(int $leftEvaluatedId, int $rightEvaluatedId): int
155
    {
156
        return $this
157
            ->tree
158
            ->createNode(AstNodeType::EVALUATE_LOGICAL_AND)
159
            ->addChild($this->tree->getNode($leftEvaluatedId))
160
            ->addChild($this->tree->getNode($rightEvaluatedId))
161
            ->getId();
162
    }
163
164
    /**
165
     * @param int $evaluatedId
166
     * @return int
167
     * @throws UniLexException
168
     */
169
    public function evaluateLogicalNot(int $evaluatedId): int
170
    {
171
        return $this
172
            ->tree
173
            ->createNode(AstNodeType::EVALUATE_LOGICAL_NOT)
174
            ->addChild($this->tree->getNode($evaluatedId))
175
            ->getId();
176
    }
177
178
    /**
179
     * @param int $leftId
180
     * @param int $rightId
181
     * @return int
182
     * @throws UniLexException
183
     */
184
    public function calculateIsEqual(int $leftId, int $rightId): int
185
    {
186
        return $this
187
            ->tree
188
            ->createNode(AstNodeType::CALCULATE_IS_EQUAL)
189
            ->addChild($this->tree->getNode($leftId))
190
            ->addChild($this->tree->getNode($rightId))
191
            ->getId();
192
    }
193
194
    /**
195
     * @param int $leftId
196
     * @param int $rightId
197
     * @return int
198
     * @throws UniLexException
199
     */
200
    public function calculateIsGreater(int $leftId, int $rightId): int
201
    {
202
        return $this
203
            ->tree
204
            ->createNode(AstNodeType::CALCULATE_IS_GREATER)
205
            ->addChild($this->tree->getNode($leftId))
206
            ->addChild($this->tree->getNode($rightId))
207
            ->getId();
208
    }
209
210
    /**
211
     * @param string $pattern
212
     * @param int $id
213
     * @return int
214
     * @throws UniLexException
215
     */
216
    public function calculateIsRegExp(string $pattern, int $id): int
217
    {
218
        return $this
219
            ->tree
220
            ->createNode(AstNodeType::CALCULATE_IS_REGEXP)
221
            ->addChild($this->tree->getNode($id))
222
            ->setAttribute('pattern', $pattern)
223
            ->getId();
224
    }
225
226
    /**
227
     * @param int $id
228
     * @param int $matcherId
229
     * @return int
230
     * @throws UniLexException
231
     */
232
    public function fetchChildren(int $id, int $matcherId): int
233
    {
234
        return $this
235
            ->tree
236
            ->createNode(AstNodeType::FETCH_CHILDREN)
237
            ->addChild($this->tree->getNode($id))
238
            ->addChild($this->tree->getNode($matcherId))
239
            ->getId();
240
    }
241
242
    /**
243
     * @param int $id
244
     * @param int $matcherId
245
     * @return int
246
     * @throws UniLexException
247
     */
248
    public function fetchChildrenDeep(int $id, int $matcherId): int
249
    {
250
        return $this
251
            ->tree
252
            ->createNode(AstNodeType::FETCH_CHILDREN_DEEP)
253
            ->addChild($this->tree->getNode($id))
254
            ->addChild($this->tree->getNode($matcherId))
255
            ->getId();
256
    }
257
258
    /**
259
     * @param int $sourceId
260
     * @return int
261
     * @throws UniLexException
262
     */
263
    public function matchAnyChild(int $sourceId): int
264
    {
265
        return $this
266
            ->tree
267
            ->createNode(AstNodeType::MATCH_ANY_CHILD)
268
            ->addChild($this->tree->getNode($sourceId))
269
            ->getId();
270
    }
271
272
    /**
273
     * @param int $nameListId
274
     * @return int
275
     * @throws UniLexException
276
     */
277
    public function matchPropertyStrictly(int $nameListId): int
278
    {
279
        return $this
280
            ->tree
281
            ->createNode(AstNodeType::MATCH_PROPERTY_STRICTLY)
282
            ->addChild($this->tree->getNode($nameListId))
283
            ->getId();
284
    }
285
286
    /**
287
     * @param int $indexListId
288
     * @return int
289
     * @throws UniLexException
290
     */
291
    public function matchElementStrictly(int $indexListId): int
292
    {
293
        return $this
294
            ->tree
295
            ->createNode(AstNodeType::MATCH_ELEMENT_STRICTLY)
296
            ->addChild($this->tree->getNode($indexListId))
297
            ->getId();
298
    }
299
300
    /**
301
     * @param int $sourceId
302
     * @param int|null $start
303
     * @param int|null $end
304
     * @param int|null $step
305
     * @return int
306
     * @throws UniLexException
307
     */
308
    public function matchElementSlice(int $sourceId, ?int $start, ?int $end, ?int $step): int
309
    {
310
        return $this
311
            ->tree
312
            ->createNode(AstNodeType::MATCH_ELEMENT_SLICE)
313
            ->addChild($this->tree->getNode($sourceId))
314
            ->setAttribute('hasStart', isset($start))
315
            ->setAttribute('start', $start)
316
            ->setAttribute('hasEnd', isset($end))
317
            ->setAttribute('end', $end)
318
            ->setAttribute('step', $step ?? 1)
319
            ->getId();
320
    }
321
322
    /**
323
     * @param string $name
324
     * @param int $id
325
     * @return int
326
     * @throws UniLexException
327
     */
328
    public function aggregate(string $name, int $id): int
329
    {
330
        return $this
331
            ->tree
332
            ->createNode(AstNodeType::AGGREGATE)
333
            ->addChild($this->tree->getNode($id))
334
            ->setAttribute('name', $name)
335
            ->getId();
336
    }
337
338
    /**
339
     * @param int $sourceId
340
     * @param int $valueId
341
     * @return int
342
     * @throws UniLexException
343
     */
344
    public function populateLiteral(int $sourceId, int $valueId): int
345
    {
346
        return $this
347
            ->tree
348
            ->createNode(AstNodeType::POPULATE_LITERAL)
349
            ->addChild($this->tree->getNode($sourceId))
350
            ->addChild($this->tree->getNode($valueId))
351
            ->getId();
352
    }
353
354
    /**
355
     * @param int $sourceId
356
     * @param int $arrayId
357
     * @return int
358
     * @throws UniLexException
359
     */
360
    public function populateArrayElements(int $sourceId, int $arrayId): int
361
    {
362
        return $this
363
            ->tree
364
            ->createNode(AstNodeType::POPULATE_ARRAY_ELEMENTS)
365
            ->addChild($this->tree->getNode($sourceId))
366
            ->addChild($this->tree->getNode($arrayId))
367
            ->getId();
368
    }
369
370
    /**
371
     * @param int $sourceId
372
     * @param int $elementsId
373
     * @return int
374
     * @throws UniLexException
375
     */
376
    public function createLiteralArray(int $sourceId, int $elementsId): int
377
    {
378
        return $this
379
            ->tree
380
            ->createNode(AstNodeType::CREATE_LITERAL_ARRAY)
381
            ->addChild($this->tree->getNode($sourceId))
382
            ->addChild($this->tree->getNode($elementsId))
383
            ->getId();
384
    }
385
386
    /**
387
     * @param int $sourceId
388
     * @param int ...$indexList
389
     * @return int
390
     * @throws UniLexException
391
     */
392
    public function populateIndexList(int $sourceId, int ...$indexList): int
393
    {
394
        return $this
395
            ->tree
396
            ->createNode(AstNodeType::POPULATE_INDEX_LIST)
397
            ->addChild($this->tree->getNode($sourceId))
398
            ->setAttribute('indexList', $indexList)
399
            ->getId();
400
    }
401
402
    /**
403
     * @param int $sourceId
404
     * @param string ...$nameList
405
     * @return int
406
     * @throws UniLexException
407
     */
408
    public function populateNameList(int $sourceId, string ...$nameList): int
409
    {
410
        return $this
411
            ->tree
412
            ->createNode(AstNodeType::POPULATE_NAME_LIST)
413
            ->addChild($this->tree->getNode($sourceId))
414
            ->setAttribute('nameList', $nameList)
415
            ->getId();
416
    }
417
418
    /**
419
     * @param $value
420
     * @return int
421
     * @throws UniLexException
422
     */
423
    public function createScalar($value): int
424
    {
425
        return $this
426
            ->tree
427
            ->createNode(AstNodeType::CREATE_SCALAR)
428
            ->setAttribute('value', $value)
429
            ->getId();
430
    }
431
432
    public function createArray(): int
433
    {
434
        return $this
435
            ->tree
436
            ->createNode(AstNodeType::CREATE_ARRAY)
437
            ->getId();
438
    }
439
440
    /**
441
     * @param int $arrayId
442
     * @param int $valueId
443
     * @return int
444
     * @throws UniLexException
445
     */
446
    public function appendToArray(int $arrayId, int $valueId): int
447
    {
448
        $appendNode = $this
449
            ->tree
450
            ->createNode(AstNodeType::APPEND_TO_ARRAY)
451
            ->addChild($this->tree->getNode($valueId));
452
453
        return $this
454
            ->tree
455
            ->getNode($arrayId)
456
            ->addChild($appendNode)
457
            ->getId();
458
    }
459
}
460