|
8 | 8 | class Patch
|
9 | 9 | {
|
10 | 10 |
|
11 |
| - private $reader; |
| 11 | + private $dataReader; |
12 | 12 |
|
| 13 | + private $patchReader; |
13 | 14 |
|
14 |
| - public function __construct(SelectableReaderInterface $reader) |
| 15 | + private $dataPointer; |
| 16 | + |
| 17 | + private $patchPointer; |
| 18 | + |
| 19 | + |
| 20 | + public function __construct(SelectableReaderInterface $dataReader) |
15 | 21 | {
|
16 |
| - $this->reader = $reader; |
| 22 | + $this->dataReader = $dataReader; |
17 | 23 | }
|
18 | 24 |
|
19 | 25 |
|
20 | 26 | public function apply(SelectableReaderInterface $patchReader)
|
21 | 27 | {
|
22 |
| - $patchReader->selectRoot(); |
23 |
| - if (!$patchReader->isArraySelected()) { |
24 |
| - throw new \Exception("Patch must be an array"); |
| 28 | + $this |
| 29 | + ->setPatchReader($patchReader) |
| 30 | + ->getPatchReader() |
| 31 | + ->selectRoot(); |
| 32 | + if (!$this->getPatchReader()->isArraySelected()) { |
| 33 | + throw new \RuntimeException("Patch must be an array"); |
25 | 34 | }
|
26 |
| - $operationCount = $patchReader->getElementCount(); |
| 35 | + $operationCount = $this |
| 36 | + ->getPatchReader() |
| 37 | + ->getElementCount(); |
27 | 38 | for ($operationIndex = 0; $operationIndex < $operationCount; $operationIndex++) {
|
28 |
| - $this->performOperation($operationIndex, $patchReader); |
| 39 | + $this->performOperation($operationIndex); |
29 | 40 | }
|
30 | 41 | return $this;
|
31 | 42 | }
|
32 | 43 |
|
33 | 44 |
|
34 |
| - protected function getReader(): SelectableReaderInterface |
| 45 | + protected function getDataReader(): SelectableReaderInterface |
35 | 46 | {
|
36 |
| - return $this->reader; |
| 47 | + return $this->dataReader; |
37 | 48 | }
|
38 | 49 |
|
39 | 50 |
|
40 |
| - protected function performOperation(int $index, SelectableReaderInterface $patchReader) |
| 51 | + protected function performOperation(int $index) |
41 | 52 | {
|
42 |
| - $patchPointer = new Pointer($patchReader); |
43 |
| - $op = $patchPointer->read("/{$index}/op")->getData(); |
44 |
| - $path = $patchPointer->read("/{$index}/path")->getData(); |
45 |
| - $dataPointer = new Pointer($this->getReader()); |
| 53 | + $op = $this->getPatchPointer()->read("/{$index}/op")->getData(); |
| 54 | + $path = $this->getPatchPointer()->read("/{$index}/path")->getData(); |
46 | 55 | switch ($op) {
|
47 | 56 | case 'add':
|
48 |
| - $valueReader = $patchPointer->read("/{$index}/value"); |
49 |
| - $dataPointer->add($path, $valueReader); |
| 57 | + $valueReader = $this->getPatchPointer()->read("/{$index}/value"); |
| 58 | + $this->getDataPointer()->add($path, $valueReader); |
50 | 59 | break;
|
51 | 60 |
|
52 | 61 | case 'remove':
|
53 |
| - $dataPointer->remove($path); |
| 62 | + $this->getDataPointer()->remove($path); |
54 | 63 | break;
|
55 | 64 |
|
56 | 65 | case 'replace':
|
57 |
| - $valueReader = $patchPointer->read("/{$index}/value"); |
58 |
| - $dataPointer->replace($path, $valueReader); |
| 66 | + $valueReader = $this->getPatchPointer()->read("/{$index}/value"); |
| 67 | + $this->getDataPointer()->replace($path, $valueReader); |
59 | 68 | break;
|
60 | 69 |
|
61 | 70 | case 'test':
|
62 |
| - $expectedValueReader = $patchPointer->read("/{$index}/value"); |
63 |
| - $actualValueReader = $dataPointer->read($path); |
| 71 | + $expectedValueReader = $this->getPatchPointer()->read("/{$index}/value"); |
| 72 | + $actualValueReader = $this->getDataPointer()->read($path); |
64 | 73 | if ($expectedValueReader->getData() !== $actualValueReader->getData()) {
|
65 |
| - throw new \Exception("Test operation failed"); |
| 74 | + throw new \RuntimeException("Test operation failed"); |
66 | 75 | }
|
67 | 76 | break;
|
68 | 77 |
|
69 | 78 | case 'copy':
|
70 |
| - $from = $patchPointer->read("/{$index}/from")->getData(); |
71 |
| - $valueReader = $dataPointer->read($from); |
72 |
| - $dataPointer->add($path, $valueReader); |
| 79 | + $from = $this->getPatchPointer()->read("/{$index}/from")->getData(); |
| 80 | + $valueReader = $this->getDataPointer()->read($from); |
| 81 | + $this->getDataPointer()->add($path, $valueReader); |
73 | 82 | break;
|
74 | 83 |
|
75 | 84 | case 'move':
|
76 |
| - $from = $patchPointer->read("/{$index}/from")->getData(); |
77 |
| - $valueReader = $dataPointer->read($from); |
78 |
| - $dataPointer |
| 85 | + $from = $this->getPatchPointer()->read("/{$index}/from")->getData(); |
| 86 | + $valueReader = $this->getDataPointer()->read($from); |
| 87 | + $this |
| 88 | + ->getDataPointer() |
79 | 89 | ->remove($from)
|
80 | 90 | ->add($path, $valueReader);
|
81 | 91 | break;
|
82 | 92 |
|
83 | 93 | default:
|
84 |
| - throw new \Exception("Unknown operation '{$op}'"); |
| 94 | + throw new \RuntimeException("Unknown operation '{$op}'"); |
85 | 95 | }
|
86 | 96 | return $this;
|
87 | 97 | }
|
| 98 | + |
| 99 | + |
| 100 | + protected function setPatchReader(SelectableReaderInterface $patchReader) |
| 101 | + { |
| 102 | + $this->patchReader = $patchReader; |
| 103 | + return $this; |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + protected function getPatchReader(): SelectableReaderInterface |
| 108 | + { |
| 109 | + if (null === $this->patchReader) { |
| 110 | + throw new \LogicException("Patch reader is not set"); |
| 111 | + } |
| 112 | + return $this->patchReader; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + protected function getPatchPointer(): Pointer |
| 117 | + { |
| 118 | + if (null === $this->patchPointer) { |
| 119 | + $this->patchPointer = new Pointer($this->getPatchReader()); |
| 120 | + } |
| 121 | + return $this->patchPointer; |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + protected function getDataPointer(): Pointer |
| 126 | + { |
| 127 | + if (null === $this->dataPointer) { |
| 128 | + $this->dataPointer = new Pointer($this->getDataReader()); |
| 129 | + } |
| 130 | + return $this->dataPointer; |
| 131 | + } |
88 | 132 | }
|
0 commit comments