1 | <?php |
||
23 | class MapPatcher extends ThrowingPatcher { |
||
24 | |||
25 | /** |
||
26 | * @var Patcher |
||
27 | */ |
||
28 | private $listPatcher; |
||
29 | |||
30 | /** |
||
31 | * @var ValueComparer|null |
||
32 | */ |
||
33 | private $comparer = null; |
||
34 | |||
35 | /** |
||
36 | * @since 0.4 |
||
37 | * |
||
38 | * @param bool $throwErrors |
||
39 | * @param Patcher|null $listPatcher The patcher that will be used for lists in the value |
||
40 | */ |
||
41 | 13 | public function __construct( bool $throwErrors = false, Patcher $listPatcher = null ) { |
|
46 | |||
47 | /** |
||
48 | * @see Patcher::patch |
||
49 | * |
||
50 | * Applies the provided diff to the provided array and returns the result. |
||
51 | * The array is treated as a map, ie keys are held into account. |
||
52 | * |
||
53 | * It is possible to pass in non-associative diffs (those for which isAssociative) |
||
54 | * returns false, however the likely intended behavior can be obtained via |
||
55 | * a list patcher. |
||
56 | * |
||
57 | * @since 0.4 |
||
58 | * |
||
59 | * @param array $base |
||
60 | * @param Diff $diff |
||
61 | * |
||
62 | * @return array |
||
63 | * @throws PatcherException |
||
64 | */ |
||
65 | 27 | public function patch( array $base, Diff $diff ): array { |
|
72 | |||
73 | /** |
||
74 | * @param array &$base |
||
75 | * @param int|string $key |
||
76 | * @param DiffOp $diffOp |
||
77 | * |
||
78 | * @throws PatcherException |
||
79 | */ |
||
80 | 23 | private function applyOperation( array &$base, $key, DiffOp $diffOp ) { |
|
81 | 23 | if ( $diffOp instanceof DiffOpAdd ) { |
|
82 | 11 | $this->applyDiffOpAdd( $base, $key, $diffOp ); |
|
83 | } |
||
84 | 19 | elseif ( $diffOp instanceof DiffOpChange ) { |
|
85 | 9 | $this->applyDiffOpChange( $base, $key, $diffOp ); |
|
86 | } |
||
87 | 13 | elseif ( $diffOp instanceof DiffOpRemove ) { |
|
88 | 9 | $this->applyDiffOpRemove( $base, $key, $diffOp ); |
|
89 | } |
||
90 | 4 | elseif ( $diffOp instanceof Diff ) { |
|
91 | 3 | $this->applyDiff( $base, $key, $diffOp ); |
|
92 | } |
||
93 | else { |
||
94 | 1 | $this->handleError( 'Unknown diff operation cannot be applied to map element' ); |
|
95 | } |
||
96 | 23 | } |
|
97 | |||
98 | /** |
||
99 | * @param array &$base |
||
100 | * @param int|string $key |
||
101 | * @param DiffOpAdd $diffOp |
||
102 | * |
||
103 | * @throws PatcherException |
||
104 | */ |
||
105 | 11 | private function applyDiffOpAdd( array &$base, $key, DiffOpAdd $diffOp ) { |
|
113 | |||
114 | /** |
||
115 | * @param array &$base |
||
116 | * @param int|string $key |
||
117 | * @param DiffOpRemove $diffOp |
||
118 | * |
||
119 | * @throws PatcherException |
||
120 | */ |
||
121 | 9 | private function applyDiffOpRemove( array &$base, $key, DiffOpRemove $diffOp ) { |
|
134 | |||
135 | /** |
||
136 | * @param array &$base |
||
137 | * @param int|string $key |
||
138 | * @param DiffOpChange $diffOp |
||
139 | * |
||
140 | * @throws PatcherException |
||
141 | */ |
||
142 | 9 | private function applyDiffOpChange( array &$base, $key, DiffOpChange $diffOp ) { |
|
155 | |||
156 | /** |
||
157 | * @param array &$base |
||
158 | * @param int|string $key |
||
159 | * @param Diff $diffOp |
||
160 | * |
||
161 | * @throws PatcherException |
||
162 | */ |
||
163 | 3 | private function applyDiff( &$base, $key, Diff $diffOp ) { |
|
175 | |||
176 | /** |
||
177 | * @param array &$base |
||
178 | * @param int|string $key |
||
179 | * @param Diff $diffOp |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | 3 | private function isAttemptToModifyNotExistingElement( $base, $key, Diff $diffOp ): bool { |
|
187 | |||
188 | /** |
||
189 | * @param array $base |
||
190 | * @param Diff $diff |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | 3 | private function patchMapOrList( array $base, Diff $diff ): array { |
|
201 | |||
202 | /** |
||
203 | * @param mixed $firstValue |
||
204 | * @param mixed $secondValue |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | 11 | private function valuesAreEqual( $firstValue, $secondValue ): bool { |
|
215 | |||
216 | /** |
||
217 | * Sets the value comparer that should be used to determine if values are equal. |
||
218 | * |
||
219 | * @since 0.6 |
||
220 | * |
||
221 | * @param ValueComparer $comparer |
||
222 | */ |
||
223 | 2 | public function setValueComparer( ValueComparer $comparer ) { |
|
226 | |||
227 | } |
||
228 |