-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDeepComparator.php
More file actions
225 lines (195 loc) · 6.11 KB
/
DeepComparator.php
File metadata and controls
225 lines (195 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace Icecave\Parity\Comparator;
use ReflectionObject;
/**
* A comparator that performs deep comparison of PHP arrays and objects.
*
* Comparison of objects is recursion-safe.
*/
class DeepComparator implements Comparator
{
/**
* If $relaxClassComparisons is true, class names are not included in the
* comparison of objects.
*
* @param Comparator $fallbackComparator The comparator to use when the operands are not arrays or objects.
* @param bool $relaxClassComparisons True to relax class name comparisons; false to compare strictly.
*/
public function __construct(
Comparator $fallbackComparator,
$relaxClassComparisons = false
) {
$this->fallbackComparator = $fallbackComparator;
$this->relaxClassComparisons = $relaxClassComparisons;
}
/**
* Fetch the fallback comparator.
*
* @return Comparator The comparator to use when the operands are not arrays or objects.
*/
public function fallbackComparator(): Comparator
{
return $this->fallbackComparator;
}
/**
* Compare two values, yielding a result according to the following table:
*
* +--------------------+---------------+
* | Condition | Result |
* +--------------------+---------------+
* | $this == $value | $result === 0 |
* | $this < $value | $result < 0 |
* | $this > $value | $result > 0 |
* +--------------------+---------------+
*
* A deep comparison is performed if both operands are arrays, or both are
* objects; otherwise, the fallback comparator is used.
*
* @param mixed $lhs The first value to compare.
* @param mixed $rhs The second value to compare.
*
* @return int The result of the comparison.
*/
public function compare($lhs, $rhs): int
{
$visitationContext = [];
return $this->compareValue($lhs, $rhs, $visitationContext);
}
/**
* An alias for compare().
*
* @param mixed $lhs The first value to compare.
* @param mixed $rhs The second value to compare.
*
* @return int The result of the comparison.
*/
public function __invoke($lhs, $rhs): int
{
return $this->compare($lhs, $rhs);
}
/**
* @param mixed $lhs
* @param mixed $rhs
* @param mixed &$visitationContext
*
* @return int The result of the comparison.
*/
protected function compareValue($lhs, $rhs, &$visitationContext): int
{
if (is_array($lhs) && is_array($rhs)) {
return $this->compareArray($lhs, $rhs, $visitationContext);
} elseif (is_object($lhs) && is_object($rhs)) {
return $this->compareObject($lhs, $rhs, $visitationContext);
}
return $this->fallbackComparator()->compare($lhs, $rhs);
}
/**
* @param array $lhs
* @param array $rhs
* @param mixed &$visitationContext
*
* @return int The result of the comparison.
*/
protected function compareArray(array $lhs, array $rhs, &$visitationContext): int
{
reset($lhs);
reset($rhs);
while (true) {
$lk = key($lhs);
$rk = key($rhs);
if ($lk === null && $rk === null) {
break;
} elseif ($lk === null) {
return -1;
} elseif ($rk === null) {
return +1;
}
$cmp = $this->compareValue($lk, $rk, $visitationContext);
if ($cmp !== 0) {
return $cmp;
}
$lv = current($lhs);
$rv = current($rhs);
$cmp = $this->compareValue($lv, $rv, $visitationContext);
if ($cmp !== 0) {
return $cmp;
}
next($lhs);
next($rhs);
}
return 0;
}
/**
* @param object $lhs
* @param object $rhs
* @param mixed &$visitationContext
*
* @return int The result of the comparison.
*/
protected function compareObject($lhs, $rhs, &$visitationContext): int
{
if ($lhs === $rhs) {
return 0;
} elseif ($this->isNestedComparison($lhs, $rhs, $visitationContext)) {
return strcmp(
spl_object_hash($lhs),
spl_object_hash($rhs)
);
} elseif (!$this->relaxClassComparisons) {
$diff = strcmp(get_class($lhs), get_class($rhs));
if ($diff !== 0) {
return $diff;
}
}
return $this->compareArray(
$this->objectProperties($lhs, $visitationContext),
$this->objectProperties($rhs, $visitationContext),
$visitationContext
);
}
/**
* @param object $object
* @param mixed &$visitationContext
*
* @return array<string,mixed>
*/
protected function objectProperties($object, &$visitationContext): array
{
$properties = [];
$reflector = new ReflectionObject($object);
while ($reflector) {
foreach ($reflector->getProperties() as $property) {
if ($property->isStatic()) {
continue;
}
$key = sprintf(
'%s::%s',
$property->getDeclaringClass()->getName(),
$property->getName()
);
$property->setAccessible(true);
$properties[$key] = $property->getValue($object);
}
$reflector = $reflector->getParentClass();
}
return $properties;
}
/**
* @param mixed $lhs
* @param mixed $rhs
* @param mixed &$visitationContext
*
* @return bool
*/
protected function isNestedComparison($lhs, $rhs, &$visitationContext): bool
{
$key = spl_object_hash($lhs) . ':' . spl_object_hash($rhs);
if (array_key_exists($key, $visitationContext)) {
return true;
}
$visitationContext[$key] = true;
return false;
}
private $fallbackComparator;
private $relaxClassComparisons;
}