Completed
Push — master ( 0acfd1...8c96cb )
by Edward
08:27
created

AstBuilder::calculateIsEqual()   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 fetchFilterContext(int $id): int
62
    {
63
        return $this
64
            ->tree
65
            ->createNode(AstNodeType::FETCH_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
     * @return int
260
     */
261
    public function matchAnyChild(): int
262
    {
263
        return $this
264
            ->tree
265
            ->createNode(AstNodeType::MATCH_ANY_CHILD)
266
            ->getId();
267
    }
268
269
    /**
270
     * @param string ...$names
271
     * @return int
272
     * @throws UniLexException
273
     */
274
    public function matchPropertyStrictly(string ...$names): int
275
    {
276
        return $this
277
            ->tree
278
            ->createNode(AstNodeType::MATCH_PROPERTY_STRICTLY)
279
            ->setAttribute('names', $names)
280
            ->getId();
281
    }
282
283
    /**
284
     * @param int ...$indexes
285
     * @return int
286
     * @throws UniLexException
287
     */
288
    public function matchElementStrictly(int ...$indexes): int
289
    {
290
        return $this
291
            ->tree
292
            ->createNode(AstNodeType::MATCH_ELEMENT_STRICTLY)
293
            ->setAttribute('indexes', $indexes)
294
            ->getId();
295
    }
296
297
    /**
298
     * @param int|null $start
299
     * @param int|null $end
300
     * @param int|null $step
301
     * @return int
302
     * @throws UniLexException
303
     */
304
    public function matchElementSlice(?int $start, ?int $end, ?int $step): int
305
    {
306
        return $this
307
            ->tree
308
            ->createNode(AstNodeType::MATCH_ELEMENT_SLICE)
309
            ->setAttribute('hasStart', isset($start))
310
            ->setAttribute('start', $start)
311
            ->setAttribute('hasEnd', isset($end))
312
            ->setAttribute('end', $end)
313
            ->setAttribute('step', $step ?? 1)
314
            ->getId();
315
    }
316
317
    /**
318
     * @param string $name
319
     * @param int $id
320
     * @return int
321
     * @throws UniLexException
322
     */
323
    public function aggregate(string $name, int $id): int
324
    {
325
        return $this
326
            ->tree
327
            ->createNode(AstNodeType::AGGREGATE)
328
            ->addChild($this->tree->getNode($id))
329
            ->setAttribute('name', $name)
330
            ->getId();
331
    }
332
333
    /**
334
     * @param int $sourceId
335
     * @param mixed $value
336
     * @return int
337
     * @throws UniLexException
338
     */
339
    public function createScalar(int $sourceId, $value): int
340
    {
341
        return $this
342
            ->tree
343
            ->createNode(AstNodeType::CREATE_LITERAL_SCALAR)
344
            ->setAttribute('value', $value)
345
            ->addChild($this->tree->getNode($sourceId))
346
            ->getId();
347
    }
348
349
    /**
350
     * @param int $sourceId
351
     * @param int $arrayId
352
     * @return int
353
     * @throws UniLexException
354
     */
355
    public function createLiteralArray(int $sourceId, int $arrayId): int
356
    {
357
        return $this
358
            ->tree
359
            ->createNode(AstNodeType::CREATE_LITERAL_ARRAY)
360
            ->addChild($this->tree->getNode($sourceId))
361
            ->addChild($this->tree->getNode($arrayId))
362
            ->getId();
363
    }
364
365
    public function createArray(): int
366
    {
367
        return $this
368
            ->tree
369
            ->createNode(AstNodeType::CREATE_ARRAY)
370
            ->getId();
371
    }
372
373
    /**
374
     * @param int $arrayId
375
     * @param int $valueId
376
     * @return int
377
     * @throws UniLexException
378
     */
379
    public function appendToArray(int $arrayId, int $valueId): int
380
    {
381
        $appendNode = $this
382
            ->tree
383
            ->createNode(AstNodeType::APPEND_TO_ARRAY)
384
            ->addChild($this->tree->getNode($valueId));
385
386
        return $this
387
            ->tree
388
            ->getNode($arrayId)
389
            ->addChild($appendNode)
390
            ->getId();
391
    }
392
}
393