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
|
484 |
|
public function __get(string $property) |
64
|
|
|
{ |
65
|
484 |
|
return $this->DoGetSet($property, false); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param scalar|array|object|null $v |
70
|
|
|
*/ |
71
|
368 |
|
public function __set(string $property, $v) : void |
72
|
|
|
{ |
73
|
368 |
|
$this->DoGetSet($property, true, $v); |
74
|
300 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @see static::NudgePropertyValue() |
78
|
|
|
*/ |
79
|
40 |
|
public function __unset(string $property) : void |
80
|
|
|
{ |
81
|
40 |
|
$this->NudgePropertyValue($property, null); |
82
|
40 |
|
} |
83
|
|
|
|
84
|
88 |
|
public function __debugInfo() : array |
85
|
|
|
{ |
86
|
88 |
|
$getters = static::DaftObjectPublicGetters(); |
87
|
88 |
|
$exportables = static::DaftObjectExportableProperties(); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var array<int, string> |
91
|
|
|
*/ |
92
|
|
|
$properties = array_filter($exportables, function (string $prop) use ($getters) : bool { |
93
|
|
|
return |
94
|
80 |
|
$this->__isset($prop) && |
95
|
80 |
|
in_array($prop, $getters, DefinitionAssistant::IN_ARRAY_STRICT_MODE); |
96
|
88 |
|
}); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var array<string, scalar|array|object|null> |
100
|
|
|
*/ |
101
|
88 |
|
$out = array_combine($properties, array_map( |
102
|
|
|
/** |
103
|
|
|
* @return scalar|array|object|null |
104
|
|
|
*/ |
105
|
|
|
function (string $prop) { |
106
|
56 |
|
return $this->__get($prop); |
107
|
88 |
|
}, |
108
|
44 |
|
$properties |
109
|
|
|
)); |
110
|
|
|
|
111
|
88 |
|
return $out; |
112
|
|
|
} |
113
|
|
|
|
114
|
1896 |
|
public static function DaftObjectProperties() : array |
115
|
|
|
{ |
116
|
|
|
/** |
117
|
|
|
* @var array<int, string> |
118
|
|
|
*/ |
119
|
1896 |
|
$out = static::PROPERTIES; |
120
|
|
|
|
121
|
1896 |
|
return $out; |
122
|
|
|
} |
123
|
|
|
|
124
|
832 |
|
public static function DaftObjectNullableProperties() : array |
125
|
|
|
{ |
126
|
|
|
/** |
127
|
|
|
* @var array<int, string> |
128
|
|
|
*/ |
129
|
832 |
|
$out = static::NULLABLE_PROPERTIES; |
130
|
|
|
|
131
|
832 |
|
return $out; |
132
|
|
|
} |
133
|
|
|
|
134
|
320 |
|
public static function DaftObjectExportableProperties() : array |
135
|
|
|
{ |
136
|
|
|
/** |
137
|
|
|
* @var array<int, string> |
138
|
|
|
*/ |
139
|
320 |
|
$out = static::EXPORTABLE_PROPERTIES; |
140
|
|
|
|
141
|
320 |
|
return $out; |
142
|
|
|
} |
143
|
|
|
|
144
|
556 |
|
public static function DaftObjectPublicGetters() : array |
145
|
|
|
{ |
146
|
556 |
|
return TypeUtilities::DaftObjectPublicGetters(static::class); |
147
|
|
|
} |
148
|
|
|
|
149
|
8 |
|
public static function DaftObjectPublicOrProtectedGetters() : array |
150
|
|
|
{ |
151
|
8 |
|
return TypeUtilities::DaftObjectPublicOrProtectedGetters(static::class); |
152
|
|
|
} |
153
|
|
|
|
154
|
368 |
|
public static function DaftObjectPublicSetters() : array |
155
|
|
|
{ |
156
|
368 |
|
return TypeUtilities::DaftObjectPublicSetters(static::class); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return array<int|string, string> |
161
|
|
|
*/ |
162
|
176 |
|
public static function DaftObjectJsonProperties() : array |
163
|
|
|
{ |
164
|
|
|
/** |
165
|
|
|
* @var array<int|string, string> |
166
|
|
|
*/ |
167
|
176 |
|
$out = JsonTypeUtilities::ThrowIfNotDaftJson(static::class)::JSON_PROPERTIES; |
168
|
|
|
|
169
|
104 |
|
return $out; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array<int, string> |
174
|
|
|
*/ |
175
|
176 |
|
final public static function DaftObjectJsonPropertyNames() : array |
176
|
|
|
{ |
177
|
|
|
/** |
178
|
|
|
* @var array<int, string> |
179
|
|
|
*/ |
180
|
176 |
|
$out = []; |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @var array<int|string, string> |
184
|
|
|
*/ |
185
|
176 |
|
$jsonProperties = static::DaftObjectJsonProperties(); |
186
|
|
|
|
187
|
104 |
|
foreach ($jsonProperties as $k => $prop) { |
188
|
104 |
|
if (is_string($k)) { |
189
|
96 |
|
$prop = $k; |
190
|
|
|
} |
191
|
|
|
|
192
|
104 |
|
$out[] = $prop; |
193
|
|
|
} |
194
|
|
|
|
195
|
104 |
|
return $out; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return array<string, array<int, string>> |
200
|
|
|
*/ |
201
|
216 |
|
public static function DaftObjectPropertiesChangeOtherProperties() : array |
202
|
|
|
{ |
203
|
|
|
/** |
204
|
|
|
* @var array<string, array<int, string>> |
205
|
|
|
*/ |
206
|
216 |
|
$out = static::CHANGE_OTHER_PROPERTIES; |
207
|
|
|
|
208
|
216 |
|
return $out; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return array<string, array<int, string>> |
213
|
|
|
*/ |
214
|
184 |
|
public static function DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() : array |
215
|
|
|
{ |
216
|
|
|
if ( |
217
|
184 |
|
! is_a( |
218
|
184 |
|
static::class, |
219
|
184 |
|
DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class, |
220
|
184 |
|
true |
221
|
|
|
) |
222
|
|
|
) { |
223
|
160 |
|
throw Exceptions\Factory::ClassDoesNotImplementClassException( |
224
|
160 |
|
static::class, |
225
|
160 |
|
DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @var array<string, array<int, string>> |
231
|
|
|
*/ |
232
|
24 |
|
$out = static::PROPERTIES_WITH_MULTI_TYPED_ARRAYS; |
233
|
|
|
|
234
|
24 |
|
return $out; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Nudge the state of a given property, marking it as dirty. |
239
|
|
|
* |
240
|
|
|
* @param string $property property being nudged |
241
|
|
|
* @param scalar|array|object|null $value value to nudge property with |
242
|
|
|
* |
243
|
|
|
* @throws Exceptions\UndefinedPropertyException if $property is not in static::DaftObjectProperties() |
244
|
|
|
* @throws Exceptions\PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties() |
245
|
|
|
* @throws Exceptions\PropertyNotRewriteableException if class is write-once read-many and $property was already changed |
246
|
|
|
*/ |
247
|
|
|
abstract protected function NudgePropertyValue(string $property, $value) : void; |
248
|
|
|
|
249
|
816 |
|
protected function MaybeThrowOnDoGetSet(string $property, bool $setter, array $props) : void |
250
|
|
|
{ |
251
|
816 |
|
if ( ! in_array($property, $props, DefinitionAssistant::IN_ARRAY_STRICT_MODE)) { |
252
|
|
|
if ( |
253
|
432 |
|
! in_array( |
254
|
324 |
|
$property, |
255
|
432 |
|
static::DaftObjectProperties(), |
256
|
432 |
|
DefinitionAssistant::IN_ARRAY_STRICT_MODE |
257
|
|
|
) |
258
|
|
|
) { |
259
|
200 |
|
throw Exceptions\Factory::UndefinedPropertyException(static::class, $property); |
260
|
232 |
|
} elseif ($setter) { |
261
|
52 |
|
throw Exceptions\Factory::NotPublicSetterPropertyException(static::class, $property); |
262
|
|
|
} |
263
|
|
|
|
264
|
180 |
|
throw Exceptions\Factory::NotPublicGetterPropertyException(static::class, $property); |
265
|
|
|
} |
266
|
384 |
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param scalar|array|object|null $v |
270
|
|
|
* |
271
|
|
|
* @return scalar|array|object|null |
272
|
|
|
*/ |
273
|
816 |
|
protected function DoGetSet(string $property, bool $setter, $v = null) |
274
|
|
|
{ |
275
|
816 |
|
$props = $setter ? static::DaftObjectPublicSetters() : static::DaftObjectPublicGetters(); |
276
|
|
|
|
277
|
816 |
|
$this->MaybeThrowOnDoGetSet($property, $setter, $props); |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @var callable |
281
|
|
|
*/ |
282
|
384 |
|
$callable = [$this, TypeUtilities::MethodNameFromProperty($property, $setter)]; |
283
|
384 |
|
$closure = Closure::fromCallable($callable); |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @var scalar|array|object|null |
287
|
|
|
*/ |
288
|
384 |
|
$out = $closure->__invoke($v); |
289
|
|
|
|
290
|
368 |
|
return $out; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|