-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMapPatcher.php
223 lines (194 loc) · 5.32 KB
/
MapPatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
declare( strict_types = 1 );
namespace Diff\Patcher;
use Diff\Comparer\StrictComparer;
use Diff\Comparer\ValueComparer;
use Diff\DiffOp\Diff\Diff;
use Diff\DiffOp\DiffOp;
use Diff\DiffOp\DiffOpAdd;
use Diff\DiffOp\DiffOpChange;
use Diff\DiffOp\DiffOpRemove;
/**
* Map patcher.
*
* @since 0.4
*
* @license BSD-3-Clause
* @author Jeroen De Dauw < [email protected] >
*/
class MapPatcher extends ThrowingPatcher {
/**
* @var Patcher
*/
private $listPatcher;
/**
* @var ValueComparer|null
*/
private $comparer = null;
/**
* @since 0.4
*
* @param bool $throwErrors
* @param Patcher|null $listPatcher The patcher that will be used for lists in the value
*/
public function __construct( bool $throwErrors = false, ?Patcher $listPatcher = null ) {
parent::__construct( $throwErrors );
$this->listPatcher = $listPatcher ?: new ListPatcher( $throwErrors );
}
/**
* @see Patcher::patch
*
* Applies the provided diff to the provided array and returns the result.
* The array is treated as a map, ie keys are held into account.
*
* It is possible to pass in non-associative diffs (those for which isAssociative)
* returns false, however the likely intended behavior can be obtained via
* a list patcher.
*
* @since 0.4
*
* @param array $base
* @param Diff $diff
*
* @return array
* @throws PatcherException
*/
public function patch( array $base, Diff $diff ): array {
foreach ( $diff as $key => $diffOp ) {
$this->applyOperation( $base, $key, $diffOp );
}
return $base;
}
/**
* @param array &$base
* @param int|string $key
* @param DiffOp $diffOp
*
* @throws PatcherException
*/
private function applyOperation( array &$base, $key, DiffOp $diffOp ) {
if ( $diffOp instanceof DiffOpAdd ) {
$this->applyDiffOpAdd( $base, $key, $diffOp );
} elseif ( $diffOp instanceof DiffOpChange ) {
$this->applyDiffOpChange( $base, $key, $diffOp );
} elseif ( $diffOp instanceof DiffOpRemove ) {
$this->applyDiffOpRemove( $base, $key, $diffOp );
} elseif ( $diffOp instanceof Diff ) {
$this->applyDiff( $base, $key, $diffOp );
} else {
$this->handleError( 'Unknown diff operation cannot be applied to map element' );
}
}
/**
* @param array &$base
* @param int|string $key
* @param DiffOpAdd $diffOp
*
* @throws PatcherException
*/
private function applyDiffOpAdd( array &$base, $key, DiffOpAdd $diffOp ) {
if ( array_key_exists( $key, $base ) ) {
$this->handleError( 'Cannot add an element already present in a map' );
return;
}
$base[$key] = $diffOp->getNewValue();
}
/**
* @param array &$base
* @param int|string $key
* @param DiffOpRemove $diffOp
*
* @throws PatcherException
*/
private function applyDiffOpRemove( array &$base, $key, DiffOpRemove $diffOp ) {
if ( !array_key_exists( $key, $base ) ) {
$this->handleError( 'Cannot do a non-add operation with an element not present in a map' );
return;
}
if ( !$this->valuesAreEqual( $base[$key], $diffOp->getOldValue() ) ) {
$this->handleError( 'Tried removing a map value that mismatches the current value' );
return;
}
unset( $base[$key] );
}
/**
* @param array &$base
* @param int|string $key
* @param DiffOpChange $diffOp
*
* @throws PatcherException
*/
private function applyDiffOpChange( array &$base, $key, DiffOpChange $diffOp ) {
if ( !array_key_exists( $key, $base ) ) {
$this->handleError( 'Cannot do a non-add operation with an element not present in a map' );
return;
}
if ( !$this->valuesAreEqual( $base[$key], $diffOp->getOldValue() ) ) {
$this->handleError( 'Tried changing a map value from an invalid source value' );
return;
}
$base[$key] = $diffOp->getNewValue();
}
/**
* @param array &$base
* @param int|string $key
* @param Diff $diffOp
*
* @throws PatcherException
*/
private function applyDiff( &$base, $key, Diff $diffOp ) {
if ( $this->isAttemptToModifyNotExistingElement( $base, $key, $diffOp ) ) {
$this->handleError( 'Cannot apply a diff with non-add operations to an element not present in a map' );
return;
}
if ( !array_key_exists( $key, $base ) ) {
$base[$key] = [];
}
$base[$key] = $this->patchMapOrList( $base[$key], $diffOp );
}
/**
* @param array $base
* @param int|string $key
* @param Diff $diffOp
*
* @return bool
*/
private function isAttemptToModifyNotExistingElement( $base, $key, Diff $diffOp ): bool {
return !array_key_exists( $key, $base )
&& ( $diffOp->getChanges() !== [] || $diffOp->getRemovals() !== [] );
}
/**
* @param array $base
* @param Diff $diff
*
* @return array
*/
private function patchMapOrList( array $base, Diff $diff ): array {
if ( $diff->looksAssociative() ) {
return $this->patch( $base, $diff );
}
return $this->listPatcher->patch( $base, $diff );
}
/**
* @param mixed $firstValue
* @param mixed $secondValue
*
* @return bool
*/
private function valuesAreEqual( $firstValue, $secondValue ): bool {
if ( $this->comparer === null ) {
$this->comparer = new StrictComparer();
}
return $this->comparer->valuesAreEqual( $firstValue, $secondValue );
}
/**
* Sets the value comparer that should be used to determine if values are equal.
*
* @since 0.6
*
* @param ValueComparer $comparer
*/
public function setValueComparer( ValueComparer $comparer ) {
$this->comparer = $comparer;
}
}