5
5
class JsonDiff
6
6
{
7
7
const SKIP_REARRANGE_ARRAY = 1 ;
8
+ const STOP_ON_DIFF = 2 ;
8
9
9
10
private $ options = 0 ;
10
11
private $ original ;
@@ -42,6 +43,15 @@ public function __construct($original, $new, $options = 0)
42
43
$ this ->rearranged = $ this ->rearrange ();
43
44
}
44
45
46
+ /**
47
+ * Returns total number of differences
48
+ * @return int
49
+ */
50
+ public function getDiffCnt ()
51
+ {
52
+ return $ this ->addedCnt + $ this ->modifiedCnt + $ this ->removedCnt ;
53
+ }
54
+
45
55
/**
46
56
* Returns removals as partial value of original.
47
57
* @return mixed
@@ -157,6 +167,9 @@ private function process($original, $new)
157
167
$ this ->modifiedPaths [] = $ this ->path ;
158
168
JsonProcessor::pushByPath ($ this ->modifiedOriginal , $ this ->path , $ original );
159
169
JsonProcessor::pushByPath ($ this ->modifiedNew , $ this ->path , $ new );
170
+ if ($ this ->options & self ::STOP_ON_DIFF ) {
171
+ return ;
172
+ }
160
173
}
161
174
return $ new ;
162
175
}
@@ -184,6 +197,9 @@ private function process($original, $new)
184
197
$ this ->removedCnt ++;
185
198
$ this ->removedPaths [] = $ this ->path ;
186
199
JsonProcessor::pushByPath ($ this ->removed , $ this ->path , $ originalValue );
200
+ if ($ this ->options & self ::STOP_ON_DIFF ) {
201
+ return ;
202
+ }
187
203
}
188
204
$ this ->path = $ path ;
189
205
}
@@ -195,6 +211,9 @@ private function process($original, $new)
195
211
JsonProcessor::pushByPath ($ this ->added , $ path , $ value );
196
212
$ this ->addedCnt ++;
197
213
$ this ->addedPaths [] = $ path ;
214
+ if ($ this ->options & self ::STOP_ON_DIFF ) {
215
+ return ;
216
+ }
198
217
}
199
218
200
219
return is_array ($ new ) ? $ newOrdered : (object )$ newOrdered ;
0 commit comments