Repository navigation
Expand file tree
/
Copy pathObjects.php
More file actions
159 lines (135 loc) · 4.56 KB
/
Copy pathObjects.php
File metadata and controls
159 lines (135 loc) · 4.56 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
<?php
declare(strict_types=1);
namespace Pharaonic\Diff\Support;
use Closure;
use DateTimeInterface;
use JsonSerializable;
use Pharaonic\Diff\DiffOptions;
use Pharaonic\Diff\Exceptions\UnsupportedValueException;
use Pharaonic\Diff\Path;
use stdClass;
/**
* How objects are compared: a comparator claims a pair of instances of its class, and every other
* object is normalized into a scalar, null or array by a registered normalizer, jsonSerialize()
* or, for stdClass, its public properties.
*
* The last comparator or normalizer registered for a matching class wins, and anything registered
* wins over the built-in DateTimeInterface comparator, which treats two dates as equal when they
* are the same instant, to the microsecond, whatever their time zones.
*
* @internal
*/
final class Objects
{
/**
* @var list<array{class-string, Closure}>
*/
private array $comparators;
/**
* @var list<array{class-string, Closure}>
*/
private array $normalizers;
public function __construct(DiffOptions $options)
{
$this->comparators = array_reverse($options->comparators());
$this->comparators[] = [DateTimeInterface::class, Closure::fromCallable([self::class, 'sameInstant'])];
$this->normalizers = array_reverse($options->normalizers());
}
/**
* The comparator that claims both values, or null
*/
public function comparator(mixed $before, mixed $after): ?Closure
{
if (! is_object($before) || ! is_object($after)) {
return null;
}
foreach ($this->comparators as $index => [$class, $comparator]) {
if ($before instanceof $class && $after instanceof $class) {
return $this->isBuiltInDate($index) && ($this->isNormalized($before) || $this->isNormalized($after)) ? null : $comparator;
}
}
return null;
}
/**
* Whether a comparator claims an object, so it is compared as it is instead of normalized
*/
public function isCompared(object $value): bool
{
return $this->claim($value) !== null;
}
/**
* The position and the callback of the comparator that claims an object, or null
*
* @return array{int, Closure}|null
*/
public function claim(object $value): ?array
{
foreach ($this->comparators as $index => [$class, $comparator]) {
if ($value instanceof $class) {
return $this->isBuiltInDate($index) && $this->isNormalized($value) ? null : [$index, $comparator];
}
}
return null;
}
/**
* Whether the comparator at this position is the built-in DateTimeInterface one
*/
public function isBuiltInDate(int $index): bool
{
return $index === count($this->comparators) - 1;
}
/**
* Whether a registered normalizer handles an object
*/
private function isNormalized(object $value): bool
{
foreach ($this->normalizers as [$class]) {
if ($value instanceof $class) {
return true;
}
}
return false;
}
/**
* Turn an object that no comparator claims into a scalar, null, an array or an object a
* comparator claims; any other value is returned unchanged
*
* @param list<int|string> $path
*/
public function normalize(mixed $value, array $path): mixed
{
$chain = [];
while (is_object($value) && ! $this->isCompared($value)) {
foreach ($chain as $previous) {
if ($previous === $value) {
throw UnsupportedValueException::circular(Path::fromSegments($path), $value);
}
}
$chain[] = $value;
$value = $this->normalizeOnce($value, $path);
}
return $value;
}
/**
* @param list<int|string> $path
*/
private function normalizeOnce(object $value, array $path): mixed
{
foreach ($this->normalizers as [$class, $normalizer]) {
if ($value instanceof $class) {
return $normalizer($value);
}
}
if ($value instanceof JsonSerializable) {
return $value->jsonSerialize();
}
if ($value instanceof stdClass) {
return get_object_vars($value);
}
throw UnsupportedValueException::at(Path::fromSegments($path), Values::describe($value));
}
private static function sameInstant(DateTimeInterface $before, DateTimeInterface $after): bool
{
return $before->format('U.u') === $after->format('U.u');
}
}