Repository navigation
Expand file tree
/
Copy pathChange.php
More file actions
139 lines (120 loc) · 3.38 KB
/
Copy pathChange.php
File metadata and controls
139 lines (120 loc) · 3.38 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
<?php
declare(strict_types=1);
namespace Pharaonic\Diff;
use LogicException;
/**
* One change reported by a structured diff
*
* A change at a list item may know both the item's old and new paths. Its canonical path() is the
* old path for a removed value and the new path otherwise.
*/
final class Change
{
/**
* Stored instead of a value matched by DiffOptions::redact()
*/
public const REDACTED = '[REDACTED]';
private Path $path;
/**
* @internal Changes are created by Diff::between().
*/
public function __construct(
private string $type,
private ?Path $oldPath,
private ?Path $newPath,
private bool $hasOldValue,
private mixed $oldValue,
private bool $hasNewValue,
private mixed $newValue,
private bool $aggregate = false,
private bool $redacted = false,
) {
$path = $type === ChangeType::REMOVED ? $oldPath : $newPath;
if ($path === null) {
throw new LogicException(sprintf('A %s change needs its canonical path.', $type));
}
$this->path = $path;
}
/**
* One of the ChangeType constants
*/
public function type(): string
{
return $this->type;
}
public function path(): Path
{
return $this->path;
}
/**
* Where the value was, or null when it did not exist before
*/
public function oldPath(): ?Path
{
return $this->oldPath;
}
/**
* Where the value is, or null when it no longer exists
*/
public function newPath(): ?Path
{
return $this->newPath;
}
public function hasOldValue(): bool
{
return $this->hasOldValue;
}
public function hasNewValue(): bool
{
return $this->hasNewValue;
}
/**
* The value before the change; null both when it was null and when it did not exist, which
* hasOldValue() tells apart
*/
public function oldValue(): mixed
{
return $this->oldValue;
}
/**
* The value after the change; null both when it is null and when it does not exist, which
* hasNewValue() tells apart
*/
public function newValue(): mixed
{
return $this->newValue;
}
/**
* Whether this is a container reported as modified because something inside it changed
*/
public function isAggregate(): bool
{
return $this->aggregate;
}
/**
* Whether the values were replaced with Change::REDACTED as a whole; redacted values inside a
* container are replaced without setting this flag
*/
public function isRedacted(): bool
{
return $this->redacted;
}
/**
* @return 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 [
'type' => $this->type,
'path' => $this->path->toDot(),
'old_path' => $this->oldPath?->toDot(),
'new_path' => $this->newPath?->toDot(),
'old_exists' => $this->hasOldValue,
'old' => $this->oldValue,
'new_exists' => $this->hasNewValue,
'new' => $this->newValue,
'aggregate' => $this->aggregate,
'redacted' => $this->redacted,
];
}
}