1 | <?php |
||
21 | class MapPatcher extends ThrowingPatcher { |
||
22 | |||
23 | /** |
||
24 | * @var Patcher |
||
25 | */ |
||
26 | private $listPatcher; |
||
27 | |||
28 | /** |
||
29 | * @var ValueComparer|null |
||
30 | */ |
||
31 | private $comparer = null; |
||
32 | |||
33 | /** |
||
34 | * @since 0.4 |
||
35 | * |
||
36 | * @param bool $throwErrors |
||
37 | * @param Patcher|null $listPatcher The patcher that will be used for lists in the value |
||
38 | */ |
||
39 | 13 | public function __construct( bool $throwErrors = false, Patcher $listPatcher = null ) { |
|
44 | |||
45 | /** |
||
46 | * @see Patcher::patch |
||
47 | * |
||
48 | * Applies the provided diff to the provided array and returns the result. |
||
49 | * The array is treated as a map, ie keys are held into account. |
||
50 | * |
||
51 | * It is possible to pass in non-associative diffs (those for which isAssociative) |
||
52 | * returns false, however the likely intended behavior can be obtained via |
||
53 | * a list patcher. |
||
54 | * |
||
55 | * @since 0.4 |
||
56 | * |
||
57 | * @param array $base |
||
58 | * @param Diff $diff |
||
59 | * |
||
60 | * @return array |
||
61 | * @throws PatcherException |
||
62 | */ |
||
63 | 27 | public function patch( array $base, Diff $diff ): array { |
|
70 | |||
71 | /** |
||
72 | * @param array &$base |
||
73 | * @param int|string $key |
||
74 | * @param DiffOp $diffOp |
||
75 | * |
||
76 | * @throws PatcherException |
||
77 | */ |
||
78 | 23 | private function applyOperation( array &$base, $key, DiffOp $diffOp ) { |
|
95 | |||
96 | /** |
||
97 | * @param array &$base |
||
98 | * @param int|string $key |
||
99 | * @param DiffOpAdd $diffOp |
||
100 | * |
||
101 | * @throws PatcherException |
||
102 | */ |
||
103 | 11 | private function applyDiffOpAdd( array &$base, $key, DiffOpAdd $diffOp ) { |
|
111 | |||
112 | /** |
||
113 | * @param array &$base |
||
114 | * @param int|string $key |
||
115 | * @param DiffOpRemove $diffOp |
||
116 | * |
||
117 | * @throws PatcherException |
||
118 | */ |
||
119 | 9 | private function applyDiffOpRemove( array &$base, $key, DiffOpRemove $diffOp ) { |
|
132 | |||
133 | /** |
||
134 | * @param array &$base |
||
135 | * @param int|string $key |
||
136 | * @param DiffOpChange $diffOp |
||
137 | * |
||
138 | * @throws PatcherException |
||
139 | */ |
||
140 | 9 | private function applyDiffOpChange( array &$base, $key, DiffOpChange $diffOp ) { |
|
153 | |||
154 | /** |
||
155 | * @param array &$base |
||
156 | * @param int|string $key |
||
157 | * @param Diff $diffOp |
||
158 | * |
||
159 | * @throws PatcherException |
||
160 | */ |
||
161 | 3 | private function applyDiff( &$base, $key, Diff $diffOp ) { |
|
173 | |||
174 | /** |
||
175 | * @param array &$base |
||
176 | * @param int|string $key |
||
177 | * @param Diff $diffOp |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | 3 | private function isAttemptToModifyNotExistingElement( $base, $key, Diff $diffOp ): bool { |
|
185 | |||
186 | /** |
||
187 | * @param array $base |
||
188 | * @param Diff $diff |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | 3 | private function patchMapOrList( array $base, Diff $diff ): array { |
|
199 | |||
200 | /** |
||
201 | * @param mixed $firstValue |
||
202 | * @param mixed $secondValue |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | 11 | private function valuesAreEqual( $firstValue, $secondValue ): bool { |
|
213 | |||
214 | /** |
||
215 | * Sets the value comparer that should be used to determine if values are equal. |
||
216 | * |
||
217 | * @since 0.6 |
||
218 | * |
||
219 | * @param ValueComparer $comparer |
||
220 | */ |
||
221 | 2 | public function setValueComparer( ValueComparer $comparer ) { |
|
224 | |||
225 | } |
||
226 |