1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Remorhaz\JSON\Path\Runtime; |
5
|
|
|
|
6
|
|
|
use Remorhaz\JSON\Data\Value\ArrayValueInterface; |
7
|
|
|
use Remorhaz\JSON\Path\Value\EvaluatedValueInterface; |
8
|
|
|
use Remorhaz\JSON\Path\Value\EvaluatedValueList; |
9
|
|
|
use Remorhaz\JSON\Path\Value\EvaluatedValueListInterface; |
10
|
|
|
use Remorhaz\JSON\Data\Value\NodeValueInterface; |
11
|
|
|
use Remorhaz\JSON\Path\Value\NodeValueList; |
12
|
|
|
use Remorhaz\JSON\Path\Value\NodeValueListBuilder; |
13
|
|
|
use Remorhaz\JSON\Path\Value\NodeValueListInterface; |
14
|
|
|
|
15
|
|
|
final class ValueListFetcher implements ValueListFetcherInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
private $valueFetcher; |
19
|
|
|
|
20
|
|
|
public function __construct(ValueFetcherInterface $valueFetcher) |
21
|
|
|
{ |
22
|
|
|
$this->valueFetcher = $valueFetcher; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param NodeValueListInterface $source |
27
|
|
|
* @param Matcher\ChildMatcherInterface $matcher |
28
|
|
|
* @return NodeValueListInterface |
29
|
|
|
*/ |
30
|
|
|
public function fetchChildren( |
31
|
|
|
NodeValueListInterface $source, |
32
|
|
|
Matcher\ChildMatcherInterface $matcher |
33
|
|
|
): NodeValueListInterface { |
34
|
|
|
$nodesBuilder = new NodeValueListBuilder; |
35
|
|
|
foreach ($source->getValues() as $sourceIndex => $sourceValue) { |
36
|
|
|
$children = $this |
37
|
|
|
->valueFetcher |
38
|
|
|
->createChildrenIterator($matcher, $sourceValue); |
39
|
|
|
$outerIndex = $source->getIndexMap()->getOuterIndex($sourceIndex); |
40
|
|
|
foreach ($children as $child) { |
41
|
|
|
$nodesBuilder->addValue($child, $outerIndex); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $nodesBuilder->build(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function fetchChildrenDeep( |
49
|
|
|
NodeValueListInterface $source, |
50
|
|
|
Matcher\ChildMatcherInterface $matcher |
51
|
|
|
): NodeValueListInterface { |
52
|
|
|
$nodesBuilder = new NodeValueListBuilder; |
53
|
|
|
foreach ($source->getValues() as $sourceIndex => $sourceValue) { |
54
|
|
|
$children = $this |
55
|
|
|
->valueFetcher |
56
|
|
|
->createDeepChildrenIterator($matcher, $sourceValue); |
57
|
|
|
$outerIndex = $source->getIndexMap()->getOuterIndex($sourceIndex); |
58
|
|
|
foreach ($children as $child) { |
59
|
|
|
$nodesBuilder->addValue($child, $outerIndex); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $nodesBuilder->build(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function merge(NodeValueListInterface ...$sources): NodeValueListInterface |
67
|
|
|
{ |
68
|
|
|
$nodesBuilder = new NodeValueListBuilder; |
69
|
|
|
foreach ($sources as $source) { |
70
|
|
|
foreach ($source->getValues() as $innerIndex => $value) { |
71
|
|
|
$nodesBuilder->addValue( |
72
|
|
|
$value, |
73
|
|
|
$source->getIndexMap()->getOuterIndex($innerIndex), |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $nodesBuilder->build(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function fetchFilterContext(NodeValueListInterface $source): NodeValueListInterface |
82
|
|
|
{ |
83
|
|
|
$nodesBuilder = new NodeValueListBuilder; |
84
|
|
|
foreach ($source->getValues() as $sourceIndex => $sourceValue) { |
85
|
|
|
if (!$sourceValue instanceof NodeValueInterface) { |
86
|
|
|
throw new Exception\InvalidContextValueException($sourceValue); |
87
|
|
|
} |
88
|
|
|
$outerIndex = $source->getIndexMap()->getOuterIndex($sourceIndex); |
89
|
|
|
$children = $sourceValue instanceof ArrayValueInterface |
90
|
|
|
? $this |
91
|
|
|
->valueFetcher |
92
|
|
|
->createChildrenIterator(new Matcher\AnyChildMatcher, $sourceValue) |
93
|
|
|
: [$sourceValue]; |
94
|
|
|
foreach ($children as $child) { |
95
|
|
|
$nodesBuilder->addValue($child, $outerIndex); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $nodesBuilder->build(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function fetchFilteredValues( |
103
|
|
|
NodeValueListInterface $values, |
104
|
|
|
EvaluatedValueListInterface $results |
105
|
|
|
): NodeValueListInterface { |
106
|
|
|
if (!$values->getIndexMap()->isCompatible($results->getIndexMap())) { |
107
|
|
|
throw new Exception\IndexMapMatchFailedException($values, $results); |
108
|
|
|
} |
109
|
|
|
$valueIndex = 0; |
110
|
|
|
$valueMap = []; |
111
|
|
|
foreach ($results->getIndexMap()->toArray() as $innerIndex => $outerIndex) { |
112
|
|
|
if (isset($outerIndex)) { |
113
|
|
|
$valueMap[$innerIndex] = $valueIndex++; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
$nodesBuilder = new NodeValueListBuilder; |
117
|
|
|
foreach ($values->getValues() as $index => $value) { |
118
|
|
|
if (!isset($valueMap[$index])) { |
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
$evaluatedValue = $results->getValue($valueMap[$index]); |
122
|
|
|
if (!$evaluatedValue instanceof EvaluatedValueInterface) { |
123
|
|
|
throw new Exception\InvalidFilterValueException($evaluatedValue); |
124
|
|
|
} |
125
|
|
|
if (!$evaluatedValue->getData()) { |
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
$nodesBuilder->addValue( |
129
|
|
|
$value, |
130
|
|
|
$values->getIndexMap()->getOuterIndex($index) |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $nodesBuilder->build(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function splitFilterContext(NodeValueListInterface $values): NodeValueListInterface |
138
|
|
|
{ |
139
|
|
|
return new NodeValueList( |
140
|
|
|
$values->getIndexMap()->split(), |
141
|
|
|
...$values->getValues(), |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function joinFilterResults( |
146
|
|
|
EvaluatedValueListInterface $evaluatedValues, |
147
|
|
|
NodeValueListInterface $contextValues |
148
|
|
|
): EvaluatedValueListInterface { |
149
|
|
|
return new EvaluatedValueList( |
150
|
|
|
$evaluatedValues->getIndexMap()->join($contextValues->getIndexMap()), |
151
|
|
|
...$evaluatedValues->getResults() |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|