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