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