1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Remorhaz\JSON\Path\Processor; |
5
|
|
|
|
6
|
|
|
use Collator; |
7
|
|
|
use Remorhaz\JSON\Data\Event\ValueWalker; |
8
|
|
|
use Remorhaz\JSON\Data\Export\EventDecoder; |
9
|
|
|
use Remorhaz\JSON\Data\Export\ValueDecoder; |
10
|
|
|
use Remorhaz\JSON\Data\Export\ValueEncoder; |
11
|
|
|
use Remorhaz\JSON\Data\Value\NodeValueInterface; |
12
|
|
|
use Remorhaz\JSON\Path\Processor\Mutator\Mutator; |
13
|
|
|
use Remorhaz\JSON\Path\Processor\Mutator\MutatorInterface; |
14
|
|
|
use Remorhaz\JSON\Path\Processor\Result\ValueResultInterface; |
15
|
|
|
use Remorhaz\JSON\Path\Processor\Result\ResultFactory; |
16
|
|
|
use Remorhaz\JSON\Path\Processor\Result\ResultFactoryInterface; |
17
|
|
|
use Remorhaz\JSON\Path\Processor\Result\SelectOnePathResultInterface; |
18
|
|
|
use Remorhaz\JSON\Path\Processor\Result\SelectPathsResultInterface; |
19
|
|
|
use Remorhaz\JSON\Path\Processor\Result\SelectResultInterface; |
20
|
|
|
use Remorhaz\JSON\Path\Query\QueryInterface; |
21
|
|
|
use Remorhaz\JSON\Path\Query\QueryValidator; |
22
|
|
|
use Remorhaz\JSON\Path\Query\QueryValidatorInterface; |
23
|
|
|
use Remorhaz\JSON\Path\Runtime\Aggregator\AggregatorCollection; |
24
|
|
|
use Remorhaz\JSON\Path\Runtime\Comparator\ComparatorCollection; |
25
|
|
|
use Remorhaz\JSON\Path\Runtime\Evaluator; |
26
|
|
|
use Remorhaz\JSON\Path\Runtime\LiteralFactory; |
27
|
|
|
use Remorhaz\JSON\Path\Runtime\Matcher\MatcherFactory; |
28
|
|
|
use Remorhaz\JSON\Path\Runtime\ValueListFetcher; |
29
|
|
|
use Remorhaz\JSON\Path\Runtime\Runtime; |
30
|
|
|
use Remorhaz\JSON\Path\Runtime\RuntimeInterface; |
31
|
|
|
use Remorhaz\JSON\Path\Runtime\ValueFetcher; |
32
|
|
|
|
33
|
|
|
final class Processor implements ProcessorInterface |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
private $runtime; |
37
|
|
|
|
38
|
|
|
private $resultFactory; |
39
|
|
|
|
40
|
|
|
private $queryValidator; |
41
|
|
|
|
42
|
|
|
private $mutator; |
43
|
|
|
|
44
|
18 |
|
public static function create(): ProcessorInterface |
45
|
|
|
{ |
46
|
18 |
|
$runtime = new Runtime( |
47
|
18 |
|
new ValueListFetcher(new ValueFetcher), |
48
|
18 |
|
new Evaluator( |
49
|
18 |
|
new ComparatorCollection(new Collator('UTF-8')), |
50
|
18 |
|
new AggregatorCollection, |
51
|
|
|
), |
52
|
18 |
|
new LiteralFactory, |
53
|
18 |
|
new MatcherFactory, |
54
|
|
|
); |
55
|
18 |
|
$jsonDecoder = new ValueDecoder; |
56
|
18 |
|
$jsonEncoder = new ValueEncoder($jsonDecoder); |
57
|
|
|
|
58
|
18 |
|
return new self( |
59
|
18 |
|
$runtime, |
60
|
18 |
|
new ResultFactory($jsonEncoder, $jsonDecoder, new PathEncoder), |
61
|
18 |
|
new QueryValidator, |
62
|
18 |
|
new Mutator(new ValueWalker, new EventDecoder), |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
18 |
|
public function __construct( |
67
|
|
|
RuntimeInterface $runtime, |
68
|
|
|
ResultFactoryInterface $resultFactory, |
69
|
|
|
QueryValidatorInterface $queryValidator, |
70
|
|
|
MutatorInterface $mutator |
71
|
|
|
) { |
72
|
18 |
|
$this->runtime = $runtime; |
73
|
18 |
|
$this->resultFactory = $resultFactory; |
74
|
18 |
|
$this->queryValidator = $queryValidator; |
75
|
18 |
|
$this->mutator = $mutator; |
76
|
18 |
|
} |
77
|
|
|
|
78
|
3 |
|
public function select(QueryInterface $query, NodeValueInterface $rootNode): SelectResultInterface |
79
|
|
|
{ |
80
|
3 |
|
$values = $query($rootNode, $this->runtime); |
81
|
|
|
|
82
|
|
|
return $this |
83
|
3 |
|
->resultFactory |
84
|
3 |
|
->createSelectResult($values); |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
public function selectOne(QueryInterface $query, NodeValueInterface $rootNode): ValueResultInterface |
88
|
|
|
{ |
89
|
|
|
$values = $this |
90
|
3 |
|
->queryValidator |
91
|
3 |
|
->getDefiniteQuery($query)($rootNode, $this->runtime); |
92
|
|
|
|
93
|
|
|
return $this |
94
|
2 |
|
->resultFactory |
95
|
2 |
|
->createSelectOneResult($values); |
96
|
|
|
} |
97
|
|
|
|
98
|
8 |
|
public function selectPaths(QueryInterface $query, NodeValueInterface $rootNode): SelectPathsResultInterface |
99
|
|
|
{ |
100
|
|
|
$values = $this |
101
|
8 |
|
->queryValidator |
102
|
8 |
|
->getAddressableQuery($query)($rootNode, $this->runtime); |
103
|
|
|
|
104
|
|
|
return $this |
105
|
7 |
|
->resultFactory |
106
|
7 |
|
->createSelectPathsResult($values); |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
public function selectOnePath(QueryInterface $query, NodeValueInterface $rootNode): SelectOnePathResultInterface |
110
|
|
|
{ |
111
|
|
|
$query = $this |
112
|
4 |
|
->queryValidator |
113
|
4 |
|
->getDefiniteQuery($query); |
114
|
|
|
$values = $this |
115
|
3 |
|
->queryValidator |
116
|
3 |
|
->getAddressableQuery($query)($rootNode, $this->runtime); |
117
|
|
|
|
118
|
|
|
return $this |
119
|
2 |
|
->resultFactory |
120
|
2 |
|
->createSelectOnePathResult($values); |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
public function delete(QueryInterface $query, NodeValueInterface $rootNode): ValueResultInterface |
124
|
|
|
{ |
125
|
|
|
$paths = $this |
126
|
3 |
|
->selectPaths($query, $rootNode) |
127
|
2 |
|
->get(); |
128
|
|
|
$value = $this |
129
|
2 |
|
->mutator |
130
|
2 |
|
->deletePaths($rootNode, ...$paths); |
131
|
|
|
|
132
|
|
|
return $this |
133
|
2 |
|
->resultFactory |
134
|
2 |
|
->createValueResult($value); |
135
|
|
|
} |
136
|
|
|
|
137
|
2 |
|
public function replace( |
138
|
|
|
QueryInterface $query, |
139
|
|
|
NodeValueInterface $rootNode, |
140
|
|
|
NodeValueInterface $newNode |
141
|
|
|
): ValueResultInterface { |
142
|
|
|
$paths = $this |
143
|
2 |
|
->selectPaths($query, $rootNode) |
144
|
2 |
|
->get(); |
145
|
|
|
$value = $this |
146
|
2 |
|
->mutator |
147
|
2 |
|
->replacePaths($rootNode, $newNode, ...$paths); |
148
|
|
|
|
149
|
|
|
return $this |
150
|
1 |
|
->resultFactory |
151
|
1 |
|
->createValueResult($value); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|