Repository navigation
Expand file tree
/
Copy pathDiffResult.php
More file actions
238 lines (205 loc) · 5.97 KB
/
Copy pathDiffResult.php
File metadata and controls
238 lines (205 loc) · 5.97 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
<?php
declare(strict_types=1);
namespace Pharaonic\Diff;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use JsonSerializable;
/**
* The changes between two structured values, parents before their descendants
*
* Methods that take a path accept dot notation or a Path.
*
* @implements IteratorAggregate<int, Change>
*/
final class DiffResult implements Countable, IteratorAggregate, JsonSerializable
{
/**
* @param list<Change> $changes
*
* @internal Results are created by Diff::between().
*/
public function __construct(private array $changes) {}
public function hasChanges(): bool
{
return $this->changes !== [];
}
/**
* Whether the value at the path or anything below it changed, at its old or its new path
*/
public function hasChanged(string|Path $path): bool
{
return $this->under($path) !== [];
}
/**
* Whether a value was added at exactly this path
*/
public function wasAdded(string|Path $path): bool
{
return $this->exactly(ChangeType::ADDED, $path);
}
/**
* Whether a value was removed from exactly this path
*/
public function wasRemoved(string|Path $path): bool
{
return $this->exactly(ChangeType::REMOVED, $path);
}
/**
* Whether the value at exactly this path was modified, directly or through its descendants
*/
public function wasModified(string|Path $path): bool
{
return $this->exactly(ChangeType::MODIFIED, $path);
}
/**
* Whether a value moved from or to exactly this path
*/
public function wasMoved(string|Path $path): bool
{
$segments = self::path($path)->segments();
foreach ($this->moved() as $change) {
if ($change->oldPath()?->segments() === $segments || $change->newPath()?->segments() === $segments) {
return true;
}
}
return false;
}
/**
* @return list<Change>
*/
public function changes(): array
{
return $this->changes;
}
/**
* @return list<Change>
*/
public function added(): array
{
return $this->ofType(ChangeType::ADDED);
}
/**
* @return list<Change>
*/
public function removed(): array
{
return $this->ofType(ChangeType::REMOVED);
}
/**
* Direct and aggregate modifications
*
* @return list<Change>
*/
public function modified(): array
{
return $this->ofType(ChangeType::MODIFIED);
}
/**
* @return list<Change>
*/
public function moved(): array
{
return $this->ofType(ChangeType::MOVED);
}
/**
* The changes whose canonical path is exactly this path
*
* @return list<Change>
*/
public function at(string|Path $path): array
{
$segments = self::path($path)->segments();
return array_values(array_filter(
$this->changes,
static fn(Change $change): bool => $change->path()->segments() === $segments,
));
}
/**
* The changes at this path or below it, at their old or their new path
*
* @return list<Change>
*/
public function under(string|Path $path): array
{
$segments = self::path($path)->segments();
return array_values(array_filter(
$this->changes,
static fn(Change $change): bool => self::within($change->oldPath(), $segments)
|| self::within($change->newPath(), $segments),
));
}
public function count(): int
{
return count($this->changes);
}
/**
* @return array{total: int, added: int, removed: int, modified: int, moved: int}
*/
public function stats(): array
{
return [
'total' => count($this->changes),
'added' => count($this->added()),
'removed' => count($this->removed()),
'modified' => count($this->modified()),
'moved' => count($this->moved()),
];
}
/**
* @return ArrayIterator<int, Change>
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->changes);
}
/**
* @return list<array{type: string, path: string, old_path: string|null, new_path: string|null, old_exists: bool, old: mixed, new_exists: bool, new: mixed, aggregate: bool, redacted: bool}>
*/
public function toArray(): array
{
return array_map(static fn(Change $change): array => $change->toArray(), $this->changes);
}
/**
* @return list<array{type: string, path: string, old_path: string|null, new_path: string|null, old_exists: bool, old: mixed, new_exists: bool, new: mixed, aggregate: bool, redacted: bool}>
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* Encode the changes as JSON; JSON_THROW_ON_ERROR is always added
*/
public function toJson(int $flags = 0): string
{
return json_encode($this->toArray(), $flags | JSON_THROW_ON_ERROR);
}
/**
* @return list<Change>
*/
private function ofType(string $type): array
{
return array_values(array_filter($this->changes, static fn(Change $change): bool => $change->type() === $type));
}
private function exactly(string $type, string|Path $path): bool
{
$segments = self::path($path)->segments();
foreach ($this->changes as $change) {
if ($change->type() === $type && $change->path()->segments() === $segments) {
return true;
}
}
return false;
}
/**
* @param list<int|string> $ancestor
*/
private static function within(?Path $path, array $ancestor): bool
{
return $path !== null && array_slice($path->segments(), 0, count($ancestor)) === $ancestor;
}
private static function path(string|Path $path): Path
{
return $path instanceof Path ? $path : Path::fromDot($path);
}
}