1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Remorhaz\JSON\Pointer\Query; |
6
|
|
|
|
7
|
|
|
use Remorhaz\JSON\Data\Value\ArrayValueInterface; |
8
|
|
|
use Remorhaz\JSON\Data\Value\NodeValueInterface; |
9
|
|
|
use Remorhaz\JSON\Data\Value\ObjectValueInterface; |
10
|
|
|
use Remorhaz\JSON\Data\Value\ScalarValueInterface; |
11
|
|
|
use Remorhaz\JSON\Pointer\Locator\IndexReferenceInterface; |
12
|
|
|
use Remorhaz\JSON\Pointer\Locator\LocatorInterface; |
13
|
|
|
use Remorhaz\JSON\Pointer\Locator\NextIndexReferenceInterface; |
14
|
|
|
|
15
|
|
|
final class Query implements QueryInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
private $source; |
19
|
|
|
|
20
|
|
|
private $locator; |
21
|
|
|
|
22
|
2 |
|
public function __construct(string $source, LocatorInterface $locator) |
23
|
|
|
{ |
24
|
2 |
|
$this->source = $source; |
25
|
2 |
|
$this->locator = $locator; |
26
|
2 |
|
} |
27
|
|
|
|
28
|
1 |
|
public function __invoke(NodeValueInterface $rootNode): QueryResultInterface |
29
|
|
|
{ |
30
|
1 |
|
$node = $rootNode; |
31
|
1 |
|
$parentNode = null; |
32
|
1 |
|
$lastReference = null; |
33
|
1 |
|
foreach ($this->locator->references() as $listedReference) { |
34
|
1 |
|
if ($listedReference->isLast()) { |
35
|
1 |
|
$lastReference = $listedReference->getReference(); |
36
|
|
|
} |
37
|
1 |
|
if ($node instanceof ScalarValueInterface) { |
38
|
|
|
return new QueryResult($this->source); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
if ($node instanceof ArrayValueInterface) { |
42
|
|
|
$reference = $listedReference->getReference(); |
43
|
|
|
switch (true) { |
44
|
|
|
case $reference instanceof IndexReferenceInterface: |
45
|
|
|
$key = $reference->getElementIndex(); |
46
|
|
|
break; |
47
|
|
|
|
48
|
|
|
case $reference instanceof NextIndexReferenceInterface: |
49
|
|
|
$key = null; |
50
|
|
|
break; |
51
|
|
|
|
52
|
|
|
default: |
53
|
|
|
return new QueryResult($this->source); |
54
|
|
|
} |
55
|
|
|
$lastIndex = null; |
56
|
|
|
foreach ($node->createChildIterator() as $index => $element) { |
57
|
|
|
$lastIndex = $index; |
58
|
|
|
if ($index === $key) { |
59
|
|
|
$parentNode = $node; |
60
|
|
|
$node = $element; |
61
|
|
|
continue 2; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (isset($key, $lastIndex) && $key != $lastIndex + 1) { |
66
|
|
|
return new QueryResult($this->source); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return new QueryResult( |
70
|
|
|
$this->source, |
71
|
|
|
null, |
72
|
|
|
$listedReference->isLast() ? $node : null, |
73
|
|
|
$lastReference, |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
if ($node instanceof ObjectValueInterface) { |
78
|
1 |
|
$key = $listedReference->getReference()->getPropertyName(); |
79
|
1 |
|
foreach ($node->createChildIterator() as $name => $property) { |
80
|
1 |
|
if ($name == $key) { |
81
|
1 |
|
$parentNode = $node; |
82
|
1 |
|
$node = $property; |
83
|
1 |
|
continue 2; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new QueryResult( |
88
|
|
|
$this->source, |
89
|
|
|
null, |
90
|
|
|
$listedReference->isLast() ? $node : null, |
91
|
|
|
$lastReference, |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return new QueryResult($this->source, $node, $parentNode, $lastReference); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
public function getSource(): string |
100
|
|
|
{ |
101
|
1 |
|
return $this->source; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|