33namespace Remorhaz \JSON \Patch ;
44
55use Remorhaz \JSON \Data \Exception as DataException ;
6+ use Remorhaz \JSON \Data \ReaderInterface ;
67use Remorhaz \JSON \Data \SelectorInterface ;
78use Remorhaz \JSON \Pointer \Pointer ;
89
910class Patch
1011{
1112
12- private $ dataSelector ;
13+ private $ inputSelector ;
14+
15+ private $ outputSelector ;
1316
1417 private $ patchSelector ;
1518
1619 private $ dataPointer ;
1720
1821 private $ patchPointer ;
1922
20-
2123 public function __construct (SelectorInterface $ dataSelector )
2224 {
23- $ this ->dataSelector = $ dataSelector ;
25+ $ this ->setInputSelector ( $ dataSelector) ;
2426 }
2527
26-
2728 public function apply (SelectorInterface $ patchSelector )
2829 {
2930 $ this
@@ -33,21 +34,49 @@ public function apply(SelectorInterface $patchSelector)
3334 if (!$ this ->getPatchSelector ()->isArray ()) {
3435 throw new \RuntimeException ("Patch must be an array " );
3536 }
37+ $ this ->setOutputSelector ($ this ->getInputSelector ());
3638 $ operationCount = $ this
3739 ->getPatchSelector ()
3840 ->getElementCount ();
3941 for ($ operationIndex = 0 ; $ operationIndex < $ operationCount ; $ operationIndex ++) {
4042 $ this ->performOperation ($ operationIndex );
4143 }
44+ $ this ->setInputSelector ($ this ->getOutputSelector ());
4245 return $ this ;
4346 }
4447
48+ public function getResult (): ReaderInterface
49+ {
50+ return $ this ->getOutputSelector ();
51+ }
4552
46- protected function getDataSelector (): SelectorInterface
53+ protected function setInputSelector ( SelectorInterface $ selector )
4754 {
48- return $ this ->dataSelector ;
55+ $ this ->inputSelector = $ selector ;
56+ return $ this ;
4957 }
5058
59+ protected function getInputSelector (): SelectorInterface
60+ {
61+ if (null === $ this ->inputSelector ) {
62+ throw new \LogicException ("Input selector is empty " );
63+ }
64+ return $ this ->inputSelector ;
65+ }
66+
67+ protected function setOutputSelector (SelectorInterface $ selector )
68+ {
69+ $ this ->outputSelector = $ selector ;
70+ return $ this ;
71+ }
72+
73+ protected function getOutputSelector (): SelectorInterface
74+ {
75+ if (null === $ this ->outputSelector ) {
76+ throw new \LogicException ("Output selector is empty " );
77+ }
78+ return $ this ->outputSelector ;
79+ }
5180
5281 protected function performOperation (int $ index )
5382 {
@@ -129,7 +158,7 @@ protected function getPatchPointer(): Pointer
129158 protected function getDataPointer (): Pointer
130159 {
131160 if (null === $ this ->dataPointer ) {
132- $ this ->dataPointer = new Pointer ($ this ->getDataSelector ());
161+ $ this ->dataPointer = new Pointer ($ this ->getInputSelector ());
133162 }
134163 return $ this ->dataPointer ;
135164 }
0 commit comments