@@ -61,7 +61,7 @@ public static function import(array $data)
61
61
}
62
62
63
63
if (!is_object ($ operation )) {
64
- throw new Exception ( 'Invalid patch operation - should be a JSON object ' );
64
+ throw new Exception ('Invalid patch operation - should be a JSON object ' );
65
65
}
66
66
67
67
if (!isset ($ operation ->op )) {
@@ -145,32 +145,32 @@ public function apply(&$original, $stopOnError = true)
145
145
$ errors = array ();
146
146
foreach ($ this ->operations as $ operation ) {
147
147
try {
148
- $ pathItems = JsonPointer:: splitPath ($ operation-> path );
148
+ $ pathItems = $ this -> splitPath ($ operation, ' path ' );
149
149
switch (true ) {
150
150
case $ operation instanceof Add:
151
- JsonPointer:: add ($ original , $ pathItems , $ operation ->value , $ this -> flags );
151
+ $ this -> add ($ operation , $ original , $ pathItems , $ operation ->value );
152
152
break ;
153
153
case $ operation instanceof Copy:
154
- $ fromItems = JsonPointer:: splitPath ($ operation-> from );
155
- $ value = JsonPointer:: get ($ original , $ fromItems );
156
- JsonPointer:: add ($ original , $ pathItems , $ value , $ this -> flags );
154
+ $ fromItems = $ this -> splitPath ($ operation, ' from ' );
155
+ $ value = $ this -> get ($ operation , ' from ' , $ original , $ fromItems );
156
+ $ this -> add ($ operation , $ original , $ pathItems , $ value );
157
157
break ;
158
158
case $ operation instanceof Move:
159
- $ fromItems = JsonPointer:: splitPath ($ operation-> from );
160
- $ value = JsonPointer:: get ($ original , $ fromItems );
161
- JsonPointer:: remove ($ original , $ fromItems , $ this -> flags );
162
- JsonPointer:: add ($ original , $ pathItems , $ value , $ this -> flags );
159
+ $ fromItems = $ this -> splitPath ($ operation, ' from ' );
160
+ $ value = $ this -> get ($ operation , ' from ' , $ original , $ fromItems );
161
+ $ this -> remove ($ operation , ' from ' , $ original , $ fromItems );
162
+ $ this -> add ($ operation , $ original , $ pathItems , $ value );
163
163
break ;
164
164
case $ operation instanceof Remove:
165
- JsonPointer:: remove ($ original , $ pathItems , $ this -> flags );
165
+ $ this -> remove ($ operation , ' path ' , $ original , $ pathItems );
166
166
break ;
167
167
case $ operation instanceof Replace:
168
- JsonPointer:: get ($ original , $ pathItems );
169
- JsonPointer:: remove ($ original , $ pathItems , $ this -> flags );
170
- JsonPointer:: add ($ original , $ pathItems , $ operation ->value , $ this -> flags );
168
+ $ this -> get ($ operation , ' path ' , $ original , $ pathItems );
169
+ $ this -> remove ($ operation , ' path ' , $ original , $ pathItems );
170
+ $ this -> add ($ operation , $ original , $ pathItems , $ operation ->value );
171
171
break ;
172
172
case $ operation instanceof Test:
173
- $ value = JsonPointer:: get ($ original , $ pathItems );
173
+ $ value = $ this -> get ($ operation , ' path ' , $ original , $ pathItems );
174
174
$ diff = new JsonDiff ($ operation ->value , $ value ,
175
175
JsonDiff::STOP_ON_DIFF );
176
176
if ($ diff ->getDiffCnt () !== 0 ) {
@@ -188,4 +188,41 @@ public function apply(&$original, $stopOnError = true)
188
188
}
189
189
return $ errors ;
190
190
}
191
+
192
+ private function splitPath (OpPath $ operation , $ field )
193
+ {
194
+ try {
195
+ return JsonPointer::splitPath ($ operation ->$ field );
196
+ } catch (Exception $ exception ) {
197
+ throw new PathException ($ exception ->getMessage (), $ operation , $ field , $ exception ->getCode ());
198
+ }
199
+ }
200
+
201
+ private function add (OpPath $ operation , &$ original , array $ pathItems , $ value )
202
+ {
203
+ try {
204
+ JsonPointer::add ($ original , $ pathItems , $ value , $ this ->flags );
205
+ } catch (Exception $ exception ) {
206
+ throw new PathException ($ exception ->getMessage (), $ operation , 'path ' , $ exception ->getCode ());
207
+ }
208
+ }
209
+
210
+ private function get (OpPath $ operation , $ field , $ original , array $ pathItems )
211
+ {
212
+ try {
213
+ return JsonPointer::get ($ original , $ pathItems );
214
+ } catch (Exception $ exception ) {
215
+ throw new PathException ($ exception ->getMessage (), $ operation , $ field , $ exception ->getCode ());
216
+ }
217
+ }
218
+
219
+ private function remove ($ operation , $ field , &$ original , array $ pathItems )
220
+ {
221
+ try {
222
+ JsonPointer::remove ($ original , $ pathItems , $ this ->flags );
223
+ } catch (Exception $ exception ) {
224
+ throw new PathException ($ exception ->getMessage (), $ operation , $ field , $ exception ->getCode ());
225
+ }
226
+ }
227
+
191
228
}
0 commit comments