@@ -106,45 +106,57 @@ public function jsonSerialize()
106106
107107 /**
108108 * @param mixed $original
109+ * @param bool $stopOnError
110+ * @return Exception[] array of errors
109111 * @throws Exception
110112 */
111- public function apply (&$ original )
113+ public function apply (&$ original, $ stopOnError = true )
112114 {
115+ $ errors = array ();
113116 foreach ($ this ->operations as $ operation ) {
114- $ pathItems = JsonPointer::splitPath ($ operation ->path );
115- switch (true ) {
116- case $ operation instanceof Add:
117- JsonPointer::add ($ original , $ pathItems , $ operation ->value , false );
118- break ;
119- case $ operation instanceof Copy:
120- $ fromItems = JsonPointer::splitPath ($ operation ->from );
121- $ value = JsonPointer::get ($ original , $ fromItems );
122- JsonPointer::add ($ original , $ pathItems , $ value , false );
123- break ;
124- case $ operation instanceof Move:
125- $ fromItems = JsonPointer::splitPath ($ operation ->from );
126- $ value = JsonPointer::get ($ original , $ fromItems );
127- JsonPointer::remove ($ original , $ fromItems );
128- JsonPointer::add ($ original , $ pathItems , $ value , false );
129- break ;
130- case $ operation instanceof Remove:
131- JsonPointer::remove ($ original , $ pathItems );
132- break ;
133- case $ operation instanceof Replace:
134- JsonPointer::get ($ original , $ pathItems );
135- JsonPointer::remove ($ original , $ pathItems );
136- JsonPointer::add ($ original , $ pathItems , $ operation ->value , false );
137- break ;
138- case $ operation instanceof Test:
139- $ value = JsonPointer::get ($ original , $ pathItems );
140- $ diff = new JsonDiff ($ operation ->value , $ value ,
141- JsonDiff::STOP_ON_DIFF );
142- if ($ diff ->getDiffCnt () !== 0 ) {
143- throw new Exception ('Test operation ' . json_encode ($ operation , JSON_UNESCAPED_SLASHES )
144- . ' failed: ' . json_encode ($ value ));
145- }
146- break ;
117+ try {
118+ $ pathItems = JsonPointer::splitPath ($ operation ->path );
119+ switch (true ) {
120+ case $ operation instanceof Add:
121+ JsonPointer::add ($ original , $ pathItems , $ operation ->value , false );
122+ break ;
123+ case $ operation instanceof Copy:
124+ $ fromItems = JsonPointer::splitPath ($ operation ->from );
125+ $ value = JsonPointer::get ($ original , $ fromItems );
126+ JsonPointer::add ($ original , $ pathItems , $ value , false );
127+ break ;
128+ case $ operation instanceof Move:
129+ $ fromItems = JsonPointer::splitPath ($ operation ->from );
130+ $ value = JsonPointer::get ($ original , $ fromItems );
131+ JsonPointer::remove ($ original , $ fromItems );
132+ JsonPointer::add ($ original , $ pathItems , $ value , false );
133+ break ;
134+ case $ operation instanceof Remove:
135+ JsonPointer::remove ($ original , $ pathItems );
136+ break ;
137+ case $ operation instanceof Replace:
138+ JsonPointer::get ($ original , $ pathItems );
139+ JsonPointer::remove ($ original , $ pathItems );
140+ JsonPointer::add ($ original , $ pathItems , $ operation ->value , false );
141+ break ;
142+ case $ operation instanceof Test:
143+ $ value = JsonPointer::get ($ original , $ pathItems );
144+ $ diff = new JsonDiff ($ operation ->value , $ value ,
145+ JsonDiff::STOP_ON_DIFF );
146+ if ($ diff ->getDiffCnt () !== 0 ) {
147+ throw new Exception ('Test operation ' . json_encode ($ operation , JSON_UNESCAPED_SLASHES )
148+ . ' failed: ' . json_encode ($ value ));
149+ }
150+ break ;
151+ }
152+ } catch (Exception $ exception ) {
153+ if ($ stopOnError ) {
154+ throw $ exception ;
155+ } else {
156+ $ errors [] = $ exception ;
157+ }
147158 }
148159 }
160+ return $ errors ;
149161 }
150162}
0 commit comments