|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base daft objects. |
|
4
|
|
|
* |
|
5
|
|
|
* @author SignpostMarv |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Array-backed daft objects. |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class AbstractArrayBackedDaftObject extends AbstractDaftObject implements DaftObjectCreatedByArray |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* data for this instance. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private $data = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* List of changed properties. |
|
25
|
|
|
* |
|
26
|
|
|
* @var bool[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $changedProperties = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* List of changed properties, for write-once read-many. |
|
32
|
|
|
* |
|
33
|
|
|
* @var bool[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $wormProperties = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
189 |
|
public function __construct(array $data = [], bool $writeAll = false) |
|
41
|
|
|
{ |
|
42
|
189 |
|
parent::__construct(); |
|
43
|
|
|
|
|
44
|
187 |
|
if (true === $writeAll) { |
|
45
|
60 |
|
foreach ($data as $k => $v) { |
|
46
|
60 |
|
$this->__set($k, $v); |
|
47
|
|
|
} |
|
48
|
|
|
} else { |
|
49
|
129 |
|
foreach ($data as $k => $v) { |
|
50
|
68 |
|
$this->data[$k] = $v; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
185 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
33 |
|
public function __isset(string $property) : bool |
|
59
|
|
|
{ |
|
60
|
|
|
return |
|
61
|
33 |
|
in_array($property, static::PROPERTIES, true) && |
|
62
|
33 |
|
isset($this->data, $this->data[$property]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
29 |
|
public function ChangedProperties() : array |
|
69
|
|
|
{ |
|
70
|
|
|
/** |
|
71
|
|
|
* @var string[] $out |
|
72
|
|
|
*/ |
|
73
|
29 |
|
$out = array_keys($this->changedProperties); |
|
74
|
|
|
|
|
75
|
29 |
|
return $out; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
33 |
|
public function MakePropertiesUnchanged(string ...$properties) : void |
|
82
|
|
|
{ |
|
83
|
33 |
|
foreach ($properties as $property) { |
|
84
|
33 |
|
unset($this->changedProperties[$property]); |
|
85
|
|
|
} |
|
86
|
33 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
82 |
|
public function HasPropertyChanged(string $property) : bool |
|
92
|
|
|
{ |
|
93
|
|
|
return |
|
94
|
82 |
|
isset($this->changedProperties[$property]) && |
|
95
|
82 |
|
true === $this->changedProperties[$property]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
22 |
|
public function jsonSerialize() : array |
|
102
|
|
|
{ |
|
103
|
22 |
|
static::ThrowIfNotDaftJson(); |
|
104
|
|
|
|
|
105
|
6 |
|
$out = []; |
|
106
|
|
|
|
|
107
|
6 |
|
foreach (static::DaftObjectJsonProperties() as $k => $v) { |
|
108
|
6 |
|
$property = $v; |
|
109
|
6 |
|
if (is_string($k)) { |
|
110
|
3 |
|
$property = $k; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
6 |
|
$val = $this->DoGetSet( |
|
114
|
6 |
|
$property, |
|
115
|
6 |
|
'Get' . ucfirst($property), |
|
116
|
6 |
|
PropertyNotReadableException::class, |
|
117
|
6 |
|
NotPublicGetterPropertyException::class |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
6 |
|
if (false === is_null($val)) { |
|
121
|
6 |
|
$out[$property] = $val; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
6 |
|
return $out; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
|
|
*/ |
|
131
|
22 |
|
final public static function DaftObjectFromJsonArray( |
|
132
|
|
|
array $array, |
|
133
|
|
|
bool $writeAll = false |
|
134
|
|
|
) : DaftJson { |
|
135
|
22 |
|
static::ThrowIfNotDaftJson(); |
|
136
|
|
|
|
|
137
|
6 |
|
$in = []; |
|
138
|
|
|
|
|
139
|
6 |
|
$props = static::DaftObjectJsonProperties(); |
|
140
|
6 |
|
$jsonDef = static::DaftObjectJsonProperties(); |
|
141
|
6 |
|
$nullableProps = static::DaftObjectNullableProperties(); |
|
142
|
|
|
|
|
143
|
6 |
|
$jsonProps = []; |
|
144
|
|
|
|
|
145
|
6 |
|
foreach ($jsonDef as $k => $v) { |
|
146
|
6 |
|
if (is_string($k)) { |
|
147
|
3 |
|
$jsonProps[] = $k; |
|
148
|
|
|
} else { |
|
149
|
6 |
|
$jsonProps[] = $v; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
6 |
|
foreach ($jsonProps as $prop) { |
|
154
|
|
|
if ( |
|
155
|
|
|
( |
|
156
|
6 |
|
false === isset($array[$prop]) || |
|
157
|
6 |
|
is_null($array[$prop]) |
|
158
|
|
|
) && |
|
159
|
2 |
|
false === in_array($prop, $nullableProps, true) |
|
160
|
|
|
) { |
|
161
|
|
|
throw new PropertyNotNullableException(static::class, $prop); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
6 |
|
foreach (array_keys($array) as $prop) { |
|
166
|
|
|
if ( |
|
167
|
6 |
|
false === in_array($prop, $props, true) && |
|
168
|
3 |
|
false === isset($jsonDef[$prop]) |
|
169
|
|
|
) { |
|
170
|
|
|
throw new UndefinedPropertyException(static::class, $prop); |
|
171
|
|
|
} elseif ( |
|
172
|
6 |
|
false === in_array($prop, $jsonProps, true) |
|
173
|
|
|
) { |
|
174
|
|
|
throw new PropertyNotJsonDecodableException( |
|
175
|
|
|
static::class, |
|
176
|
|
|
$prop |
|
177
|
|
|
); |
|
178
|
6 |
|
} elseif (is_null($array[$prop])) { |
|
179
|
|
|
continue; |
|
180
|
6 |
|
} elseif (isset($jsonDef[$prop])) { |
|
181
|
3 |
|
$in[$prop] = static::DaftObjectFromJsonType( |
|
182
|
3 |
|
$prop, |
|
183
|
3 |
|
$jsonDef[$prop], |
|
184
|
3 |
|
$array[$prop], |
|
185
|
3 |
|
$writeAll |
|
186
|
|
|
); |
|
187
|
|
|
} else { |
|
188
|
6 |
|
$in[$prop] = $array[$prop]; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
6 |
|
$out = new static($in, $writeAll); |
|
193
|
|
|
|
|
194
|
6 |
|
if ( ! ($out instanceof DaftJson)) { // here to trick phpstan |
|
195
|
|
|
exit; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
6 |
|
return $out; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
22 |
|
public static function DaftObjectFromJsonString(string $string) : DaftJson |
|
202
|
|
|
{ |
|
203
|
22 |
|
static::ThrowIfNotDaftJson(); |
|
204
|
|
|
|
|
205
|
6 |
|
return static::DaftObjectFromJsonArray(json_decode($string, true)); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param mixed $propVal |
|
210
|
|
|
* |
|
211
|
|
|
* @return DaftJson|DaftJson[] |
|
212
|
|
|
*/ |
|
213
|
3 |
|
protected static function DaftObjectFromJsonType( |
|
214
|
|
|
string $prop, |
|
215
|
|
|
string $jsonType, |
|
216
|
|
|
$propVal, |
|
217
|
|
|
bool $writeAll |
|
218
|
|
|
) { |
|
219
|
3 |
|
$isArray = false; |
|
220
|
3 |
|
if ('[]' === mb_substr($jsonType, -2)) { |
|
221
|
1 |
|
$isArray = true; |
|
222
|
1 |
|
$jsonType = mb_substr($jsonType, 0, -2); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
3 |
|
if ($isArray && false === is_array($propVal)) { |
|
226
|
|
|
throw new PropertyNotJsonDecodableShouldBeArrayException( |
|
227
|
|
|
static::class, |
|
228
|
|
|
$prop |
|
229
|
|
|
); |
|
230
|
3 |
|
} elseif (false === is_a($jsonType, DaftJson::class, true)) { |
|
231
|
|
|
throw new ClassDoesNotImplementClassException( |
|
232
|
|
|
$jsonType, |
|
233
|
|
|
DaftJson::class |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @var DaftJson $jsonType |
|
239
|
|
|
*/ |
|
240
|
3 |
|
$jsonType = $jsonType; |
|
241
|
|
|
|
|
242
|
3 |
|
if ($isArray) { |
|
243
|
1 |
|
$out = []; |
|
244
|
|
|
|
|
245
|
1 |
|
foreach ($propVal as $i => $val) { |
|
246
|
1 |
|
if (is_array($val)) { |
|
247
|
1 |
|
$out[] = $jsonType::DaftObjectFromJsonArray( |
|
248
|
1 |
|
$val, |
|
249
|
1 |
|
$writeAll |
|
250
|
|
|
); |
|
251
|
|
|
} else { |
|
252
|
|
|
throw new PropertyNotJsonDecodableShouldBeArrayException( |
|
253
|
|
|
(string) $jsonType, |
|
254
|
|
|
($prop . '[' . $i . ']') |
|
255
|
|
|
); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
1 |
|
return $out; |
|
|
|
|
|
|
260
|
2 |
|
} elseif (is_array($propVal)) { |
|
261
|
2 |
|
return $jsonType::DaftObjectFromJsonArray( |
|
|
|
|
|
|
262
|
2 |
|
$propVal, |
|
263
|
2 |
|
$writeAll |
|
264
|
|
|
); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
throw new PropertyNotJsonDecodableShouldBeArrayException( |
|
268
|
|
|
(string) $jsonType, |
|
269
|
|
|
$prop |
|
270
|
|
|
); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Retrieve a property from data. |
|
275
|
|
|
* |
|
276
|
|
|
* @param string $property the property being retrieved |
|
277
|
|
|
* |
|
278
|
|
|
* @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe |
|
279
|
|
|
* |
|
280
|
|
|
* @return mixed the property value |
|
281
|
|
|
*/ |
|
282
|
44 |
|
protected function RetrievePropertyValueFromData(string $property) |
|
283
|
|
|
{ |
|
284
|
|
|
if ( |
|
285
|
44 |
|
false === array_key_exists($property, $this->data) && |
|
286
|
5 |
|
false === in_array($property, static::NULLABLE_PROPERTIES, true) |
|
287
|
|
|
) { |
|
288
|
1 |
|
throw new PropertyNotNullableException(static::class, $property); |
|
289
|
|
|
} elseif ( |
|
290
|
43 |
|
in_array($property, static::NULLABLE_PROPERTIES, true) |
|
291
|
|
|
) { |
|
292
|
33 |
|
return $this->data[$property] ?? null; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
43 |
|
return $this->data[$property]; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* {@inheritdoc} |
|
300
|
|
|
*/ |
|
301
|
134 |
|
protected function NudgePropertyValue(string $property, $value) : void |
|
302
|
|
|
{ |
|
303
|
134 |
|
if (true !== in_array($property, static::PROPERTIES, true)) { |
|
304
|
1 |
|
throw new UndefinedPropertyException(static::class, $property); |
|
305
|
|
|
} elseif ( |
|
306
|
133 |
|
true === is_null($value) && |
|
307
|
88 |
|
true !== in_array($property, static::NULLABLE_PROPERTIES, true) |
|
308
|
|
|
) { |
|
309
|
58 |
|
throw new PropertyNotNullableException(static::class, $property); |
|
310
|
|
|
} elseif ( |
|
311
|
75 |
|
$this instanceof DaftObjectWorm && |
|
312
|
|
|
( |
|
313
|
50 |
|
$this->HasPropertyChanged($property) || |
|
314
|
|
|
( |
|
315
|
50 |
|
isset($this->wormProperties[$property]) && |
|
|
|
|
|
|
316
|
2 |
|
true === $this->wormProperties[$property] |
|
317
|
|
|
) |
|
318
|
|
|
) |
|
319
|
|
|
) { |
|
320
|
50 |
|
throw new PropertyNotRewriteableException( |
|
321
|
50 |
|
static::class, |
|
322
|
50 |
|
$property |
|
323
|
|
|
); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
$isChanged = ( |
|
327
|
75 |
|
false === array_key_exists($property, $this->data) || |
|
328
|
75 |
|
$this->data[$property] !== $value |
|
329
|
|
|
); |
|
330
|
|
|
|
|
331
|
75 |
|
$this->data[$property] = $value; |
|
332
|
|
|
|
|
333
|
75 |
|
if ($isChanged && true !== isset($this->changedProperties[$property])) { |
|
334
|
75 |
|
$this->changedProperties[$property] = true; |
|
335
|
75 |
|
$this->wormProperties[$property] = true; |
|
336
|
|
|
} |
|
337
|
75 |
|
} |
|
338
|
|
|
|
|
339
|
54 |
View Code Duplication |
private static function ThrowIfNotDaftJson() : void |
|
|
|
|
|
|
340
|
|
|
{ |
|
341
|
54 |
|
if (false === is_a(static::class, DaftJson::class, true)) { |
|
342
|
48 |
|
throw new DaftObjectNotDaftJsonBadMethodCallException( |
|
343
|
48 |
|
static::class |
|
344
|
|
|
); |
|
345
|
|
|
} |
|
346
|
6 |
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|