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