@@ -79,10 +79,7 @@ class JsonDiff
79
79
private $ jsonPatch ;
80
80
81
81
/** @var JsonHash */
82
- private $ jsonHashOriginal ;
83
-
84
- /** @var JsonHash */
85
- private $ jsonHashNew ;
82
+ private $ jsonHash ;
86
83
87
84
/**
88
85
* @param mixed $original
@@ -410,7 +407,7 @@ private function rearrangeArray(array $original, array $new)
410
407
/** @var mixed[string] $f */
411
408
$ f = get_object_vars ($ first );
412
409
foreach ($ f as $ key => $ value ) {
413
- if (is_array ($ value ) || $ value instanceof \stdClass ) {
410
+ if (is_array ($ value )) {
414
411
continue ;
415
412
}
416
413
@@ -425,11 +422,19 @@ private function rearrangeArray(array $original, array $new)
425
422
break ;
426
423
}
427
424
$ value = $ item ->$ key ;
428
- if ($ value instanceof \stdClass || is_array ($ value )) {
425
+ if (is_array ($ value )) {
429
426
$ keyIsUnique = false ;
430
427
break ;
431
428
}
432
429
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
+
433
438
if (isset ($ uniqueIdx [$ value ])) {
434
439
$ keyIsUnique = false ;
435
440
break ;
@@ -461,10 +466,18 @@ private function rearrangeArray(array $original, array $new)
461
466
462
467
$ value = $ item ->$ uniqueKey ;
463
468
464
- if ($ value instanceof \stdClass || is_array ($ value )) {
469
+ if (is_array ($ value )) {
465
470
return $ new ;
466
471
}
467
472
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
+
468
481
469
482
if (isset ($ uniqueIdx [$ value ])) {
470
483
$ idx = $ uniqueIdx [$ value ];
@@ -490,26 +503,24 @@ private function rearrangeArray(array $original, array $new)
490
503
}
491
504
492
505
ksort ($ newRearranged );
493
- $ newRearranged = array_values ($ newRearranged );
494
506
return $ newRearranged ;
495
507
}
496
508
497
509
private function rearrangeEqualItems (array $ original , array $ new )
498
510
{
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 );
502
513
}
503
514
504
515
$ origIdx = [];
505
516
foreach ($ original as $ i => $ item ) {
506
- $ hash = $ this ->jsonHashOriginal ->xorHash ($ item );
517
+ $ hash = $ this ->jsonHash ->xorHash ($ item );
507
518
$ origIdx [$ hash ][] = $ i ;
508
519
}
509
520
510
521
$ newIdx = [];
511
522
foreach ($ new as $ i => $ item ) {
512
- $ hash = $ this ->jsonHashNew ->xorHash ($ item );
523
+ $ hash = $ this ->jsonHash ->xorHash ($ item );
513
524
$ newIdx [$ i ] = $ hash ;
514
525
}
515
526
@@ -535,7 +546,6 @@ private function rearrangeEqualItems(array $original, array $new)
535
546
}
536
547
537
548
ksort ($ newRearranged );
538
- $ newRearranged = array_values ($ newRearranged );
539
549
540
550
return $ newRearranged ;
541
551
}
0 commit comments