@@ -30,62 +30,50 @@ public function __construct(SerializerInterface $serializer)
30
30
/**
31
31
* @inheritDoc
32
32
*/
33
- public function isEqual (?AddressInterface $ shippingAddress , ?AddressInterface $ billingAddress ): bool
33
+ public function isEqual (?AddressInterface $ address1 , ?AddressInterface $ address2 ): bool
34
34
{
35
- if ($ shippingAddress === null || $ billingAddress === null ) {
35
+ if ($ address1 === null || $ address2 === null ) {
36
36
return false ;
37
37
}
38
38
39
- if ($ shippingAddress ->getCustomerAddressId () !== null &&
40
- $ billingAddress ->getCustomerAddressId () !== null
39
+ if ($ address1 ->getCustomerAddressId () !== null &&
40
+ $ address2 ->getCustomerAddressId () !== null
41
41
) {
42
- return ((int )$ shippingAddress ->getCustomerAddressId () ===
43
- (int )$ billingAddress ->getCustomerAddressId ());
42
+ return ((int )$ address1 ->getCustomerAddressId () ===
43
+ (int )$ address2 ->getCustomerAddressId ());
44
44
} else {
45
- $ shippingAddressData = $ shippingAddress ->getData ();
46
- $ billingAddressData = $ billingAddress ->getData ();
47
- $ billingKeys = array_flip (array_keys ($ billingAddressData ));
48
- $ shippingData = array_intersect_key ($ shippingAddressData , $ billingKeys );
45
+ $ addressKeys = array_intersect_key ($ address1 ->getData (), $ address2 ->getData ());
49
46
$ removeKeys = ['address_type ' , 'region_code ' , 'save_in_address_book ' ];
50
- $ billingData = array_diff_key ($ billingAddressData , array_flip ($ removeKeys ));
51
- $ diff = $ this ->arrayDiffAssocRecursive ($ billingData , $ shippingData );
47
+ $ addressKeys = array_diff_key ($ addressKeys , array_flip ($ removeKeys ));
48
+
49
+ $ address1Data = array_intersect_key ($ address1 ->getData (), $ addressKeys );
50
+ $ address2Data = array_intersect_key ($ address2 ->getData (), $ addressKeys );
51
+ $ diff = $ this ->computeArrayDifference ($ address1Data , $ address2Data );
52
52
return empty ($ diff );
53
53
}
54
54
}
55
55
56
56
/**
57
- * Compare two arrays
57
+ * Computing the difference of two arrays
58
58
*
59
59
* @param array $array1
60
60
* @param array $array2
61
61
* @return array
62
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
63
62
*/
64
- private function arrayDiffAssocRecursive (array $ array1 , array $ array2 ): array
63
+ private function computeArrayDifference (array $ array1 , array $ array2 ): array
65
64
{
66
- $ difference = [];
67
- foreach ($ array1 as $ key => $ value ) {
68
- if (is_array ($ value )) {
69
- if (!isset ($ array2 [$ key ])) {
70
- $ difference [$ key ] = $ value ;
71
- } elseif (!is_array ($ array2 [$ key ])) {
72
- $ difference [$ key ] = $ value ;
73
- } else {
74
- $ new_diff = $ this ->arrayDiffAssocRecursive ($ value , $ array2 [$ key ]);
75
- if (!empty ($ new_diff )) {
76
- $ difference [$ key ] = $ new_diff ;
77
- }
65
+ return array_udiff_assoc (
66
+ $ array1 ,
67
+ $ array2 ,
68
+ function ($ el1 , $ el2 ) {
69
+ if (is_object ($ el1 ) || is_array ($ el1 )) {
70
+ $ el1 = $ this ->serializer ->serialize ($ el1 );
78
71
}
79
- } else {
80
- $ str1 = is_object ($ array2 [$ key ]) ? $ this ->serializer ->serialize ($ array2 [$ key ]) : (string )$ array2 [$ key ];
81
- $ str2 = is_object ($ array2 [$ key ]) ? $ this ->serializer ->serialize ($ value ) : (string )$ value ;
82
- if ((!empty ($ str1 ) && !empty ($ str2 ) && $ str1 !== $ str2 )
83
- || (!empty ($ str1 ) && empty ($ str2 ))
84
- || (!empty ($ str1 ) && empty ($ str2 ))) {
85
- $ difference [$ key ] = $ str2 ;
72
+ if (is_object ($ el2 ) || is_array ($ el2 )) {
73
+ $ el2 = $ this ->serializer ->serialize ($ el2 );
86
74
}
75
+ return strcmp ((string )$ el1 , (string )$ el2 );
87
76
}
88
- }
89
- return $ difference ;
77
+ );
90
78
}
91
79
}
0 commit comments