1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Remorhaz\JSON\Path\Runtime; |
5
|
|
|
|
6
|
|
|
use Remorhaz\JSON\Data\Value\StructValueInterface; |
7
|
|
|
use function iterator_count; |
8
|
|
|
use Remorhaz\JSON\Data\Value\ArrayValueInterface; |
9
|
|
|
use Remorhaz\JSON\Data\Value\NodeValueInterface; |
10
|
|
|
use Remorhaz\JSON\Data\Value\ScalarValueInterface; |
11
|
|
|
|
12
|
|
|
final class ValueFetcher implements ValueFetcherInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Matcher\ChildMatcherInterface $matcher |
17
|
|
|
* @param NodeValueInterface $value |
18
|
|
|
* @return NodeValueInterface[] |
19
|
|
|
*/ |
20
|
|
|
public function fetchValueChildren( |
21
|
|
|
Matcher\ChildMatcherInterface $matcher, |
22
|
|
|
NodeValueInterface $value |
23
|
|
|
): array { |
24
|
|
|
if ($value instanceof ScalarValueInterface) { |
25
|
|
|
return []; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ($value instanceof StructValueInterface) { |
29
|
|
|
return $this->fetchChildren($value, $matcher); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
throw new Exception\UnexpectedNodeValueFetchedException($value); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function fetchValueDeepChildren( |
36
|
|
|
Matcher\ChildMatcherInterface $matcher, |
37
|
|
|
NodeValueInterface $value |
38
|
|
|
): array { |
39
|
|
|
if ($value instanceof ScalarValueInterface) { |
40
|
|
|
return []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if ($value instanceof StructValueInterface) { |
44
|
|
|
return $this->fetchDeepChildren($value, $matcher); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
throw new Exception\UnexpectedNodeValueFetchedException($value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function fetchChildren(NodeValueInterface $value, Matcher\ChildMatcherInterface $matcher): array |
51
|
|
|
{ |
52
|
|
|
if (!$value instanceof StructValueInterface) { |
53
|
|
|
// TODO: extract correct argument type? |
54
|
|
|
throw new Exception\UnexpectedNodeValueFetchedException($value); |
55
|
|
|
} |
56
|
|
|
$results = []; |
57
|
|
|
foreach ($value->createChildIterator() as $index => $element) { |
58
|
|
|
if ($matcher->match($index, $element, $value)) { |
59
|
|
|
$results[] = $element; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $results; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function fetchDeepChildren(NodeValueInterface $value, Matcher\ChildMatcherInterface $matcher): array |
67
|
|
|
{ |
68
|
|
|
if (!$value instanceof StructValueInterface) { |
69
|
|
|
// TODO: extract correct argument type? |
70
|
|
|
throw new Exception\UnexpectedNodeValueFetchedException($value); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$results = []; |
74
|
|
|
foreach ($value->createChildIterator() as $index => $element) { |
75
|
|
|
if ($matcher->match($index, $element, $value)) { |
76
|
|
|
$results[] = $element; |
77
|
|
|
} |
78
|
|
|
array_push( |
79
|
|
|
$results, |
80
|
|
|
...$this->fetchValueDeepChildren($matcher, $element) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $results; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function fetchArrayLength(NodeValueInterface $value): ?int |
88
|
|
|
{ |
89
|
|
|
return $value instanceof ArrayValueInterface |
90
|
|
|
? iterator_count($value->createChildIterator()) |
91
|
|
|
: null; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|