|
2 | 2 |
|
3 | 3 | namespace Remorhaz\JSON\Patch; |
4 | 4 |
|
5 | | -use Remorhaz\JSON\Data\Exception as DataException; |
6 | | -use Remorhaz\JSON\Data\ReaderInterface; |
7 | | -use Remorhaz\JSON\Data\SelectorInterface; |
8 | | -use Remorhaz\JSON\Pointer\Pointer; |
| 5 | +use Remorhaz\JSON\Data\Value\ArrayValueInterface; |
| 6 | +use Remorhaz\JSON\Data\Value\DecodedJson\NodeValueFactory as DecodedJsonNodeValueFactory; |
| 7 | +use Remorhaz\JSON\Data\Value\NodeValueInterface; |
| 8 | +use Remorhaz\JSON\Pointer\Processor\Processor; |
| 9 | +use Remorhaz\JSON\Pointer\Processor\Result\ResultInterface; |
| 10 | +use Remorhaz\JSON\Pointer\Query\QueryFactory; |
| 11 | +use Remorhaz\JSON\Pointer\Query\QueryInterface; |
| 12 | +use RuntimeException; |
| 13 | +use function is_string; |
9 | 14 |
|
10 | 15 | class Patch |
11 | 16 | { |
12 | 17 |
|
13 | | - private $inputSelector; |
| 18 | + private $queryFactory; |
14 | 19 |
|
15 | | - private $outputSelector; |
| 20 | + private $queryProcessor; |
16 | 21 |
|
17 | | - private $patchSelector; |
| 22 | + private $decodedJsonNodeValueFactory; |
18 | 23 |
|
19 | | - private $dataPointer; |
| 24 | + private $outputData; |
20 | 25 |
|
21 | | - private $patchPointer; |
22 | | - |
23 | | - public function __construct(SelectorInterface $dataSelector) |
| 26 | + public function __construct(NodeValueInterface $inputData) |
24 | 27 | { |
25 | | - $this->setInputSelector($dataSelector); |
| 28 | + $this->outputData = $inputData; |
| 29 | + $this->queryFactory = QueryFactory::create(); |
| 30 | + $this->queryProcessor = Processor::create(); |
| 31 | + $this->decodedJsonNodeValueFactory = DecodedJsonNodeValueFactory::create(); |
26 | 32 | } |
27 | 33 |
|
28 | | - public function apply(SelectorInterface $patchSelector) |
| 34 | + public function apply(NodeValueInterface $patch) |
29 | 35 | { |
30 | | - $this |
31 | | - ->setPatchSelector($patchSelector) |
32 | | - ->getPatchSelector() |
33 | | - ->selectRoot(); |
34 | | - if (!$this->getPatchSelector()->isArray()) { |
35 | | - throw new \RuntimeException("Patch must be an array"); |
36 | | - } |
37 | | - $this->setOutputSelector($this->getInputSelector()); |
38 | | - $operationCount = $this |
39 | | - ->getPatchSelector() |
40 | | - ->getElementCount(); |
41 | | - for ($operationIndex = 0; $operationIndex < $operationCount; $operationIndex++) { |
42 | | - $this->performOperation($operationIndex); |
| 36 | + if (!$patch instanceof ArrayValueInterface) { |
| 37 | + throw new RuntimeException("Patch must be an array"); |
43 | 38 | } |
44 | | - $this->setInputSelector($this->getOutputSelector()); |
45 | | - return $this; |
46 | | - } |
47 | | - |
48 | | - public function getResult(): ReaderInterface |
49 | | - { |
50 | | - return $this->getOutputSelector(); |
51 | | - } |
52 | | - |
53 | | - protected function setInputSelector(SelectorInterface $selector) |
54 | | - { |
55 | | - $this->inputSelector = $selector; |
56 | | - return $this; |
57 | | - } |
58 | | - |
59 | | - protected function getInputSelector(): SelectorInterface |
60 | | - { |
61 | | - if (null === $this->inputSelector) { |
62 | | - throw new \LogicException("Input selector is empty"); |
| 39 | + foreach ($patch->createChildIterator() as $element) { |
| 40 | + $this->performOperation($element); |
63 | 41 | } |
64 | | - return $this->inputSelector; |
65 | | - } |
66 | 42 |
|
67 | | - protected function setOutputSelector(SelectorInterface $selector) |
68 | | - { |
69 | | - $this->outputSelector = $selector; |
70 | 43 | return $this; |
71 | 44 | } |
72 | 45 |
|
73 | | - protected function getOutputSelector(): SelectorInterface |
| 46 | + protected function performOperation(NodeValueInterface $element) |
74 | 47 | { |
75 | | - if (null === $this->outputSelector) { |
76 | | - throw new \LogicException("Output selector is empty"); |
| 48 | + $operation = $this |
| 49 | + ->queryProcessor |
| 50 | + ->select($this->queryFactory->createQuery('/op'), $element) |
| 51 | + ->decode(); |
| 52 | + if (!is_string($operation)) { |
| 53 | + throw new RuntimeException("Invalid patch operation"); |
77 | 54 | } |
78 | | - return $this->outputSelector; |
79 | | - } |
80 | | - |
81 | | - protected function performOperation(int $index) |
82 | | - { |
83 | | - $operation = $this->getPatchPointer()->read("/{$index}/op")->getAsString(); |
84 | | - $path = $this->getPatchPointer()->read("/{$index}/path")->getAsString(); |
| 55 | + $pathQuery = $this->getOperationPath($element); |
85 | 56 | switch ($operation) { |
86 | 57 | case 'add': |
87 | | - $valueReader = $this->getPatchPointer()->read("/{$index}/value"); |
88 | | - $this->getDataPointer()->add($path, $valueReader); |
| 58 | + $result = $this |
| 59 | + ->queryProcessor |
| 60 | + ->add( |
| 61 | + $pathQuery, |
| 62 | + $this->outputData, |
| 63 | + $this->getOperationValue($element) |
| 64 | + ); |
| 65 | + $this->outputData = $this->getResultValue($result); |
89 | 66 | break; |
90 | 67 |
|
91 | 68 | case 'remove': |
92 | | - $this->getDataPointer()->remove($path); |
| 69 | + $result = $this |
| 70 | + ->queryProcessor |
| 71 | + ->delete($pathQuery, $this->outputData); |
| 72 | + $this->outputData = $this->getResultValue($result); |
93 | 73 | break; |
94 | 74 |
|
95 | 75 | case 'replace': |
96 | | - $valueReader = $this->getPatchPointer()->read("/{$index}/value"); |
97 | | - $this->getDataPointer()->replace($path, $valueReader); |
| 76 | + $result = $this |
| 77 | + ->queryProcessor |
| 78 | + ->replace( |
| 79 | + $pathQuery, |
| 80 | + $this->outputData, |
| 81 | + $this->getOperationValue($element) |
| 82 | + ); |
| 83 | + $this->outputData = $this->getResultValue($result); |
98 | 84 | break; |
99 | 85 |
|
100 | 86 | case 'test': |
101 | | - $expectedValueReader = $this->getPatchPointer()->read("/{$index}/value"); |
102 | | - $actualValueReader = $this->getDataPointer()->read($path); |
103 | | - try { |
104 | | - // TODO: Make reader's test() method boolean and refactor pointer's test(). |
105 | | - $expectedValueReader->test($actualValueReader); |
106 | | - } catch (DataException $e) { |
107 | | - throw new \RuntimeException("Test operation failed", 0, $e); |
| 87 | + $result = $this |
| 88 | + ->queryProcessor |
| 89 | + ->select($pathQuery, $this->outputData); |
| 90 | + if (!$result->exists()) { |
| 91 | + throw new RuntimeException("Test operation failed"); |
108 | 92 | } |
109 | 93 | break; |
110 | 94 |
|
111 | 95 | case 'copy': |
112 | | - $from = $this->getPatchPointer()->read("/{$index}/from")->getAsString(); |
113 | | - $valueReader = $this->getDataPointer()->read($from); |
114 | | - $this->getDataPointer()->add($path, $valueReader); |
| 96 | + $fromResult = $this |
| 97 | + ->queryProcessor |
| 98 | + ->select( |
| 99 | + $this->getOperationFrom($element), |
| 100 | + $this->outputData |
| 101 | + ); |
| 102 | + $result = $this |
| 103 | + ->queryProcessor |
| 104 | + ->add( |
| 105 | + $pathQuery, |
| 106 | + $this->outputData, |
| 107 | + $this->getResultValue($fromResult) |
| 108 | + ); |
| 109 | + $this->outputData = $this->getResultValue($result); |
115 | 110 | break; |
116 | 111 |
|
117 | 112 | case 'move': |
118 | | - $from = $this->getPatchPointer()->read("/{$index}/from")->getAsString(); |
119 | | - $valueReader = $this->getDataPointer()->read($from); |
120 | | - $this |
121 | | - ->getDataPointer() |
122 | | - ->remove($from) |
123 | | - ->add($path, $valueReader); |
| 113 | + $fromPathQuery = $this->getOperationFrom($element); |
| 114 | + $fromResult = $this |
| 115 | + ->queryProcessor |
| 116 | + ->select($fromPathQuery, $this->outputData); |
| 117 | + $removeResult = $this |
| 118 | + ->queryProcessor |
| 119 | + ->delete($fromPathQuery, $this->outputData); |
| 120 | + $result = $this |
| 121 | + ->queryProcessor |
| 122 | + ->add( |
| 123 | + $pathQuery, |
| 124 | + $this->getResultValue($removeResult), |
| 125 | + $this->getResultValue($fromResult) |
| 126 | + ); |
| 127 | + $this->outputData = $this->getResultValue($result); |
124 | 128 | break; |
125 | 129 |
|
126 | 130 | default: |
127 | | - throw new \RuntimeException("Unknown operation '{$operation}'"); |
| 131 | + throw new RuntimeException("Unknown operation '{$operation}'"); |
128 | 132 | } |
| 133 | + |
129 | 134 | return $this; |
130 | 135 | } |
131 | 136 |
|
132 | | - |
133 | | - protected function setPatchSelector(SelectorInterface $patchReader) |
| 137 | + private function getOperationPath(NodeValueInterface $operation): QueryInterface |
134 | 138 | { |
135 | | - $this->patchSelector = $patchReader; |
136 | | - return $this; |
137 | | - } |
| 139 | + $path = $this |
| 140 | + ->queryProcessor |
| 141 | + ->select($this->queryFactory->createQuery('/path'), $operation) |
| 142 | + ->decode(); |
| 143 | + if (!is_string($path)) { |
| 144 | + throw new RuntimeException("Invalid operation path"); |
| 145 | + } |
138 | 146 |
|
| 147 | + return $this |
| 148 | + ->queryFactory |
| 149 | + ->createQuery($path); |
| 150 | + } |
139 | 151 |
|
140 | | - protected function getPatchSelector(): SelectorInterface |
| 152 | + private function getOperationFrom(NodeValueInterface $operation): QueryInterface |
141 | 153 | { |
142 | | - if (null === $this->patchSelector) { |
143 | | - throw new \LogicException("Patch reader is not set"); |
| 154 | + $path = $this |
| 155 | + ->queryProcessor |
| 156 | + ->select($this->queryFactory->createQuery('/from'), $operation) |
| 157 | + ->decode(); |
| 158 | + if (!is_string($path)) { |
| 159 | + throw new RuntimeException("Invalid operation from"); |
144 | 160 | } |
145 | | - return $this->patchSelector; |
146 | | - } |
147 | 161 |
|
| 162 | + return $this |
| 163 | + ->queryFactory |
| 164 | + ->createQuery($path); |
| 165 | + } |
148 | 166 |
|
149 | | - protected function getPatchPointer(): Pointer |
| 167 | + private function getOperationValue(NodeValueInterface $operation): NodeValueInterface |
150 | 168 | { |
151 | | - if (null === $this->patchPointer) { |
152 | | - $this->patchPointer = new Pointer($this->getPatchSelector()); |
| 169 | + $result = $this |
| 170 | + ->queryProcessor |
| 171 | + ->select($this->queryFactory->createQuery('/value'), $operation); |
| 172 | + |
| 173 | + if (!$result->exists()) { |
| 174 | + throw new RuntimeException("Patch result not found"); |
153 | 175 | } |
154 | | - return $this->patchPointer; |
| 176 | + |
| 177 | + return $this->getResultValue($result); |
155 | 178 | } |
156 | 179 |
|
| 180 | + private function getResultValue(ResultInterface $result): NodeValueInterface |
| 181 | + { |
| 182 | + return $this |
| 183 | + ->decodedJsonNodeValueFactory |
| 184 | + ->createValue($result->decode()); |
| 185 | + } |
157 | 186 |
|
158 | | - protected function getDataPointer(): Pointer |
| 187 | + public function getOutputData(): NodeValueInterface |
159 | 188 | { |
160 | | - if (null === $this->dataPointer) { |
161 | | - $this->dataPointer = new Pointer($this->getInputSelector()); |
162 | | - } |
163 | | - return $this->dataPointer; |
| 189 | + return $this->outputData; |
164 | 190 | } |
165 | 191 | } |
0 commit comments