Passed
Push — master ( da0f52...6413a2 )
by Edward
03:06
created

AstBuilder::getInput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
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 populateLiteralArray(int $sourceId, int $arrayId): int
323
    {
324
        return $this
325
            ->tree
326
            ->createNode(AstNodeType::POPULATE_LITERAL_ARRAY)
327
            ->addChild($this->tree->getNode($sourceId))
328
            ->addChild($this->tree->getNode($arrayId))
329
            ->getId();
330
    }
331
332
    /**
333
     * @param int $sourceId
334
     * @param int ...$indexList
335
     * @return int
336
     * @throws UniLexException
337
     */
338
    public function populateIndexList(int $sourceId, int ...$indexList): int
339
    {
340
        return $this
341
            ->tree
342
            ->createNode(AstNodeType::POPULATE_INDEX_LIST)
343
            ->addChild($this->tree->getNode($sourceId))
344
            ->setAttribute('indexList', $indexList)
345
            ->getId();
346
    }
347
348
    /**
349
     * @param int $sourceId
350
     * @param int|null $start
351
     * @param int|null $end
352
     * @param int|null $step
353
     * @return int
354
     * @throws UniLexException
355
     */
356
    public function populateIndexSlice(int $sourceId, ?int $start, ?int $end, ?int $step): int
357
    {
358
        return $this
359
            ->tree
360
            ->createNode(AstNodeType::POPULATE_INDEX_SLICE)
361
            ->addChild($this->tree->getNode($sourceId))
362
            ->setAttribute('start', $start)
363
            ->setAttribute('end', $end)
364
            ->setAttribute('step', $step)
365
            ->getId();
366
    }
367
368
    /**
369
     * @param int $sourceId
370
     * @param string ...$nameList
371
     * @return int
372
     * @throws UniLexException
373
     */
374
    public function populateNameList(int $sourceId, string ...$nameList): int
375
    {
376
        return $this
377
            ->tree
378
            ->createNode(AstNodeType::POPULATE_NAME_LIST)
379
            ->addChild($this->tree->getNode($sourceId))
380
            ->setAttribute('nameList', $nameList)
381
            ->getId();
382
    }
383
384
    /**
385
     * @param $value
386
     * @return int
387
     * @throws UniLexException
388
     */
389
    public function createScalar($value): int
390
    {
391
        return $this
392
            ->tree
393
            ->createNode(AstNodeType::CREATE_SCALAR)
394
            ->setAttribute('value', $value)
395
            ->getId();
396
    }
397
398
    public function createArray(): int
399
    {
400
        return $this
401
            ->tree
402
            ->createNode(AstNodeType::CREATE_ARRAY)
403
            ->getId();
404
    }
405
406
    /**
407
     * @param int $arrayId
408
     * @param int $valueId
409
     * @return int
410
     * @throws UniLexException
411
     */
412
    public function appendToArray(int $arrayId, int $valueId): int
413
    {
414
        $appendNode = $this
415
            ->tree
416
            ->createNode(AstNodeType::APPEND_TO_ARRAY)
417
            ->addChild($this->tree->getNode($valueId));
418
419
        return $this
420
            ->tree
421
            ->getNode($arrayId)
422
            ->addChild($appendNode)
423
            ->getId();
424
    }
425
}
426