1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author SignpostMarv |
4
|
|
|
*/ |
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftObject; |
8
|
|
|
|
9
|
|
|
use ReflectionException; |
10
|
|
|
use ReflectionMethod; |
11
|
|
|
|
12
|
|
|
class TypeUtilities |
13
|
|
|
{ |
14
|
|
|
const BOOL_EXPECTING_NON_PUBLIC_METHOD = false; |
15
|
|
|
|
16
|
|
|
const BOOL_EXPECTING_GETTER = false; |
17
|
|
|
|
18
|
|
|
const BOOL_DEFAULT_THROWIFNOTIMPLEMENTATION = false; |
19
|
|
|
|
20
|
|
|
const BOOL_DEFAULT_EXPECTING_NON_PUBLIC_METHOD = true; |
21
|
|
|
|
22
|
|
|
const BOOL_METHOD_IS_PUBLIC = true; |
23
|
|
|
|
24
|
|
|
const BOOL_METHOD_IS_NON_PUBLIC = false; |
25
|
|
|
|
26
|
|
|
const BOOL_DEFAULT_SET_NOT_GET = false; |
27
|
|
|
|
28
|
|
|
const BOOL_DOES_NOT_HAVE_METHOD = false; |
29
|
|
|
|
30
|
|
|
const SUPPORTED_INVALID_LEADING_CHARACTERS = [ |
31
|
|
|
'@', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
const BOOL_METHOD_IS_NOT_STATIC = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array<string, array<string, bool>> |
38
|
|
|
* |
39
|
|
|
* @psalm-var array<class-string<DaftObject>, array<string, bool>> |
40
|
|
|
*/ |
41
|
|
|
private static $Getters = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array<string, array<int, string>> |
45
|
|
|
* |
46
|
|
|
* @psalm-var array<class-string<DaftObject>, array<int, string>> |
47
|
|
|
*/ |
48
|
|
|
private static $publicSetters = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @psalm-param class-string<DaftObject> $class |
52
|
|
|
* |
53
|
|
|
* @return array<int, string> |
54
|
|
|
*/ |
55
|
556 |
|
public static function DaftObjectPublicGetters(string $class) : array |
56
|
|
|
{ |
57
|
556 |
|
static::CachePublicGettersAndSetters($class); |
58
|
|
|
|
59
|
556 |
|
return array_keys(array_filter(self::$Getters[$class])); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @psalm-param class-string<DaftObject> $class |
64
|
|
|
* |
65
|
|
|
* @return array<int, string> |
66
|
|
|
*/ |
67
|
76 |
|
public static function DaftObjectPublicOrProtectedGetters(string $class) : array |
68
|
|
|
{ |
69
|
76 |
|
static::CachePublicGettersAndSetters($class); |
70
|
|
|
|
71
|
76 |
|
return array_keys(self::$Getters[$class]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @psalm-param class-string<DaftObject> $class |
76
|
|
|
*/ |
77
|
368 |
|
public static function DaftObjectPublicSetters(string $class) : array |
78
|
|
|
{ |
79
|
368 |
|
static::CachePublicGettersAndSetters($class); |
80
|
|
|
|
81
|
368 |
|
return self::$publicSetters[$class]; |
82
|
|
|
} |
83
|
|
|
|
84
|
886 |
|
public static function MethodNameFromProperty( |
85
|
|
|
string $prop, |
86
|
|
|
bool $SetNotGet = self::BOOL_DEFAULT_SET_NOT_GET |
87
|
|
|
) : string { |
88
|
|
|
if ( |
89
|
886 |
|
in_array( |
90
|
886 |
|
mb_substr($prop, 0, 1), |
91
|
886 |
|
self::SUPPORTED_INVALID_LEADING_CHARACTERS, |
92
|
886 |
|
DefinitionAssistant::IN_ARRAY_STRICT_MODE |
93
|
|
|
) |
94
|
|
|
) { |
95
|
69 |
|
return ($SetNotGet ? 'Alter' : 'Obtain') . ucfirst(mb_substr($prop, 1)); |
96
|
|
|
} |
97
|
|
|
|
98
|
874 |
|
return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop); |
99
|
|
|
} |
100
|
|
|
|
101
|
39 |
|
public static function HasMethod( |
102
|
|
|
string $class, |
103
|
|
|
string $property, |
104
|
|
|
bool $SetNotGet, |
105
|
|
|
bool $pub = self::BOOL_DEFAULT_EXPECTING_NON_PUBLIC_METHOD |
106
|
|
|
) : bool { |
107
|
39 |
|
$method = static::MethodNameFromProperty($property, $SetNotGet); |
108
|
|
|
|
109
|
|
|
try { |
110
|
39 |
|
$ref = new ReflectionMethod($class, $method); |
111
|
|
|
|
112
|
|
|
return |
113
|
35 |
|
($pub ? $ref->isPublic() : $ref->isProtected()) && |
114
|
35 |
|
self::BOOL_METHOD_IS_NOT_STATIC === $ref->isStatic(); |
115
|
19 |
|
} catch (ReflectionException $e) { |
116
|
19 |
|
return self::BOOL_DOES_NOT_HAVE_METHOD; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @template T as string |
122
|
|
|
* |
123
|
|
|
* @param scalar|array|object|null $value |
124
|
|
|
* |
125
|
|
|
* @psalm-param class-string<DaftObject> $class_name |
126
|
|
|
*/ |
127
|
80 |
|
public static function ExpectRetrievedValueIsString( |
128
|
|
|
string $property, |
129
|
|
|
$value, |
130
|
|
|
string $class_name |
131
|
|
|
) : string { |
132
|
|
|
/** |
133
|
|
|
* @psalm-var T |
134
|
|
|
*/ |
135
|
80 |
|
$value = static::MaybeThrowIfNotType($property, $value, $class_name, 'string'); |
136
|
|
|
|
137
|
76 |
|
return $value; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @template T as array |
142
|
|
|
* |
143
|
|
|
* @param scalar|array|object|null $value |
144
|
|
|
* |
145
|
|
|
* @psalm-param class-string<DaftObject> $class_name |
146
|
|
|
*/ |
147
|
8 |
|
public static function ExpectRetrievedValueIsArray( |
148
|
|
|
string $property, |
149
|
|
|
$value, |
150
|
|
|
string $class_name |
151
|
|
|
) : array { |
152
|
|
|
/** |
153
|
|
|
* @psalm-var T |
154
|
|
|
*/ |
155
|
8 |
|
$value = static::MaybeThrowIfNotType($property, $value, $class_name, 'array'); |
156
|
|
|
|
157
|
4 |
|
return $value; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @template T as int |
162
|
|
|
* |
163
|
|
|
* @param scalar|array|object|null $value |
164
|
|
|
* |
165
|
|
|
* @psalm-param class-string<DaftObject> $class_name |
166
|
|
|
*/ |
167
|
20 |
|
public static function ExpectRetrievedValueIsIntish( |
168
|
|
|
string $property, |
169
|
|
|
$value, |
170
|
|
|
string $class_name |
171
|
|
|
) : int { |
172
|
20 |
|
if (is_string($value) && ctype_digit($value)) { |
173
|
4 |
|
$value = (int) $value; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @psalm-var T |
178
|
|
|
*/ |
179
|
20 |
|
$value = static::MaybeThrowIfNotType($property, $value, $class_name, 'integer'); |
180
|
|
|
|
181
|
16 |
|
return $value; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @template T as float |
186
|
|
|
* |
187
|
|
|
* @param scalar|array|object|null $value |
188
|
|
|
* |
189
|
|
|
* @psalm-param class-string<DaftObject> $class_name |
190
|
|
|
*/ |
191
|
16 |
|
public static function ExpectRetrievedValueIsFloatish( |
192
|
|
|
string $property, |
193
|
|
|
$value, |
194
|
|
|
string $class_name |
195
|
|
|
) : float { |
196
|
16 |
|
if (is_string($value) && is_numeric($value)) { |
197
|
4 |
|
$value = (float) $value; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @psalm-var T |
202
|
|
|
*/ |
203
|
16 |
|
$value = static::MaybeThrowIfNotType($property, $value, $class_name, 'double'); |
204
|
|
|
|
205
|
12 |
|
return $value; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @template T as bool |
210
|
|
|
* |
211
|
|
|
* @param scalar|array|object|null $value |
212
|
|
|
* |
213
|
|
|
* @psalm-param class-string<DaftObject> $class_name |
214
|
|
|
*/ |
215
|
28 |
|
public static function ExpectRetrievedValueIsBoolish( |
216
|
|
|
string $property, |
217
|
|
|
$value, |
218
|
|
|
string $class_name |
219
|
|
|
) : bool { |
220
|
28 |
|
if ('1' === $value || 1 === $value) { |
221
|
8 |
|
return true; |
222
|
20 |
|
} elseif ('0' === $value || 0 === $value) { |
223
|
8 |
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @psalm-var T |
228
|
|
|
*/ |
229
|
12 |
|
$value = static::MaybeThrowIfNotType($property, $value, $class_name, 'boolean'); |
230
|
|
|
|
231
|
8 |
|
return $value; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @psalm-param class-string<DaftObject> $class_name |
236
|
|
|
* |
237
|
|
|
* @param scalar|array|object|null $value |
238
|
|
|
*/ |
239
|
540 |
|
public static function MaybeThrowOnNudge( |
240
|
|
|
string $class_name, |
241
|
|
|
string $property, |
242
|
|
|
$value |
243
|
|
|
) : void { |
244
|
|
|
if ( |
245
|
540 |
|
! in_array( |
246
|
405 |
|
$property, |
247
|
540 |
|
$class_name::DaftObjectProperties(), |
248
|
540 |
|
DefinitionAssistant::IN_ARRAY_STRICT_MODE |
249
|
|
|
) |
250
|
|
|
) { |
251
|
8 |
|
throw Exceptions\Factory::UndefinedPropertyException($class_name, $property); |
252
|
|
|
} elseif ( |
253
|
532 |
|
true === is_null($value) && |
254
|
300 |
|
! in_array( |
255
|
225 |
|
$property, |
256
|
300 |
|
$class_name::DaftObjectNullableProperties(), |
257
|
474 |
|
DefinitionAssistant::IN_ARRAY_STRICT_MODE |
258
|
|
|
) |
259
|
|
|
) { |
260
|
208 |
|
throw Exceptions\Factory::PropertyNotNullableException($class_name, $property); |
261
|
|
|
} |
262
|
324 |
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @template T |
266
|
|
|
* |
267
|
|
|
* @param scalar|array|object|null $value |
268
|
|
|
* |
269
|
|
|
* @psalm-return T |
270
|
|
|
* |
271
|
|
|
* @return mixed |
272
|
|
|
*/ |
273
|
132 |
|
protected static function MaybeThrowIfNotType( |
274
|
|
|
string $property, |
275
|
|
|
$value, |
276
|
|
|
string $class_name, |
277
|
|
|
string $type |
278
|
|
|
) { |
279
|
132 |
|
if ($type !== gettype($value)) { |
280
|
20 |
|
throw Exceptions\Factory::PropertyValueNotOfExpectedTypeException( |
281
|
15 |
|
$class_name, |
282
|
10 |
|
$property, |
283
|
10 |
|
$type |
284
|
|
|
); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @var T |
289
|
|
|
*/ |
290
|
112 |
|
$value = $value; |
291
|
|
|
|
292
|
112 |
|
return $value; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @template T as DaftObject |
297
|
|
|
* |
298
|
|
|
* @psalm-param class-string<T> $class |
299
|
|
|
*/ |
300
|
39 |
|
protected static function CachePublicGettersAndSettersProperties(string $class) : void |
301
|
|
|
{ |
302
|
|
|
if ( |
303
|
39 |
|
is_a($class, AbstractDaftObject::class, true) && |
304
|
39 |
|
DefinitionAssistant::IsTypeUnregistered($class) |
305
|
|
|
) { |
306
|
|
|
/** |
307
|
|
|
* @psalm-var class-string<T> |
308
|
|
|
*/ |
309
|
38 |
|
$class = DefinitionAssistant::RegisterAbstractDaftObjectType($class); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
foreach ( |
313
|
39 |
|
DefinitionAssistant::ObtainExpectedProperties($class) as $prop |
314
|
|
|
) { |
315
|
39 |
|
static::CachePublicGettersAndSettersProperty($class, $prop); |
316
|
|
|
} |
317
|
39 |
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @psalm-param class-string<DaftObject> $class |
321
|
|
|
*/ |
322
|
39 |
|
protected static function CachePublicGettersAndSettersProperty( |
323
|
|
|
string $class, |
324
|
|
|
string $prop |
325
|
|
|
) : void { |
326
|
39 |
|
if (static::HasMethod($class, $prop, self::BOOL_EXPECTING_GETTER)) { |
327
|
31 |
|
self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_PUBLIC; |
328
|
8 |
|
} elseif (static::HasMethod( |
329
|
6 |
|
$class, |
330
|
4 |
|
$prop, |
331
|
8 |
|
self::BOOL_EXPECTING_GETTER, |
332
|
8 |
|
self::BOOL_EXPECTING_NON_PUBLIC_METHOD |
333
|
|
|
)) { |
334
|
4 |
|
self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_NON_PUBLIC; |
335
|
|
|
} |
336
|
|
|
|
337
|
39 |
|
if (static::HasMethod($class, $prop, self::BOOL_METHOD_IS_PUBLIC)) { |
338
|
24 |
|
self::$publicSetters[$class][] = $prop; |
339
|
|
|
} |
340
|
39 |
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @psalm-param class-string<DaftObject> $class |
344
|
|
|
*/ |
345
|
892 |
|
private static function CachePublicGettersAndSetters(string $class) : void |
346
|
|
|
{ |
347
|
892 |
|
if (false === isset(self::$Getters[$class])) { |
348
|
39 |
|
self::$Getters[$class] = []; |
349
|
39 |
|
self::$publicSetters[$class] = []; |
350
|
|
|
|
351
|
|
|
if ( |
352
|
39 |
|
is_a( |
353
|
30 |
|
$class, |
354
|
39 |
|
DefinesOwnIdPropertiesInterface::class, |
355
|
39 |
|
true |
356
|
|
|
) |
357
|
|
|
) { |
358
|
15 |
|
self::$Getters[$class]['id'] = self::BOOL_METHOD_IS_PUBLIC; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* @psalm-var class-string<DaftObject> |
363
|
|
|
*/ |
364
|
39 |
|
$class = $class; |
365
|
|
|
|
366
|
39 |
|
static::CachePublicGettersAndSettersProperties($class); |
367
|
|
|
} |
368
|
892 |
|
} |
369
|
|
|
} |
370
|
|
|
|