-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathObjectIdentityComparator.php
More file actions
74 lines (66 loc) · 1.92 KB
/
ObjectIdentityComparator.php
File metadata and controls
74 lines (66 loc) · 1.92 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
<?php
namespace Icecave\Parity\Comparator;
/**
* A comparator that compares objects by identity.
*/
class ObjectIdentityComparator implements Comparator
{
/**
* @param Comparator $fallbackComparator The comparator to use for non-objects.
*/
public function __construct(Comparator $fallbackComparator)
{
$this->fallbackComparator = $fallbackComparator;
}
/**
* Fetch the fallback comparator.
*
* @return Comparator The comparator to use for non-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 |
* +--------------------+---------------+
*
* If either of the operands is not an object 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
{
if (!is_object($lhs) || !is_object($rhs)) {
return $this->fallbackComparator()->compare($lhs, $rhs);
}
return strcmp(
spl_object_hash($lhs),
spl_object_hash($rhs)
);
}
/**
* 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);
}
private $fallbackComparator;
}