Skip to content

Commit 6cc8e1b

Browse files
committed
Output selector implemented
1 parent 84d1aa5 commit 6cc8e1b

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

src/Patch.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@
33
namespace Remorhaz\JSON\Patch;
44

55
use Remorhaz\JSON\Data\Exception as DataException;
6+
use Remorhaz\JSON\Data\ReaderInterface;
67
use Remorhaz\JSON\Data\SelectorInterface;
78
use Remorhaz\JSON\Pointer\Pointer;
89

910
class 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

Comments
 (0)