|
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
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Base daft object. |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractDaftObject implements DaftObject |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* List of properties that can be defined on an implementation. |
|
20
|
|
|
* |
|
21
|
|
|
* @var array<int, string> |
|
22
|
|
|
*/ |
|
23
|
|
|
const PROPERTIES = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* List of nullable properties that can be defined on an implementation. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array<int, string> |
|
29
|
|
|
*/ |
|
30
|
|
|
const NULLABLE_PROPERTIES = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* List of exportable properties that can be defined on an implementation. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array<int, string> |
|
36
|
|
|
*/ |
|
37
|
|
|
const EXPORTABLE_PROPERTIES = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* import/export definition for DaftJson. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array<int, string> |
|
43
|
|
|
*/ |
|
44
|
|
|
const JSON_PROPERTIES = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* List of sortable properties for DaftSortableObject. |
|
48
|
|
|
* |
|
49
|
|
|
* @var array<int, string> |
|
50
|
|
|
*/ |
|
51
|
|
|
const SORTABLE_PROPERTIES = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array<string, array<int, string>> |
|
55
|
|
|
*/ |
|
56
|
|
|
const CHANGE_OTHER_PROPERTIES = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var array<string, array<int, string>> |
|
60
|
|
|
*/ |
|
61
|
|
|
const PROPERTIES_WITH_MULTI_TYPED_ARRAYS = []; |
|
62
|
|
|
|
|
63
|
|
|
public function __get(string $property) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->DoGetSet($property, false); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function __set(string $property, $v) : void |
|
69
|
486 |
|
{ |
|
70
|
|
|
$this->DoGetSet($property, true, $v); |
|
71
|
486 |
|
} |
|
72
|
486 |
|
|
|
73
|
486 |
|
/** |
|
74
|
|
|
* @see static::NudgePropertyValue() |
|
75
|
482 |
|
*/ |
|
76
|
|
|
public function __unset(string $property) : void |
|
77
|
94 |
|
{ |
|
78
|
|
|
$this->NudgePropertyValue($property, null); |
|
79
|
94 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function __debugInfo() : array |
|
82
|
188 |
|
{ |
|
83
|
|
|
$getters = static::DaftObjectPublicGetters(); |
|
84
|
188 |
|
$exportables = static::DaftObjectExportableProperties(); |
|
85
|
172 |
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var array<int, string> |
|
88
|
|
|
*/ |
|
89
|
|
|
$properties = array_filter($exportables, function (string $prop) use ($getters) : bool { |
|
90
|
36 |
|
return |
|
91
|
|
|
$this->__isset($prop) && |
|
92
|
36 |
|
in_array($prop, $getters, DefinitionAssistant::IN_ARRAY_STRICT_MODE); |
|
93
|
32 |
|
}); |
|
94
|
|
|
|
|
95
|
72 |
|
/** |
|
96
|
|
|
* @var array<string, mixed> |
|
97
|
72 |
|
*/ |
|
98
|
72 |
|
$out = array_combine($properties, array_map( |
|
99
|
|
|
/** |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
*/ |
|
102
|
|
|
function (string $prop) { |
|
103
|
|
|
return $this->__get($prop); |
|
104
|
|
|
}, |
|
105
|
70 |
|
$properties |
|
106
|
70 |
|
)); |
|
107
|
72 |
|
|
|
108
|
|
|
return $out; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function CompareToDaftSortableObject(DaftSortableObject $otherObject) : int |
|
112
|
72 |
|
{ |
|
113
|
|
|
if ( ! is_a(static::class, DaftSortableObject::class, true)) { |
|
114
|
|
|
throw new ClassDoesNotImplementClassException( |
|
115
|
|
|
static::class, |
|
116
|
|
|
DaftSortableObject::class |
|
117
|
58 |
|
); |
|
118
|
72 |
|
} |
|
119
|
72 |
|
|
|
120
|
|
|
foreach (static::DaftSortableObjectProperties() as $property) { |
|
121
|
|
|
$sort = $this->__get($property) <=> $otherObject->__get($property); |
|
122
|
72 |
|
|
|
123
|
|
|
if (0 !== $sort) { |
|
124
|
|
|
return $sort; |
|
125
|
4 |
|
} |
|
126
|
|
|
} |
|
127
|
4 |
|
|
|
128
|
2 |
|
return 0; |
|
129
|
2 |
|
} |
|
130
|
2 |
|
|
|
131
|
|
|
public static function DaftObjectProperties() : array |
|
132
|
|
|
{ |
|
133
|
|
|
/** |
|
134
|
2 |
|
* @var array<int, string> |
|
135
|
2 |
|
*/ |
|
136
|
|
|
$out = static::PROPERTIES; |
|
137
|
2 |
|
|
|
138
|
2 |
|
return $out; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public static function DaftObjectNullableProperties() : array |
|
142
|
2 |
|
{ |
|
143
|
|
|
/** |
|
144
|
|
|
* @var array<int, string> |
|
145
|
696 |
|
*/ |
|
146
|
|
|
$out = static::NULLABLE_PROPERTIES; |
|
147
|
|
|
|
|
148
|
|
|
return $out; |
|
149
|
|
|
} |
|
150
|
696 |
|
|
|
151
|
|
|
public static function DaftObjectExportableProperties() : array |
|
152
|
696 |
|
{ |
|
153
|
|
|
/** |
|
154
|
|
|
* @var array<int, string> |
|
155
|
418 |
|
*/ |
|
156
|
|
|
$out = static::EXPORTABLE_PROPERTIES; |
|
157
|
|
|
|
|
158
|
|
|
return $out; |
|
159
|
|
|
} |
|
160
|
418 |
|
|
|
161
|
|
|
public static function DaftObjectPublicGetters() : array |
|
162
|
418 |
|
{ |
|
163
|
|
|
return TypeUtilities::DaftObjectPublicGetters(static::class); |
|
164
|
|
|
} |
|
165
|
148 |
|
|
|
166
|
|
|
public static function DaftObjectPublicOrProtectedGetters() : array |
|
167
|
|
|
{ |
|
168
|
|
|
return TypeUtilities::DaftObjectPublicOrProtectedGetters(static::class); |
|
169
|
|
|
} |
|
170
|
148 |
|
|
|
171
|
|
|
public static function DaftObjectPublicSetters() : array |
|
172
|
148 |
|
{ |
|
173
|
|
|
return TypeUtilities::DaftObjectPublicSetters(static::class); |
|
174
|
|
|
} |
|
175
|
122 |
|
|
|
176
|
|
|
/** |
|
177
|
122 |
|
* @return array<int|string, string> |
|
178
|
|
|
*/ |
|
179
|
|
|
public static function DaftObjectJsonProperties() : array |
|
180
|
2 |
|
{ |
|
181
|
|
|
/** |
|
182
|
2 |
|
* @var array<int|string, string> |
|
183
|
|
|
*/ |
|
184
|
|
|
$out = JsonTypeUtilities::ThrowIfNotDaftJson( |
|
185
|
188 |
|
static::class |
|
186
|
|
|
)::JSON_PROPERTIES; |
|
187
|
188 |
|
|
|
188
|
|
|
return $out; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @return array<int, string> |
|
193
|
122 |
|
*/ |
|
194
|
|
|
final public static function DaftObjectJsonPropertyNames() : array |
|
195
|
|
|
{ |
|
196
|
|
|
/** |
|
197
|
|
|
* @var array<int, string> |
|
198
|
122 |
|
*/ |
|
199
|
122 |
|
$out = []; |
|
200
|
38 |
|
|
|
201
|
|
|
/** |
|
202
|
38 |
|
* @var array<int|string, string> |
|
203
|
|
|
*/ |
|
204
|
|
|
$jsonProperties = static::DaftObjectJsonProperties(); |
|
205
|
|
|
|
|
206
|
|
|
foreach ($jsonProperties as $k => $prop) { |
|
207
|
|
|
if (is_string($k)) { |
|
208
|
80 |
|
$prop = $k; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$out[] = $prop; |
|
212
|
|
|
} |
|
213
|
80 |
|
|
|
214
|
|
|
return $out; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
80 |
|
* @return array<int, string> |
|
219
|
|
|
*/ |
|
220
|
38 |
|
public static function DaftSortableObjectProperties() : array |
|
221
|
38 |
|
{ |
|
222
|
24 |
|
if ( ! is_a(static::class, DaftSortableObject::class, true)) { |
|
223
|
|
|
throw new ClassDoesNotImplementClassException( |
|
224
|
|
|
static::class, |
|
225
|
38 |
|
DaftSortableObject::class |
|
226
|
|
|
); |
|
227
|
|
|
} |
|
228
|
38 |
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @var array<int, string> |
|
231
|
|
|
*/ |
|
232
|
|
|
$out = static::SORTABLE_PROPERTIES; |
|
233
|
|
|
|
|
234
|
42 |
|
return $out; |
|
235
|
|
|
} |
|
236
|
42 |
|
|
|
237
|
38 |
|
/** |
|
238
|
38 |
|
* @return array<string, array<int, string>> |
|
239
|
38 |
|
*/ |
|
240
|
|
|
public static function DaftObjectPropertiesChangeOtherProperties() : array |
|
241
|
|
|
{ |
|
242
|
|
|
/** |
|
243
|
|
|
* @var array<string, array<int, string>> |
|
244
|
|
|
*/ |
|
245
|
|
|
$out = static::CHANGE_OTHER_PROPERTIES; |
|
246
|
4 |
|
|
|
247
|
|
|
return $out; |
|
248
|
4 |
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @return array<string, array<int, string>> |
|
252
|
|
|
*/ |
|
253
|
|
|
public static function DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() : array |
|
254
|
76 |
|
{ |
|
255
|
|
|
if ( |
|
256
|
|
|
! is_a( |
|
257
|
|
|
static::class, |
|
258
|
|
|
DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class, |
|
259
|
76 |
|
true |
|
260
|
|
|
) |
|
261
|
76 |
|
) { |
|
262
|
|
|
throw new ClassDoesNotImplementClassException( |
|
263
|
|
|
static::class, |
|
264
|
|
|
DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class |
|
265
|
|
|
); |
|
266
|
|
|
} |
|
267
|
70 |
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @var array<string, array<int, string>> |
|
270
|
70 |
|
*/ |
|
271
|
70 |
|
$out = static::PROPERTIES_WITH_MULTI_TYPED_ARRAYS; |
|
272
|
70 |
|
|
|
273
|
70 |
|
return $out; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
54 |
|
/** |
|
277
|
54 |
|
* Nudge the state of a given property, marking it as dirty. |
|
278
|
54 |
|
* |
|
279
|
|
|
* @param string $property property being nudged |
|
280
|
|
|
* @param scalar|array|object|null $value value to nudge property with |
|
281
|
|
|
* |
|
282
|
|
|
* @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties() |
|
283
|
|
|
* @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties() |
|
284
|
|
|
* @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed |
|
285
|
16 |
|
*/ |
|
286
|
|
|
abstract protected function NudgePropertyValue(string $property, $value) : void; |
|
287
|
16 |
|
|
|
288
|
|
|
protected function MaybeThrowOnDoGetSet(string $property, bool $setter, array $props) : void |
|
289
|
|
|
{ |
|
290
|
|
|
if ( ! in_array($property, $props, DefinitionAssistant::IN_ARRAY_STRICT_MODE)) { |
|
291
|
|
|
if ( |
|
292
|
|
|
! in_array( |
|
293
|
|
|
$property, |
|
294
|
|
|
static::DaftObjectProperties(), |
|
295
|
|
|
DefinitionAssistant::IN_ARRAY_STRICT_MODE |
|
296
|
|
|
) |
|
297
|
|
|
) { |
|
298
|
|
|
throw new UndefinedPropertyException(static::class, $property); |
|
299
|
|
|
} elseif ($setter) { |
|
300
|
|
|
throw new NotPublicSetterPropertyException(static::class, $property); |
|
301
|
|
|
} |
|
302
|
240 |
|
|
|
303
|
|
|
throw new NotPublicGetterPropertyException(static::class, $property); |
|
304
|
240 |
|
} |
|
305
|
|
|
} |
|
306
|
12 |
|
|
|
307
|
12 |
|
/** |
|
308
|
12 |
|
* @param mixed $v |
|
309
|
12 |
|
* |
|
310
|
|
|
* @return scalar|array|object|null |
|
311
|
|
|
*/ |
|
312
|
8 |
|
protected function DoGetSet(string $property, bool $setter, $v = null) |
|
313
|
4 |
|
{ |
|
314
|
2 |
|
$props = $setter ? static::DaftObjectPublicSetters() : static::DaftObjectPublicGetters(); |
|
315
|
|
|
|
|
316
|
|
|
$this->MaybeThrowOnDoGetSet($property, $setter, $props); |
|
317
|
2 |
|
|
|
318
|
|
|
/** |
|
319
|
228 |
|
* @var callable |
|
320
|
|
|
*/ |
|
321
|
|
|
$callable = [$this, TypeUtilities::MethodNameFromProperty($property, $setter)]; |
|
322
|
|
|
$closure = Closure::fromCallable($callable); |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @var scalar|array|object|null |
|
326
|
238 |
|
*/ |
|
327
|
|
|
$out = $closure->__invoke($v); |
|
328
|
238 |
|
|
|
329
|
|
|
return $out; |
|
330
|
238 |
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|