|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base daft objects. |
|
4
|
|
|
* |
|
5
|
|
|
* @author SignpostMarv |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
|
10
|
|
|
|
|
11
|
|
|
use Closure; |
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Array-backed daft objects. |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractArrayBackedDaftObject extends AbstractDaftObject implements DaftObjectCreatedByArray |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* data for this instance. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array<string, mixed> |
|
23
|
|
|
*/ |
|
24
|
|
|
private $data = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* List of changed properties. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array<string, bool> |
|
30
|
|
|
*/ |
|
31
|
|
|
private $changedProperties = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* List of changed properties, for write-once read-many. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array<string, bool> |
|
37
|
|
|
*/ |
|
38
|
|
|
private $wormProperties = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array<int|string, scalar|array|object|null> $data |
|
42
|
|
|
*/ |
|
43
|
528 |
|
public function __construct(array $data = [], bool $writeAll = false) |
|
44
|
|
|
{ |
|
45
|
528 |
|
parent::__construct(); |
|
46
|
|
|
|
|
47
|
524 |
|
if (true === $writeAll) { |
|
48
|
156 |
|
foreach ($data as $k => $v) { |
|
49
|
106 |
|
if ( ! is_string($k)) { |
|
50
|
34 |
|
throw new InvalidArgumentException(DaftObjectCreatedByArray::ERR_KEY_NOT_STRING); |
|
51
|
|
|
} |
|
52
|
122 |
|
$this->__set($k, $v); |
|
53
|
|
|
} |
|
54
|
|
|
} else { |
|
55
|
372 |
|
foreach ($data as $k => $v) { |
|
56
|
222 |
|
if ( ! is_string($k)) { |
|
57
|
34 |
|
throw new InvalidArgumentException(DaftObjectCreatedByArray::ERR_KEY_NOT_STRING); |
|
58
|
|
|
} |
|
59
|
188 |
|
$this->data[$k] = $v; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
452 |
|
} |
|
63
|
|
|
|
|
64
|
72 |
|
public function __isset(string $property) : bool |
|
65
|
|
|
{ |
|
66
|
|
|
/** |
|
67
|
|
|
* @var array<int, string>|string |
|
68
|
|
|
*/ |
|
69
|
72 |
|
$properties = static::PROPERTIES; |
|
70
|
|
|
|
|
71
|
|
|
return |
|
72
|
72 |
|
in_array($property, (array) $properties, true) && |
|
73
|
72 |
|
isset($this->data, $this->data[$property]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
68 |
|
public function ChangedProperties() : array |
|
77
|
|
|
{ |
|
78
|
68 |
|
return array_keys($this->changedProperties); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
76 |
|
public function MakePropertiesUnchanged(string ...$properties) : void |
|
82
|
|
|
{ |
|
83
|
76 |
|
foreach ($properties as $property) { |
|
84
|
76 |
|
unset($this->changedProperties[$property]); |
|
85
|
|
|
} |
|
86
|
76 |
|
} |
|
87
|
|
|
|
|
88
|
216 |
|
public function HasPropertyChanged(string $property) : bool |
|
89
|
|
|
{ |
|
90
|
216 |
|
return $this->changedProperties[$property] ?? false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
54 |
|
public function jsonSerialize() : array |
|
94
|
|
|
{ |
|
95
|
54 |
|
JsonTypeUtilities::ThrowIfNotDaftJson(static::class); |
|
96
|
|
|
|
|
97
|
12 |
|
$out = []; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @var array<int, string> |
|
101
|
|
|
*/ |
|
102
|
12 |
|
$properties = static::DaftObjectJsonPropertyNames(); |
|
103
|
|
|
|
|
104
|
12 |
|
foreach ($properties as $property) { |
|
105
|
|
|
/** |
|
106
|
|
|
* @var scalar|array|object|null |
|
107
|
|
|
*/ |
|
108
|
12 |
|
$val = $this->DoGetSet($property, false); |
|
109
|
|
|
|
|
110
|
12 |
|
if (false === is_null($val)) { |
|
111
|
12 |
|
$out[$property] = $val; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
12 |
|
return $out; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
66 |
|
final public static function DaftObjectFromJsonArray( |
|
119
|
|
|
array $array, |
|
120
|
|
|
bool $writeAll = false |
|
121
|
|
|
) : DaftJson { |
|
122
|
66 |
|
$array = JsonTypeUtilities::ThrowIfJsonDefNotValid(static::class, $array); |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @var array<int, string> |
|
126
|
|
|
*/ |
|
127
|
18 |
|
$props = array_keys($array); |
|
128
|
18 |
|
$mapper = static::DaftJsonClosure($array, $writeAll); |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @var array<int, scalar|object|array|null> |
|
132
|
|
|
*/ |
|
133
|
18 |
|
$vals = array_map($mapper, $props); |
|
134
|
|
|
|
|
135
|
14 |
|
$out = new static(array_combine($props, $vals), $writeAll); |
|
136
|
|
|
|
|
137
|
14 |
|
return JsonTypeUtilities::ThrowIfDaftObjectObjectNotDaftJson($out); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
54 |
|
public static function DaftObjectFromJsonString(string $string) : DaftJson |
|
141
|
|
|
{ |
|
142
|
54 |
|
JsonTypeUtilities::ThrowIfNotDaftJson(static::class); |
|
143
|
|
|
|
|
144
|
12 |
|
return static::DaftObjectFromJsonArray((array) json_decode($string, true)); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
316 |
|
public function DaftObjectWormPropertyWritten(string $property) : bool |
|
148
|
|
|
{ |
|
149
|
316 |
|
$wormProperties = $this->wormProperties; |
|
150
|
|
|
|
|
151
|
|
|
return |
|
152
|
316 |
|
($this instanceof DaftObjectWorm) && |
|
153
|
|
|
( |
|
154
|
136 |
|
$this->HasPropertyChanged($property) || |
|
155
|
316 |
|
isset($wormProperties[$property]) |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
18 |
|
final protected static function DaftJsonClosure(array $array, bool $writeAll) : Closure |
|
160
|
|
|
{ |
|
161
|
18 |
|
$jsonDef = static::DaftObjectJsonProperties(); |
|
162
|
|
|
|
|
163
|
|
|
return |
|
164
|
|
|
/** |
|
165
|
|
|
* @return mixed |
|
166
|
|
|
*/ |
|
167
|
|
|
function (string $prop) use ($array, $jsonDef, $writeAll) { |
|
168
|
|
|
/** |
|
169
|
|
|
* @var string|null |
|
170
|
|
|
*/ |
|
171
|
16 |
|
$jsonType = $jsonDef[$prop] ?? null; |
|
172
|
|
|
|
|
173
|
16 |
|
if ( ! is_string($jsonType)) { |
|
174
|
12 |
|
return $array[$prop]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
10 |
|
return JsonTypeUtilities::DaftJsonFromJsonType( |
|
178
|
10 |
|
$jsonType, |
|
179
|
10 |
|
$prop, |
|
180
|
10 |
|
(array) $array[$prop], |
|
181
|
10 |
|
$writeAll |
|
182
|
|
|
); |
|
183
|
18 |
|
}; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Retrieve a property from data. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $property the property being retrieved |
|
190
|
|
|
* |
|
191
|
|
|
* @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe |
|
192
|
|
|
* |
|
193
|
|
|
* @return mixed the property value |
|
194
|
|
|
*/ |
|
195
|
102 |
|
protected function RetrievePropertyValueFromData(string $property) |
|
196
|
|
|
{ |
|
197
|
|
|
/** |
|
198
|
|
|
* @var array<int, string> |
|
199
|
|
|
*/ |
|
200
|
102 |
|
$properties = static::NULLABLE_PROPERTIES; |
|
201
|
|
|
|
|
202
|
|
|
if ( |
|
203
|
102 |
|
! array_key_exists($property, $this->data) && |
|
204
|
102 |
|
! in_array($property, $properties, true) |
|
205
|
|
|
) { |
|
206
|
4 |
|
throw new PropertyNotNullableException(static::class, $property); |
|
207
|
98 |
|
} elseif (in_array($property, $properties, true)) { |
|
208
|
74 |
|
return $this->data[$property] ?? null; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
98 |
|
return $this->data[$property]; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param scalar|array|object|null $value |
|
216
|
|
|
*/ |
|
217
|
318 |
|
protected function NudgePropertyValue( |
|
218
|
|
|
string $property, |
|
219
|
|
|
$value, |
|
220
|
|
|
bool $autoTrimStrings = false, |
|
221
|
|
|
bool $throwIfNotUnique = false |
|
222
|
|
|
) : void { |
|
223
|
|
|
/** |
|
224
|
|
|
* @var array<int, string> |
|
225
|
|
|
*/ |
|
226
|
318 |
|
$nullables = static::NULLABLE_PROPERTIES; |
|
227
|
|
|
|
|
228
|
318 |
|
$this->MaybeThrowForPropertyOnNudge($property); |
|
229
|
316 |
|
$this->MaybeThrowOnNudge($property, $value, $nullables); |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @var array<int, string>|null |
|
233
|
|
|
*/ |
|
234
|
178 |
|
$spec = null; |
|
235
|
|
|
|
|
236
|
|
|
if ( |
|
237
|
178 |
|
is_a( |
|
238
|
178 |
|
static::class, |
|
239
|
178 |
|
DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class, |
|
240
|
178 |
|
true |
|
241
|
|
|
) |
|
242
|
|
|
) { |
|
243
|
|
|
$spec = ( |
|
244
|
16 |
|
static::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues()[$property] ?? null |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
178 |
|
if (is_array($spec)) { |
|
249
|
14 |
|
$value = $this->MaybeThrowIfValueDoesNotMatchMultiTypedArray( |
|
250
|
14 |
|
$property, |
|
251
|
14 |
|
$autoTrimStrings, |
|
252
|
14 |
|
$throwIfNotUnique, |
|
253
|
14 |
|
$value, |
|
254
|
10 |
|
...$spec |
|
255
|
|
|
); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
170 |
|
if (is_string($value) && $autoTrimStrings) { |
|
259
|
2 |
|
$value = trim($value); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
$isChanged = ( |
|
263
|
170 |
|
! array_key_exists($property, $this->data) || |
|
264
|
170 |
|
$this->data[$property] !== $value |
|
265
|
|
|
); |
|
266
|
|
|
|
|
267
|
170 |
|
$this->data[$property] = $value; |
|
268
|
|
|
|
|
269
|
170 |
|
if ($isChanged && true !== isset($this->changedProperties[$property])) { |
|
270
|
170 |
|
$this->changedProperties[$property] = $this->wormProperties[$property] = true; |
|
271
|
|
|
} |
|
272
|
170 |
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @see AbstractArrayBackedDaftObject::NudgePropertyValue() |
|
276
|
|
|
*/ |
|
277
|
318 |
|
protected function MaybeThrowForPropertyOnNudge(string $property) : void |
|
278
|
|
|
{ |
|
279
|
|
|
/** |
|
280
|
|
|
* @var array<int, string> |
|
281
|
|
|
*/ |
|
282
|
318 |
|
$properties = static::PROPERTIES; |
|
283
|
|
|
|
|
284
|
318 |
|
if (true !== in_array($property, $properties, true)) { |
|
285
|
2 |
|
throw new UndefinedPropertyException(static::class, $property); |
|
286
|
316 |
|
} elseif ($this->DaftObjectWormPropertyWritten($property)) { |
|
287
|
100 |
|
throw new PropertyNotRewriteableException(static::class, $property); |
|
288
|
|
|
} |
|
289
|
316 |
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* @param mixed $value |
|
293
|
|
|
* |
|
294
|
|
|
* @see AbstractArrayBackedDaftObject::NudgePropertyValue() |
|
295
|
|
|
*/ |
|
296
|
316 |
|
protected function MaybeThrowOnNudge(string $property, $value, array $properties) : void |
|
297
|
|
|
{ |
|
298
|
316 |
|
if (true === is_null($value) && true !== in_array($property, $properties, true)) { |
|
299
|
138 |
|
throw new PropertyNotNullableException(static::class, $property); |
|
300
|
|
|
} |
|
301
|
178 |
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* @param mixed $value |
|
305
|
|
|
* |
|
306
|
|
|
* @return array<int, mixed> filtered $value |
|
307
|
|
|
*/ |
|
308
|
14 |
|
protected function MaybeThrowIfValueDoesNotMatchMultiTypedArray( |
|
309
|
|
|
string $prop, |
|
|
|
|
|
|
310
|
|
|
bool $autoTrimStrings, |
|
311
|
|
|
bool $throwIfNotUnique, |
|
312
|
|
|
$value, |
|
313
|
|
|
string ...$types |
|
314
|
|
|
) : array { |
|
315
|
14 |
|
if ( ! is_array($value)) { |
|
316
|
2 |
|
throw new InvalidArgumentException( |
|
317
|
|
|
'Argument 4 passed to ' . |
|
318
|
|
|
__METHOD__ . |
|
319
|
|
|
' must be an array, ' . |
|
320
|
2 |
|
(is_object($value) ? get_class($value) : gettype($value)) . |
|
321
|
2 |
|
' given!' |
|
322
|
|
|
); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
12 |
|
$initialCount = count($value); |
|
326
|
12 |
|
$value = array_filter($value, 'is_int', ARRAY_FILTER_USE_KEY); |
|
327
|
|
|
|
|
328
|
12 |
|
if (count($value) !== $initialCount) { |
|
329
|
2 |
|
throw new InvalidArgumentException( |
|
330
|
|
|
'Argument 4 passed to ' . |
|
331
|
|
|
__METHOD__ . |
|
332
|
2 |
|
' must be array<int, mixed>' |
|
333
|
|
|
); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
10 |
|
$initialCount = count($value); |
|
337
|
|
|
|
|
338
|
10 |
|
$value = array_filter( |
|
339
|
10 |
|
$value, |
|
340
|
|
|
/** |
|
341
|
|
|
* @param mixed $maybe |
|
342
|
|
|
*/ |
|
343
|
|
|
function ($maybe) use ($types) : bool { |
|
344
|
10 |
|
if (is_object($maybe)) { |
|
345
|
8 |
|
foreach ($types as $maybeType) { |
|
346
|
8 |
|
if (is_a($maybe, $maybeType)) { |
|
347
|
8 |
|
return true; |
|
348
|
|
|
} |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
2 |
|
return false; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
4 |
|
return in_array(gettype($maybe), $types, true); |
|
355
|
10 |
|
} |
|
356
|
|
|
); |
|
357
|
|
|
|
|
358
|
10 |
|
if (count($value) !== $initialCount) { |
|
359
|
2 |
|
throw new InvalidArgumentException( |
|
360
|
|
|
'Argument 4 passed to ' . |
|
361
|
|
|
__METHOD__ . |
|
362
|
2 |
|
' contained values that did not match the provided types!' |
|
363
|
|
|
); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
8 |
|
$initialCount = count($value); |
|
367
|
|
|
|
|
368
|
8 |
|
if (in_array('string', $types, true) && $autoTrimStrings && $initialCount > 0) { |
|
369
|
2 |
|
$value = array_map( |
|
370
|
|
|
/** |
|
371
|
|
|
* @param mixed $maybe |
|
372
|
|
|
* |
|
373
|
|
|
* @return mixed |
|
374
|
|
|
*/ |
|
375
|
|
|
function ($maybe) { |
|
376
|
2 |
|
return is_string($maybe) ? trim($maybe) : $maybe; |
|
377
|
2 |
|
}, |
|
378
|
2 |
|
$value |
|
379
|
|
|
); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
8 |
|
$value = array_unique($value, SORT_REGULAR); |
|
383
|
|
|
|
|
384
|
8 |
|
if ($throwIfNotUnique && count($value) !== $initialCount) { |
|
385
|
2 |
|
throw new InvalidArgumentException( |
|
386
|
|
|
'Argument 3 passed to ' . |
|
387
|
|
|
__METHOD__ . |
|
388
|
2 |
|
' contained non-unique values!' |
|
389
|
|
|
); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
6 |
|
return array_values($value); |
|
393
|
|
|
} |
|
394
|
|
|
} |
|
395
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.