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
|
|
|
const BOOL_DEFAULT_WRITEALL = false; |
17
|
|
|
|
18
|
|
|
const BOOL_DEFAULT_AUTOTRIMSTRINGS = false; |
19
|
|
|
|
20
|
|
|
const BOOL_DEFAULT_THROWIFNOTUNIQUE = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* data for this instance. |
24
|
|
|
* |
25
|
|
|
* @var array<string, scalar|array|object|null> |
26
|
|
|
*/ |
27
|
|
|
private $data = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* List of changed properties. |
31
|
|
|
* |
32
|
|
|
* @var array<string, bool> |
33
|
|
|
*/ |
34
|
|
|
private $changedProperties = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* List of changed properties, for write-once read-many. |
38
|
|
|
* |
39
|
|
|
* @var array<string, bool> |
40
|
|
|
*/ |
41
|
|
|
private $wormProperties = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array<string, scalar|array|object|null> $data |
45
|
|
|
*/ |
46
|
1152 |
|
public function __construct(array $data = [], bool $writeAll = false) |
47
|
|
|
{ |
48
|
1152 |
|
if (true === $writeAll) { |
49
|
192 |
|
foreach ($data as $k => $v) { |
50
|
192 |
|
$this->__set($k, $v); |
51
|
|
|
} |
52
|
|
|
} else { |
53
|
960 |
|
$this->data = $data; |
54
|
|
|
} |
55
|
1152 |
|
} |
56
|
|
|
|
57
|
80 |
|
public function __isset(string $property) : bool |
58
|
|
|
{ |
59
|
|
|
return |
60
|
80 |
|
in_array( |
61
|
60 |
|
$property, |
62
|
80 |
|
static::DaftObjectProperties(), |
63
|
80 |
|
DefinitionAssistant::IN_ARRAY_STRICT_MODE |
64
|
|
|
) && |
65
|
80 |
|
isset($this->data, $this->data[$property]); |
66
|
|
|
} |
67
|
|
|
|
68
|
88 |
|
public function ChangedProperties() : array |
69
|
|
|
{ |
70
|
88 |
|
return array_keys($this->changedProperties); |
71
|
|
|
} |
72
|
|
|
|
73
|
88 |
|
public function MakePropertiesUnchanged(string ...$properties) : void |
74
|
|
|
{ |
75
|
88 |
|
foreach ($properties as $property) { |
76
|
88 |
|
unset($this->changedProperties[$property]); |
77
|
|
|
} |
78
|
88 |
|
} |
79
|
|
|
|
80
|
280 |
|
public function HasPropertyChanged(string $property) : bool |
81
|
|
|
{ |
82
|
280 |
|
return $this->changedProperties[$property] ?? false; |
83
|
|
|
} |
84
|
|
|
|
85
|
124 |
|
public function jsonSerialize() : array |
86
|
|
|
{ |
87
|
|
|
/** |
88
|
|
|
* @var array<int, string> |
89
|
|
|
*/ |
90
|
124 |
|
$properties = static::DaftObjectJsonPropertyNames(); |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var array<string, string> |
94
|
|
|
*/ |
95
|
52 |
|
$properties = array_combine($properties, $properties); |
96
|
|
|
|
97
|
52 |
|
return array_filter( |
98
|
39 |
|
array_map( |
99
|
|
|
/** |
100
|
|
|
* @return scalar|array|object|null |
101
|
|
|
*/ |
102
|
|
|
function (string $property) { |
103
|
52 |
|
return $this->DoGetSet($property, false); |
104
|
52 |
|
}, |
105
|
26 |
|
$properties |
106
|
|
|
), |
107
|
|
|
/** |
108
|
|
|
* @param scalar|array|object|null $maybe |
109
|
|
|
*/ |
110
|
|
|
function ($maybe) : bool { |
111
|
52 |
|
return ! is_null($maybe); |
112
|
52 |
|
} |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array<int|string, scalar|(scalar|array|object|null)[]|object|null> $array |
118
|
|
|
*/ |
119
|
124 |
|
final public static function DaftObjectFromJsonArray( |
120
|
|
|
array $array, |
121
|
|
|
bool $writeAll = self::BOOL_DEFAULT_WRITEALL |
122
|
|
|
) : DaftJson { |
123
|
124 |
|
$type = JsonTypeUtilities::ThrowIfNotDaftJson(static::class); |
124
|
|
|
|
125
|
52 |
|
$array = JsonTypeUtilities::ThrowIfJsonDefNotValid($type, $array); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @var array<int, string> |
129
|
|
|
*/ |
130
|
52 |
|
$props = array_keys($array); |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @var array<int, scalar|object|array|null> |
134
|
|
|
*/ |
135
|
52 |
|
$vals = array_map( |
136
|
52 |
|
JsonTypeUtilities::DaftJsonClosure( |
137
|
39 |
|
$array, |
138
|
26 |
|
$writeAll, |
139
|
26 |
|
$type |
140
|
|
|
), |
141
|
26 |
|
$props |
142
|
|
|
); |
143
|
|
|
|
144
|
52 |
|
return new $type(array_combine($props, $vals), $writeAll); |
145
|
|
|
} |
146
|
|
|
|
147
|
196 |
|
public static function DaftObjectFromJsonString(string $string) : DaftJson |
148
|
|
|
{ |
149
|
|
|
/** |
150
|
|
|
* @var scalar|array<int|string, scalar|(scalar|array|object|null)[]|object|null>|object|null |
151
|
|
|
*/ |
152
|
196 |
|
$decoded = json_decode($string, true); |
153
|
|
|
|
154
|
196 |
|
return JsonTypeUtilities::ThrowIfNotDaftJson(static::class)::DaftObjectFromJsonArray( |
155
|
52 |
|
is_array($decoded) ? $decoded : [$decoded] |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
324 |
|
public function DaftObjectWormPropertyWritten(string $property) : bool |
160
|
|
|
{ |
161
|
324 |
|
$wormProperties = $this->wormProperties; |
162
|
|
|
|
163
|
|
|
return |
164
|
324 |
|
($this instanceof DaftObjectWorm) && |
165
|
|
|
( |
166
|
192 |
|
$this->HasPropertyChanged($property) || |
167
|
324 |
|
isset($wormProperties[$property]) |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Retrieve a property from data. |
173
|
|
|
* |
174
|
|
|
* @param string $property the property being retrieved |
175
|
|
|
* |
176
|
|
|
* @throws Exceptions\PropertyNotNullableException if value is not set and $property is not listed as nullabe |
177
|
|
|
* |
178
|
|
|
* @return scalar|array|object|null the property value |
179
|
|
|
*/ |
180
|
216 |
|
protected function RetrievePropertyValueFromData(string $property) |
181
|
|
|
{ |
182
|
216 |
|
$isNullable = in_array( |
183
|
162 |
|
$property, |
184
|
216 |
|
static::DaftObjectNullableProperties(), |
185
|
216 |
|
DefinitionAssistant::IN_ARRAY_STRICT_MODE |
186
|
|
|
); |
187
|
|
|
|
188
|
216 |
|
if ( ! array_key_exists($property, $this->data) && ! $isNullable) { |
189
|
4 |
|
throw Exceptions\Factory::PropertyNotNullableException(static::class, $property); |
190
|
212 |
|
} elseif ($isNullable) { |
191
|
128 |
|
return $this->data[$property] ?? null; |
192
|
|
|
} |
193
|
|
|
|
194
|
152 |
|
return $this->data[$property]; |
195
|
|
|
} |
196
|
|
|
|
197
|
4 |
|
protected function RetrievePropertyValueFromDataExpectStringOrNull(string $property) : ? string |
198
|
|
|
{ |
199
|
4 |
|
$value = $this->RetrievePropertyValueFromData($property); |
200
|
|
|
|
201
|
4 |
|
if (is_null($value)) { |
202
|
4 |
|
return null; |
203
|
|
|
} |
204
|
|
|
|
205
|
4 |
|
return TypeUtilities::ExpectRetrievedValueIsString($property, $value, static::class); |
206
|
|
|
} |
207
|
|
|
|
208
|
4 |
|
protected function RetrievePropertyValueFromDataExpectArrayOrNull(string $property) : ? array |
209
|
|
|
{ |
210
|
4 |
|
$value = $this->RetrievePropertyValueFromData($property); |
211
|
|
|
|
212
|
4 |
|
if (is_null($value)) { |
213
|
4 |
|
return null; |
214
|
|
|
} |
215
|
|
|
|
216
|
4 |
|
return TypeUtilities::ExpectRetrievedValueIsArray($property, $value, static::class); |
217
|
|
|
} |
218
|
|
|
|
219
|
12 |
|
protected function RetrievePropertyValueFromDataExpectIntishOrNull(string $property) : ? int |
220
|
|
|
{ |
221
|
12 |
|
$value = $this->RetrievePropertyValueFromData($property); |
222
|
|
|
|
223
|
12 |
|
if (is_null($value)) { |
224
|
12 |
|
return null; |
225
|
|
|
} |
226
|
|
|
|
227
|
12 |
|
return TypeUtilities::ExpectRetrievedValueIsIntish($property, $value, static::class); |
228
|
|
|
} |
229
|
|
|
|
230
|
12 |
|
protected function RetrievePropertyValueFromDataExpectFloatishOrNull(string $property) : ? float |
231
|
|
|
{ |
232
|
12 |
|
$value = $this->RetrievePropertyValueFromData($property); |
233
|
|
|
|
234
|
12 |
|
if (is_null($value)) { |
235
|
12 |
|
return null; |
236
|
|
|
} |
237
|
|
|
|
238
|
12 |
|
return TypeUtilities::ExpectRetrievedValueIsFloatish($property, $value, static::class); |
239
|
|
|
} |
240
|
|
|
|
241
|
28 |
|
protected function RetrievePropertyValueFromDataExpectBoolishOrNull(string $property) : ? bool |
242
|
|
|
{ |
243
|
28 |
|
$value = $this->RetrievePropertyValueFromData($property); |
244
|
|
|
|
245
|
28 |
|
if (is_null($value)) { |
246
|
28 |
|
return null; |
247
|
|
|
} |
248
|
|
|
|
249
|
28 |
|
return TypeUtilities::ExpectRetrievedValueIsBoolish($property, $value, static::class); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param scalar|array|object|null $value |
254
|
|
|
*/ |
255
|
540 |
|
protected function NudgePropertyValue( |
256
|
|
|
string $property, |
257
|
|
|
$value, |
258
|
|
|
bool $autoTrimStrings = self::BOOL_DEFAULT_AUTOTRIMSTRINGS, |
259
|
|
|
bool $throwIfNotUnique = self::BOOL_DEFAULT_THROWIFNOTUNIQUE |
260
|
|
|
) : void { |
261
|
540 |
|
TypeUtilities::MaybeThrowOnNudge(static::class, $property, $value); |
262
|
|
|
|
263
|
324 |
|
if ($this->DaftObjectWormPropertyWritten($property)) { |
264
|
192 |
|
throw Exceptions\Factory::PropertyNotRewriteableException(static::class, $property); |
265
|
|
|
} |
266
|
|
|
|
267
|
324 |
|
$value = $this->MaybeModifyValueBeforeNudge( |
268
|
243 |
|
$property, |
269
|
162 |
|
$value, |
270
|
162 |
|
$autoTrimStrings, |
271
|
162 |
|
$throwIfNotUnique |
272
|
|
|
); |
273
|
|
|
|
274
|
|
|
$isChanged = ( |
275
|
308 |
|
! array_key_exists($property, $this->data) || |
276
|
308 |
|
$this->data[$property] !== $value |
277
|
|
|
); |
278
|
|
|
|
279
|
308 |
|
$this->data[$property] = $value; |
280
|
|
|
|
281
|
308 |
|
if ($isChanged && true !== isset($this->changedProperties[$property])) { |
282
|
308 |
|
$this->changedProperties[$property] = $this->wormProperties[$property] = true; |
283
|
|
|
} |
284
|
308 |
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param scalar|array|object|null $value |
288
|
|
|
* |
289
|
|
|
* @return scalar|array|object|null |
290
|
|
|
*/ |
291
|
324 |
|
private function MaybeModifyValueBeforeNudge( |
292
|
|
|
string $property, |
293
|
|
|
$value, |
294
|
|
|
bool $autoTrimStrings = self::BOOL_DEFAULT_AUTOTRIMSTRINGS, |
295
|
|
|
bool $throwIfNotUnique = self::BOOL_DEFAULT_THROWIFNOTUNIQUE |
296
|
|
|
) { |
297
|
324 |
|
$spec = null; |
298
|
|
|
|
299
|
|
|
if ( |
300
|
324 |
|
is_a( |
301
|
324 |
|
static::class, |
302
|
324 |
|
DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class, |
303
|
324 |
|
true |
304
|
|
|
) |
305
|
|
|
) { |
306
|
|
|
$spec = ( |
307
|
24 |
|
static::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues()[$property] ?? null |
308
|
|
|
); |
309
|
|
|
} |
310
|
|
|
|
311
|
324 |
|
if (is_array($spec)) { |
312
|
20 |
|
$value = DefinitionAssistant::MaybeThrowIfValueDoesNotMatchMultiTypedArray( |
313
|
15 |
|
$autoTrimStrings, |
314
|
10 |
|
$throwIfNotUnique, |
315
|
10 |
|
$value, |
316
|
8 |
|
...$spec |
317
|
|
|
); |
318
|
|
|
} |
319
|
|
|
|
320
|
308 |
|
if (is_string($value) && $autoTrimStrings) { |
321
|
4 |
|
$value = trim($value); |
322
|
|
|
} |
323
|
|
|
|
324
|
308 |
|
return $value; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|