@@ -57,105 +57,28 @@ public function __construct(iterable $subset, bool $strict = false)
57
57
*/
58
58
public function evaluate ($ other , $ description = '' , $ returnResult = false )
59
59
{
60
- // Anonymous function that checks whether the given array is
61
- // associative.
62
- $ is_associative = function (array $ array ): bool {
63
- return \array_reduce (\array_keys ($ array ), function (bool $ carry , $ key ): bool {
64
- return $ carry || \is_string ($ key );
65
- }, false );
66
- };
67
-
68
- // Anonymous function that compares the two given values using either
69
- // strict or loose comparisons.
70
- $ strict = $ this ->strict ;
71
- $ compare = function ($ first , $ second ) use ($ strict ): bool {
72
- return $ strict ? $ first === $ second : $ first == $ second ;
73
- };
74
-
75
- // Anonymous function that sorts the given multidimensional array.
76
- $ deep_sort = function (array &$ array ) use (&$ deep_sort , $ is_associative ): void {
77
- foreach ($ array as &$ value ) {
78
- if (\is_array ($ value )) {
79
- $ deep_sort ($ value );
80
- }
81
- }
82
-
83
- if ($ is_associative ($ array )) {
84
- \ksort ($ array );
85
- } else {
86
- \sort ($ array );
87
- }
88
- };
89
-
90
- $ array_intersect_recursive = function (array $ array , array $ subset ) use (&$ array_intersect_recursive , $ is_associative , $ compare ): array {
91
- $ intersect = [];
92
-
93
- if ($ is_associative ($ subset )) {
94
- // If the subset is an associative array, get the intersection
95
- // while preserving the keys.
96
- foreach ($ subset as $ key => $ subset_value ) {
97
- if (\array_key_exists ($ key , $ array )) {
98
- $ array_value = $ array [$ key ];
99
-
100
- if (\is_array ($ subset_value ) && \is_array ($ array_value )) {
101
- $ intersect [$ key ] = $ array_intersect_recursive ($ array_value , $ subset_value );
102
- } elseif ($ compare ($ subset_value , $ array_value )) {
103
- $ intersect [$ key ] = $ array_value ;
104
- }
105
- }
106
- }
107
- } else {
108
- // If the subset is an indexed array, loop over all entries in
109
- // the haystack and check if they match the ones in the subset.
110
- foreach ($ array as $ array_value ) {
111
- if (\is_array ($ array_value )) {
112
- foreach ($ subset as $ key => $ subset_value ) {
113
- if (\is_array ($ subset_value )) {
114
- $ recursed = $ array_intersect_recursive ($ array_value , $ subset_value );
115
-
116
- if (!empty ($ recursed )) {
117
- $ intersect [$ key ] = $ recursed ;
118
- }
119
- }
120
- }
121
- } else {
122
- foreach ($ subset as $ key => $ subset_value ) {
123
- if (!\is_array ($ subset_value ) && $ compare (
124
- $ subset_value ,
125
- $ array_value
126
- )) {
127
- $ intersect [$ key ] = $ array_value ;
128
-
129
- break ;
130
- }
131
- }
132
- }
133
- }
134
- }
135
-
136
- return $ intersect ;
137
- };
138
-
139
60
//type cast $other & $this->subset as an array to allow
140
61
//support in standard array functions.
141
62
$ other = $ this ->toArray ($ other );
142
63
$ this ->subset = $ this ->toArray ($ this ->subset );
143
64
144
- $ intersect = $ array_intersect_recursive ($ other , $ this ->subset );
145
- $ deep_sort ($ intersect );
146
- $ deep_sort ($ this ->subset );
65
+ $ patched = \array_replace_recursive ($ other , $ this ->subset );
147
66
148
- $ result = $ compare ($ intersect , $ this ->subset );
67
+ if ($ this ->strict ) {
68
+ $ result = $ other === $ patched ;
69
+ } else {
70
+ $ result = $ other == $ patched ;
71
+ }
149
72
150
73
if ($ returnResult ) {
151
74
return $ result ;
152
75
}
153
76
154
77
if (!$ result ) {
155
78
$ f = new ComparisonFailure (
156
- $ this -> subset ,
79
+ $ patched ,
157
80
$ other ,
158
- \print_r ($ this -> subset , true ),
81
+ \print_r ($ patched , true ),
159
82
\print_r ($ other , true )
160
83
);
161
84
0 commit comments