1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Mschindler83\ArrayAccess; |
5
|
|
|
|
6
|
|
|
use BadMethodCallException; |
7
|
|
|
use Mschindler83\ArrayAccess\DotAnnotation\DotAnnotation; |
8
|
|
|
use Opis\JsonSchema\Schema; |
9
|
|
|
use Opis\JsonSchema\Validator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method string string(...$path) |
13
|
|
|
* @method string|null stringOrNull(...$path) |
14
|
|
|
* @method int int(...$path) |
15
|
|
|
* @method int|null intOrNull(...$path) |
16
|
|
|
* @method float float(...$path) |
17
|
|
|
* @method float|null floatOrNull(...$path) |
18
|
|
|
* @method bool bool(...$path) |
19
|
|
|
* @method bool|null boolOrNull(...$path) |
20
|
|
|
* @method array array(...$path) |
21
|
|
|
* @method array|null arrayOrNull(...$path) |
22
|
|
|
*/ |
23
|
|
|
class ArrayAccess |
24
|
|
|
{ |
25
|
|
|
private array $data; |
26
|
|
|
|
27
|
37 |
|
public static function create($value): self |
28
|
|
|
{ |
29
|
37 |
|
if (!is_array($value)) { |
30
|
3 |
|
throw ArrayAccessFailed::notAnArray($value); |
31
|
|
|
} |
32
|
|
|
|
33
|
34 |
|
return new self($value); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
public static function createWithJsonSchemaValidation($value, string $jsonSchemaDefinition): self |
37
|
|
|
{ |
38
|
2 |
|
if (!is_array($value)) { |
39
|
|
|
throw ArrayAccessFailed::notAnArray($value); |
40
|
|
|
} |
41
|
2 |
|
$schema = Schema::fromJsonString($jsonSchemaDefinition); |
42
|
2 |
|
$result = (new Validator())->schemaValidation(\json_decode(\json_encode($value)), $schema, 10); |
43
|
|
|
|
44
|
2 |
|
if (!$result->isValid()) { |
45
|
1 |
|
throw ArrayAccessValidationFailed::withValidationErrors(...$result->getErrors()); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return new self($value); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
public static function newFromDotAnnotation(DotAnnotation ...$dotAnnotations): self |
52
|
|
|
{ |
53
|
3 |
|
$newArray = []; |
54
|
|
|
|
55
|
3 |
|
foreach ($dotAnnotations as $dotAnnotation) { |
56
|
3 |
|
$pointer = &$newArray; |
57
|
3 |
|
foreach ($dotAnnotation->path() as $key) { |
58
|
3 |
|
$pointer = &$pointer[$key]; |
59
|
|
|
} |
60
|
3 |
|
$pointer = $dotAnnotation->value(); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
return new self($newArray); |
64
|
|
|
} |
65
|
|
|
|
66
|
12 |
|
public function writeAtPath($value, string ...$path): self |
67
|
|
|
{ |
68
|
12 |
|
$instance = new self($this->data); |
69
|
|
|
|
70
|
12 |
|
$path = empty($path) ? [0] : $path; |
71
|
|
|
|
72
|
12 |
|
$pointer = &$instance->data; |
73
|
12 |
|
foreach ($path as $key) { |
74
|
12 |
|
$pointer = &$pointer[$key]; |
75
|
|
|
} |
76
|
12 |
|
$pointer = $value; |
77
|
|
|
|
78
|
12 |
|
return $instance; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function hasPath(...$path): bool |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
1 |
|
$this->findInPath(...$path); |
85
|
|
|
|
86
|
1 |
|
return true; |
87
|
1 |
|
} catch (ArrayAccessFailed $e) { |
88
|
1 |
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public function arrayAccess(...$path): self |
93
|
|
|
{ |
94
|
1 |
|
return self::create($this->array(...$path)); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
public function objectOfType(string $objectType, ...$path): object |
98
|
|
|
{ |
99
|
2 |
|
$value = $this->findInPath(...$path); |
100
|
|
|
|
101
|
2 |
|
if (get_class($value) !== $objectType) { |
102
|
1 |
|
throw ArrayAccessFailed::invalidType($path, $value, $objectType); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
return $value; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
public function dateTimeImmutable(string $format, ...$path): \DateTimeImmutable |
109
|
|
|
{ |
110
|
2 |
|
$value = $this->string(...$path); |
111
|
|
|
|
112
|
2 |
|
$dateTime = \DateTimeImmutable::createFromFormat($format, $value); |
113
|
2 |
|
if ($dateTime === false) { |
114
|
1 |
|
throw ArrayAccessFailed::invalidDateTimeType($path, $value, $format); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return $dateTime; |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
public function dateTime(string $format, ...$path): \DateTime |
121
|
|
|
{ |
122
|
2 |
|
$value = $this->string(...$path); |
123
|
|
|
|
124
|
2 |
|
$dateTime = \DateTime::createFromFormat($format, $value); |
125
|
2 |
|
if ($dateTime === false) { |
126
|
1 |
|
throw ArrayAccessFailed::invalidDateTimeType($path, $value, $format); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return $dateTime; |
130
|
|
|
} |
131
|
|
|
|
132
|
2 |
|
public function callback(callable $callback, ...$path) |
133
|
|
|
{ |
134
|
2 |
|
$value = $this->findInPath(...$path); |
135
|
2 |
|
if (!$callback($value)) { |
136
|
1 |
|
throw ArrayAccessFailed::failedByCallbackRestriction($path); |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
return $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
14 |
|
public function data(): array |
143
|
|
|
{ |
144
|
14 |
|
return $this->data; |
145
|
|
|
} |
146
|
|
|
|
147
|
19 |
|
public function __call($name, $arguments) |
148
|
|
|
{ |
149
|
|
|
$validMethods = [ |
150
|
19 |
|
'int', 'intOrNull', |
151
|
|
|
'string', 'stringOrNull', |
152
|
|
|
'float', 'floatOrNull', |
153
|
|
|
'bool', 'boolOrNull', |
154
|
|
|
'array', 'arrayOrNull' |
155
|
|
|
]; |
156
|
19 |
|
if (!in_array($name, $validMethods)) { |
157
|
2 |
|
throw new BadMethodCallException(\sprintf('Method "%s" not found', $name)); |
158
|
|
|
} |
159
|
|
|
|
160
|
17 |
|
$value = $this->findInPath(...$arguments); |
161
|
16 |
|
if (substr($name, -6) === 'OrNull' && $value === null) { |
162
|
1 |
|
return null; |
163
|
|
|
} |
164
|
|
|
|
165
|
15 |
|
$validationFunction = 'is_' . str_replace('OrNull', '', $name); |
166
|
15 |
|
if (!call_user_func($validationFunction, $value)) { |
167
|
1 |
|
throw ArrayAccessFailed::invalidType($arguments, $value, $name); |
168
|
|
|
} |
169
|
|
|
|
170
|
14 |
|
return $value; |
171
|
|
|
} |
172
|
|
|
|
173
|
22 |
|
private function findInPath(...$path) |
174
|
|
|
{ |
175
|
22 |
|
$temp = &$this->data; |
176
|
22 |
|
foreach ($path as $key) { |
177
|
22 |
|
if (!is_array($temp) || !array_key_exists($key, $temp)) { |
178
|
2 |
|
throw ArrayAccessFailed::pathNotFound(); |
179
|
|
|
} |
180
|
22 |
|
$temp = &$temp[$key]; |
181
|
|
|
} |
182
|
|
|
|
183
|
21 |
|
return $temp; |
184
|
|
|
} |
185
|
|
|
|
186
|
38 |
|
private function __construct(array $value) |
187
|
|
|
{ |
188
|
38 |
|
$this->data = $value; |
189
|
38 |
|
} |
190
|
|
|
} |
191
|
|
|
|