Repository navigation
Expand file tree
/
Copy pathDiffOptions.php
More file actions
354 lines (295 loc) · 8.74 KB
/
Copy pathDiffOptions.php
File metadata and controls
354 lines (295 loc) · 8.74 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
declare(strict_types=1);
namespace Pharaonic\Diff;
use Closure;
use InvalidArgumentException;
use Pharaonic\Diff\Support\Arrays;
use Pharaonic\Diff\Support\DotNotation;
/**
* Immutable options for Diff::between()
*
* Every method returns new options and leaves the current ones unchanged.
*
* Paths use dot notation: "user.email", "items.*.price" ("*" matches any one key or list index),
* with "\." and "\*" for a literal dot or star inside a key. When several list rules match the
* same path, the last one added wins.
*/
final class DiffOptions
{
/**
* @var list<list<string|null>>
*/
private array $ignore = [];
/**
* @var list<list<string|null>>
*/
private array $only = [];
/**
* @var list<list<string|null>>
*/
private array $redact = [];
private int $maxDepth = 64;
private float $floatTolerance = 0.0;
/**
* @var list<array{list<string|null>, string|null, list<int|string>}>
*/
private array $listRules = [];
private bool $detectMoves = false;
/**
* @var list<array{class-string, Closure}>
*/
private array $normalizers = [];
/**
* @var list<array{class-string, Closure}>
*/
private array $comparators = [];
private function __construct() {}
public static function create(): self
{
return new self();
}
/**
* Leave out changes at these paths and everything below them, without traversing them
*
* @param string|list<string> $paths
*/
public function ignore(string|array $paths): self
{
$options = clone $this;
$options->ignore = [...$this->ignore, ...self::patterns($paths)];
return $options;
}
/**
* Report only changes at these paths and below them; ignore() still wins
*
* Branches that cannot lead to these paths are not traversed. Containers above them are
* traversed but never reported, not even when a whole container was added, removed or
* changed type.
*
* @param string|list<string> $paths
*/
public function only(string|array $paths): self
{
$options = clone $this;
$options->only = [...$this->only, ...self::patterns($paths)];
return $options;
}
/**
* Compare these values but store Change::REDACTED instead of them in the result, including
* inside the values of their parents and of added or removed containers
*
* @param string|list<string> $paths
*/
public function redact(string|array $paths): self
{
$options = clone $this;
$options->redact = [...$this->redact, ...self::patterns($paths)];
return $options;
}
/**
* How deep the compared values may nest (64 by default); deeper values throw
*/
public function maxDepth(int $depth): self
{
if ($depth < 1) {
throw new InvalidArgumentException(sprintf('The maximum depth must be at least 1, %d given.', $depth));
}
$options = clone $this;
$options->maxDepth = $depth;
return $options;
}
/**
* Treat two floats as equal when they differ by at most this absolute amount
*/
public function floatTolerance(float $tolerance): self
{
if (! is_finite($tolerance) || $tolerance < 0) {
throw new InvalidArgumentException('The float tolerance must be a finite number of at least 0.');
}
$options = clone $this;
$options->floatTolerance = $tolerance;
return $options;
}
/**
* Compare the lists at a path with one of the ListStrategy constants
*/
public function listStrategy(string $path, string $strategy): self
{
if (! in_array($strategy, [ListStrategy::SEQUENCE, ListStrategy::POSITION, ListStrategy::UNORDERED], true)) {
throw new InvalidArgumentException(sprintf('Unknown list strategy "%s"; use a ListStrategy constant.', $strategy));
}
$options = clone $this;
$options->listRules[] = [DotNotation::parse($path), $strategy, []];
return $options;
}
/**
* Compare the lists at a path as records identified by one key or several keys together
*
* @param int|string|list<int|string> $keys
*/
public function identify(string $path, int|string|array $keys): self
{
$keys = self::identityKeys($keys);
$options = clone $this;
$options->listRules[] = [DotNotation::parse($path), null, $keys];
return $options;
}
/**
* Report records of identified lists that changed their order relative to the others as moved
*/
public function detectMoves(): self
{
$options = clone $this;
$options->detectMoves = true;
return $options;
}
/**
* Turn instances of a class or interface into a comparable value: a scalar, null or an array
*
* @param class-string $class
* @param callable(object): mixed $normalizer
*/
public function normalize(string $class, callable $normalizer): self
{
$options = clone $this;
$options->normalizers[] = [self::type($class), Closure::fromCallable($normalizer)];
return $options;
}
/**
* Decide whether two instances of a class or interface are equal; only true means equal, and
* a different pair is one modified change that is never compared further
*
* @param class-string $class
* @param callable(object, object): bool $equals
*/
public function compare(string $class, callable $equals): self
{
$options = clone $this;
$options->comparators[] = [self::type($class), Closure::fromCallable($equals)];
return $options;
}
/**
* @internal
*
* @return list<list<string|null>>
*/
public function ignoredPatterns(): array
{
return $this->ignore;
}
/**
* @internal
*
* @return list<list<string|null>>
*/
public function onlyPatterns(): array
{
return $this->only;
}
/**
* @internal
*
* @return list<list<string|null>>
*/
public function redactedPatterns(): array
{
return $this->redact;
}
/**
* @internal
*/
public function depthLimit(): int
{
return $this->maxDepth;
}
/**
* @internal
*/
public function tolerance(): float
{
return $this->floatTolerance;
}
/**
* Pattern, strategy (a ListStrategy constant, or null for identity) and identity keys, in the
* order they were added
*
* @internal
*
* @return list<array{list<string|null>, string|null, list<int|string>}>
*/
public function listRules(): array
{
return $this->listRules;
}
/**
* @internal
*/
public function movesDetected(): bool
{
return $this->detectMoves;
}
/**
* @internal
*
* @return list<array{class-string, Closure}>
*/
public function normalizers(): array
{
return $this->normalizers;
}
/**
* @internal
*
* @return list<array{class-string, Closure}>
*/
public function comparators(): array
{
return $this->comparators;
}
/**
* @param int|string|array<mixed> $keys
*
* @return list<int|string>
*/
private static function identityKeys(int|string|array $keys): array
{
$keys = is_array($keys) ? $keys : [$keys];
if ($keys === [] || ! Arrays::isList($keys)) {
throw new InvalidArgumentException('Identity keys must be a key or a non-empty list of keys.');
}
$identityKeys = [];
foreach ($keys as $key) {
if (! is_int($key) && ! is_string($key)) {
throw new InvalidArgumentException(sprintf('An identity key must be an int or a string, %s given.', get_debug_type($key)));
}
$identityKeys[] = $key;
}
return $identityKeys;
}
/**
* @param string|array<mixed> $paths
*
* @return list<list<string|null>>
*/
private static function patterns(string|array $paths): array
{
$patterns = [];
foreach ((array) $paths as $path) {
if (! is_string($path)) {
throw new InvalidArgumentException(sprintf('A path must be a string, %s given.', get_debug_type($path)));
}
$patterns[] = DotNotation::parse($path);
}
return $patterns;
}
/**
* @return class-string
*/
private static function type(string $class): string
{
if (! class_exists($class) && ! interface_exists($class)) {
throw new InvalidArgumentException(sprintf('The class or interface "%s" does not exist.', $class));
}
return $class;
}
}