@@ -79,10 +79,7 @@ class JsonDiff
7979 private $ jsonPatch ;
8080
8181 /** @var JsonHash */
82- private $ jsonHashOriginal ;
83-
84- /** @var JsonHash */
85- private $ jsonHashNew ;
82+ private $ jsonHash ;
8683
8784 /**
8885 * @param mixed $original
@@ -410,7 +407,7 @@ private function rearrangeArray(array $original, array $new)
410407 /** @var mixed[string] $f */
411408 $ f = get_object_vars ($ first );
412409 foreach ($ f as $ key => $ value ) {
413- if (is_array ($ value ) || $ value instanceof \stdClass ) {
410+ if (is_array ($ value )) {
414411 continue ;
415412 }
416413
@@ -425,11 +422,19 @@ private function rearrangeArray(array $original, array $new)
425422 break ;
426423 }
427424 $ value = $ item ->$ key ;
428- if ($ value instanceof \stdClass || is_array ($ value )) {
425+ if (is_array ($ value )) {
429426 $ keyIsUnique = false ;
430427 break ;
431428 }
432429
430+ if ($ value instanceof \stdClass) {
431+ if ($ this ->jsonHash === null ) {
432+ $ this ->jsonHash = new JsonHash ($ this ->options );
433+ }
434+
435+ $ value = $ this ->jsonHash ->xorHash ($ value );
436+ }
437+
433438 if (isset ($ uniqueIdx [$ value ])) {
434439 $ keyIsUnique = false ;
435440 break ;
@@ -461,10 +466,18 @@ private function rearrangeArray(array $original, array $new)
461466
462467 $ value = $ item ->$ uniqueKey ;
463468
464- if ($ value instanceof \stdClass || is_array ($ value )) {
469+ if (is_array ($ value )) {
465470 return $ new ;
466471 }
467472
473+ if ($ value instanceof \stdClass) {
474+ if ($ this ->jsonHash === null ) {
475+ $ this ->jsonHash = new JsonHash ($ this ->options );
476+ }
477+
478+ $ value = $ this ->jsonHash ->xorHash ($ value );
479+ }
480+
468481
469482 if (isset ($ uniqueIdx [$ value ])) {
470483 $ idx = $ uniqueIdx [$ value ];
@@ -490,26 +503,24 @@ private function rearrangeArray(array $original, array $new)
490503 }
491504
492505 ksort ($ newRearranged );
493- $ newRearranged = array_values ($ newRearranged );
494506 return $ newRearranged ;
495507 }
496508
497509 private function rearrangeEqualItems (array $ original , array $ new )
498510 {
499- if ($ this ->jsonHashOriginal === null ) {
500- $ this ->jsonHashOriginal = new JsonHash ($ this ->options );
501- $ this ->jsonHashNew = new JsonHash ($ this ->options );
511+ if ($ this ->jsonHash === null ) {
512+ $ this ->jsonHash = new JsonHash ($ this ->options );
502513 }
503514
504515 $ origIdx = [];
505516 foreach ($ original as $ i => $ item ) {
506- $ hash = $ this ->jsonHashOriginal ->xorHash ($ item );
517+ $ hash = $ this ->jsonHash ->xorHash ($ item );
507518 $ origIdx [$ hash ][] = $ i ;
508519 }
509520
510521 $ newIdx = [];
511522 foreach ($ new as $ i => $ item ) {
512- $ hash = $ this ->jsonHashNew ->xorHash ($ item );
523+ $ hash = $ this ->jsonHash ->xorHash ($ item );
513524 $ newIdx [$ i ] = $ hash ;
514525 }
515526
@@ -535,7 +546,6 @@ private function rearrangeEqualItems(array $original, array $new)
535546 }
536547
537548 ksort ($ newRearranged );
538- $ newRearranged = array_values ($ newRearranged );
539549
540550 return $ newRearranged ;
541551 }
0 commit comments