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