Completed
Push — master ( 113bfa...89e2e4 )
by Edward
07:06
created

AstBuilder::evaluate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 8
ccs 0
cts 6
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 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 string $name
286
     * @param int $id
287
     * @return int
288
     * @throws UniLexException
289
     */
290
    public function aggregate(string $name, int $id): int
291
    {
292
        return $this
293
            ->tree
294
            ->createNode(AstNodeType::AGGREGATE)
295
            ->addChild($this->tree->getNode($id))
296
            ->setAttribute('name', $name)
297
            ->getId();
298
    }
299
300
    /**
301
     * @param int $sourceId
302
     * @param int $valueId
303
     * @return int
304
     * @throws UniLexException
305
     */
306
    public function populateLiteral(int $sourceId, int $valueId): int
307
    {
308
        return $this
309
            ->tree
310
            ->createNode(AstNodeType::POPULATE_LITERAL)
311
            ->addChild($this->tree->getNode($sourceId))
312
            ->addChild($this->tree->getNode($valueId))
313
            ->getId();
314
    }
315
316
    /**
317
     * @param int $sourceId
318
     * @param int $arrayId
319
     * @return int
320
     * @throws UniLexException
321
     */
322
    public function populateArrayElements(int $sourceId, int $arrayId): int
323
    {
324
        return $this
325
            ->tree
326
            ->createNode(AstNodeType::POPULATE_ARRAY_ELEMENTS)
327
            ->addChild($this->tree->getNode($sourceId))
328
            ->addChild($this->tree->getNode($arrayId))
329
            ->getId();
330
    }
331
332
    /**
333
     * @param int $sourceId
334
     * @param int $elementsId
335
     * @return int
336
     * @throws UniLexException
337
     */
338
    public function createLiteralArray(int $sourceId, int $elementsId): int
339
    {
340
        return $this
341
            ->tree
342
            ->createNode(AstNodeType::CREATE_LITERAL_ARRAY)
343
            ->addChild($this->tree->getNode($sourceId))
344
            ->addChild($this->tree->getNode($elementsId))
345
            ->getId();
346
    }
347
348
    /**
349
     * @param int $sourceId
350
     * @param int ...$indexList
351
     * @return int
352
     * @throws UniLexException
353
     */
354
    public function populateIndexList(int $sourceId, int ...$indexList): int
355
    {
356
        return $this
357
            ->tree
358
            ->createNode(AstNodeType::POPULATE_INDEX_LIST)
359
            ->addChild($this->tree->getNode($sourceId))
360
            ->setAttribute('indexList', $indexList)
361
            ->getId();
362
    }
363
364
    /**
365
     * @param int $sourceId
366
     * @param int|null $start
367
     * @param int|null $end
368
     * @param int|null $step
369
     * @return int
370
     * @throws UniLexException
371
     */
372
    public function populateIndexSlice(int $sourceId, ?int $start, ?int $end, ?int $step): int
373
    {
374
        return $this
375
            ->tree
376
            ->createNode(AstNodeType::POPULATE_INDEX_SLICE)
377
            ->addChild($this->tree->getNode($sourceId))
378
            ->setAttribute('start', $start)
379
            ->setAttribute('end', $end)
380
            ->setAttribute('step', $step)
381
            ->getId();
382
    }
383
384
    /**
385
     * @param int $sourceId
386
     * @param string ...$nameList
387
     * @return int
388
     * @throws UniLexException
389
     */
390
    public function populateNameList(int $sourceId, string ...$nameList): int
391
    {
392
        return $this
393
            ->tree
394
            ->createNode(AstNodeType::POPULATE_NAME_LIST)
395
            ->addChild($this->tree->getNode($sourceId))
396
            ->setAttribute('nameList', $nameList)
397
            ->getId();
398
    }
399
400
    /**
401
     * @param $value
402
     * @return int
403
     * @throws UniLexException
404
     */
405
    public function createScalar($value): int
406
    {
407
        return $this
408
            ->tree
409
            ->createNode(AstNodeType::CREATE_SCALAR)
410
            ->setAttribute('value', $value)
411
            ->getId();
412
    }
413
414
    public function createArray(): int
415
    {
416
        return $this
417
            ->tree
418
            ->createNode(AstNodeType::CREATE_ARRAY)
419
            ->getId();
420
    }
421
422
    /**
423
     * @param int $arrayId
424
     * @param int $valueId
425
     * @return int
426
     * @throws UniLexException
427
     */
428
    public function appendToArray(int $arrayId, int $valueId): int
429
    {
430
        $appendNode = $this
431
            ->tree
432
            ->createNode(AstNodeType::APPEND_TO_ARRAY)
433
            ->addChild($this->tree->getNode($valueId));
434
435
        return $this
436
            ->tree
437
            ->getNode($arrayId)
438
            ->addChild($appendNode)
439
            ->getId();
440
    }
441
}
442