|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author SignpostMarv |
|
4
|
|
|
*/ |
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftObject; |
|
8
|
|
|
|
|
9
|
|
|
use Closure; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use SignpostMarv\DaftMagicPropertyAnalysis\DefinitionAssistant as Base; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @template T as DaftObject |
|
15
|
|
|
* |
|
16
|
|
|
* @template-extends Base<T> |
|
17
|
|
|
*/ |
|
18
|
|
|
class DefinitionAssistant extends Base |
|
19
|
|
|
{ |
|
20
|
|
|
const BOOL_EXPECTING_GETTER = false; |
|
21
|
|
|
|
|
22
|
|
|
const BOOL_EXPECTING_SETTER = true; |
|
23
|
|
|
|
|
24
|
|
|
const INT_ARRAY_INDEX_TYPE = 0; |
|
25
|
|
|
|
|
26
|
|
|
const INT_ARRAY_INDEX_GETTER = 1; |
|
27
|
|
|
|
|
28
|
|
|
const INT_ARRAY_INDEX_SETTER = 2; |
|
29
|
|
|
|
|
30
|
|
|
const IS_A_STRINGS = true; |
|
31
|
|
|
|
|
32
|
|
|
const COUNT_IS_EMPTY = 0; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @template A as AbstractDaftObject |
|
36
|
|
|
* |
|
37
|
|
|
* @psalm-param class-string<A> $maybe |
|
38
|
|
|
* |
|
39
|
|
|
* @psalm-return class-string<A> |
|
40
|
|
|
*/ |
|
41
|
68 |
|
public static function RegisterAbstractDaftObjectType(string $maybe) : string |
|
42
|
|
|
{ |
|
43
|
|
|
/** |
|
44
|
|
|
* @var array<int, string> |
|
45
|
|
|
*/ |
|
46
|
68 |
|
$props = $maybe::DaftObjectProperties(); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @psalm-var class-string<A> |
|
50
|
|
|
*/ |
|
51
|
68 |
|
$maybe = static::RegisterDaftObjectTypeFromTypeAndProps($maybe, ...$props); |
|
52
|
|
|
|
|
53
|
68 |
|
return $maybe; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
* |
|
59
|
|
|
* @psalm-param class-string<T>|T $maybe |
|
60
|
|
|
*/ |
|
61
|
64 |
|
public static function ObtainExpectedProperties($maybe) : array |
|
62
|
|
|
{ |
|
63
|
|
|
/** |
|
64
|
|
|
* @psalm-var class-string<T> |
|
65
|
|
|
*/ |
|
66
|
64 |
|
$maybe = is_string($maybe) ? $maybe : get_class($maybe); |
|
67
|
|
|
|
|
68
|
64 |
|
$maybe = self::MaybeRegisterAdditionalTypes($maybe); |
|
69
|
|
|
|
|
70
|
64 |
|
return parent::ObtainExpectedProperties($maybe); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @psalm-return Closure(string):?string |
|
75
|
|
|
*/ |
|
76
|
68 |
|
public static function SetterOrGetterClosure( |
|
77
|
|
|
string $type, |
|
78
|
|
|
bool $SetNotGet, |
|
79
|
|
|
string ...$props |
|
80
|
|
|
) : Closure { |
|
81
|
|
|
return function (string $property) use ($type, $props, $SetNotGet) : ? string { |
|
82
|
4 |
|
if (in_array($property, $props, self::IN_ARRAY_STRICT_MODE)) { |
|
83
|
|
|
/** |
|
84
|
|
|
* @var string |
|
85
|
|
|
*/ |
|
86
|
4 |
|
$method = TypeUtilities::MethodNameFromProperty($property, $SetNotGet); |
|
87
|
|
|
|
|
88
|
4 |
|
if (method_exists($type, $method)) { |
|
89
|
4 |
|
return $method; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
return null; |
|
94
|
68 |
|
}; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param scalar|array|object|null $value |
|
99
|
|
|
* |
|
100
|
|
|
* @return array<int, scalar|array|object|null> filtered $value |
|
101
|
|
|
*/ |
|
102
|
20 |
|
public static function MaybeThrowIfValueDoesNotMatchMultiTypedArray( |
|
103
|
|
|
bool $autoTrimStrings, |
|
104
|
|
|
bool $throwIfNotUnique, |
|
105
|
|
|
$value, |
|
106
|
|
|
string ...$types |
|
107
|
|
|
) : array { |
|
108
|
20 |
|
if ( ! is_array($value)) { |
|
109
|
4 |
|
throw new InvalidArgumentException( |
|
110
|
|
|
'Argument 3 passed to ' . |
|
111
|
|
|
__METHOD__ . |
|
112
|
|
|
' must be an array, ' . |
|
113
|
4 |
|
(is_object($value) ? get_class($value) : gettype($value)) . |
|
114
|
4 |
|
' given!' |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @var (scalar|array|object|null)[] |
|
120
|
|
|
*/ |
|
121
|
16 |
|
$value = $value; |
|
122
|
|
|
|
|
123
|
16 |
|
return static::MaybeThrowIfValueDoesNotMatchMultiTypedArrayValueArray( |
|
124
|
12 |
|
$autoTrimStrings, |
|
125
|
8 |
|
$throwIfNotUnique, |
|
126
|
8 |
|
$value, |
|
127
|
7 |
|
...$types |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @psalm-param class-string<T> $maybe |
|
133
|
|
|
* |
|
134
|
|
|
* @psalm-return class-string<T> |
|
135
|
|
|
*/ |
|
136
|
68 |
|
protected static function RegisterDaftObjectTypeFromTypeAndProps( |
|
137
|
|
|
string $maybe, |
|
138
|
|
|
string $prop, |
|
139
|
|
|
string ...$props |
|
140
|
|
|
) : string { |
|
141
|
68 |
|
array_unshift($props, $prop); |
|
142
|
|
|
|
|
143
|
68 |
|
$args = static::TypeAndGetterAndSetterClosureWithProps($maybe, ...$props); |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @var array<int, string> |
|
147
|
|
|
*/ |
|
148
|
68 |
|
$props = array_slice($args, 3); |
|
149
|
|
|
|
|
150
|
68 |
|
static::RegisterType($args[self::INT_ARRAY_INDEX_TYPE], $args[self::INT_ARRAY_INDEX_GETTER], $args[self::INT_ARRAY_INDEX_SETTER], ...$props); |
|
151
|
|
|
|
|
152
|
68 |
|
self::MaybeRegisterAdditionalTypes($args[self::INT_ARRAY_INDEX_TYPE]); |
|
153
|
|
|
|
|
154
|
68 |
|
return $maybe; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @psalm-param class-string<T> $maybe |
|
159
|
|
|
* |
|
160
|
|
|
* @psalm-return class-string<T> |
|
161
|
|
|
*/ |
|
162
|
68 |
|
protected static function MaybeRegisterAdditionalTypes(string $maybe) : string |
|
163
|
|
|
{ |
|
164
|
68 |
|
return $maybe; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @psalm-param class-string<T> $type |
|
169
|
|
|
* |
|
170
|
|
|
* @psalm-return array{0:class-string<T>, 1:null|Closure(string):?string, 2:null|Closure(string):?string, 4:string} |
|
171
|
|
|
*/ |
|
172
|
68 |
|
private static function TypeAndGetterAndSetterClosureWithProps( |
|
173
|
|
|
string $type, |
|
174
|
|
|
string ...$props |
|
175
|
|
|
) : array { |
|
176
|
|
|
/** |
|
177
|
|
|
* @psalm-var array{0:class-string<T>, 1:null|Closure(string):?string, 2:null|Closure(string):?string, 4:string} |
|
178
|
|
|
*/ |
|
179
|
68 |
|
$out = array_merge( |
|
180
|
|
|
[ |
|
181
|
68 |
|
$type, |
|
182
|
68 |
|
static::SetterOrGetterClosure($type, self::BOOL_EXPECTING_GETTER, ...$props), |
|
183
|
68 |
|
static::SetterOrGetterClosure($type, self::BOOL_EXPECTING_SETTER, ...$props), |
|
184
|
|
|
], |
|
185
|
34 |
|
$props |
|
186
|
|
|
); |
|
187
|
|
|
|
|
188
|
68 |
|
return $out; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param (scalar|array|object|null)[] $value |
|
193
|
|
|
* |
|
194
|
|
|
* @return array<int, scalar|array|object|null> filtered $value |
|
195
|
|
|
*/ |
|
196
|
16 |
|
private static function MaybeThrowIfValueDoesNotMatchMultiTypedArrayValueArray( |
|
197
|
|
|
bool $autoTrimStrings, |
|
198
|
|
|
bool $throwIfNotUnique, |
|
199
|
|
|
array $value, |
|
200
|
|
|
string ...$types |
|
201
|
|
|
) : array { |
|
202
|
16 |
|
$value = static::MaybeThrowIfNotArrayIntKeys($value); |
|
203
|
12 |
|
$value = static::MaybeThrowIfValueArrayDoesNotMatchTypes($value, ...$types); |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @var (scalar|array|object|null)[] |
|
207
|
|
|
*/ |
|
208
|
8 |
|
$value = static::MaybeRemapStringsToTrimmedStrings($value, $autoTrimStrings, ...$types); |
|
209
|
|
|
|
|
210
|
8 |
|
$initialCount = count($value); |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @var array<int, scalar|array|object|null> |
|
214
|
|
|
*/ |
|
215
|
8 |
|
$out = []; |
|
216
|
|
|
|
|
217
|
8 |
|
foreach ($value as $maybe) { |
|
218
|
8 |
|
if ( ! in_array($maybe, $out, true)) { |
|
219
|
8 |
|
$out[] = $maybe; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
8 |
|
if ($throwIfNotUnique && count($out) !== $initialCount) { |
|
224
|
4 |
|
throw new InvalidArgumentException( |
|
225
|
|
|
'Argument 3 passed to ' . |
|
226
|
|
|
__METHOD__ . |
|
227
|
4 |
|
' contained non-unique values!' |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
4 |
|
return $out; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @return array<int, scalar|array|object|null> filtered $value |
|
236
|
|
|
*/ |
|
237
|
16 |
|
private static function MaybeThrowIfNotArrayIntKeys(array $value) : array |
|
238
|
|
|
{ |
|
239
|
16 |
|
$initialCount = count($value); |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @var array<int, scalar|array|object|null> |
|
243
|
|
|
*/ |
|
244
|
16 |
|
$value = array_filter($value, 'is_int', ARRAY_FILTER_USE_KEY); |
|
245
|
|
|
|
|
246
|
16 |
|
if (count($value) !== $initialCount) { |
|
247
|
4 |
|
throw new InvalidArgumentException( |
|
248
|
|
|
'Argument 3 passed to ' . |
|
249
|
|
|
__METHOD__ . |
|
250
|
4 |
|
' must be array<int, scalar|array|object|null>' |
|
251
|
|
|
); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
12 |
|
return $value; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @param array<int, scalar|array|object|null> $value |
|
259
|
|
|
* |
|
260
|
|
|
* @return array<int, scalar|array|object|null> filtered $value |
|
261
|
|
|
*/ |
|
262
|
12 |
|
private static function MaybeThrowIfValueArrayDoesNotMatchTypes( |
|
263
|
|
|
array $value, |
|
264
|
|
|
string ...$types |
|
265
|
|
|
) : array { |
|
266
|
12 |
|
$initialCount = count($value); |
|
267
|
|
|
|
|
268
|
12 |
|
$value = array_filter( |
|
269
|
9 |
|
$value, |
|
270
|
|
|
/** |
|
271
|
|
|
* @param scalar|array|object|null $maybe |
|
272
|
|
|
*/ |
|
273
|
|
|
function ($maybe) use ($types) : bool { |
|
274
|
12 |
|
if (is_object($maybe)) { |
|
275
|
8 |
|
foreach ($types as $maybeType) { |
|
276
|
8 |
|
if (is_a($maybe, $maybeType)) { |
|
277
|
7 |
|
return true; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
4 |
|
return false; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
4 |
|
return in_array( |
|
285
|
3 |
|
gettype($maybe), |
|
286
|
2 |
|
$types, |
|
287
|
4 |
|
DefinitionAssistant::IN_ARRAY_STRICT_MODE |
|
288
|
|
|
); |
|
289
|
12 |
|
} |
|
290
|
|
|
); |
|
291
|
|
|
|
|
292
|
12 |
|
if (count($value) !== $initialCount) { |
|
293
|
4 |
|
throw new InvalidArgumentException( |
|
294
|
|
|
'Argument 3 passed to ' . |
|
295
|
|
|
__METHOD__ . |
|
296
|
4 |
|
' contained values that did not match the provided types!' |
|
297
|
|
|
); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
8 |
|
return $value; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* @param array<int, scalar|array|object|null> $value |
|
305
|
|
|
* |
|
306
|
|
|
* @return array<int, scalar|array|object|null> |
|
307
|
|
|
*/ |
|
308
|
8 |
|
private static function MaybeRemapStringsToTrimmedStrings( |
|
309
|
|
|
array $value, |
|
310
|
|
|
bool $autoTrimStrings, |
|
311
|
|
|
string ...$types |
|
312
|
|
|
) : array { |
|
313
|
|
|
if ( |
|
314
|
8 |
|
$autoTrimStrings && |
|
315
|
7 |
|
in_array('string', $types, DefinitionAssistant::IN_ARRAY_STRICT_MODE) |
|
316
|
|
|
) { |
|
317
|
4 |
|
$value = array_map( |
|
318
|
|
|
/** |
|
319
|
|
|
* @param scalar|array|object|null $maybe |
|
320
|
|
|
* |
|
321
|
|
|
* @return scalar|array|object|null |
|
322
|
|
|
*/ |
|
323
|
|
|
function ($maybe) { |
|
324
|
4 |
|
return is_string($maybe) ? trim($maybe) : $maybe; |
|
325
|
4 |
|
}, |
|
326
|
2 |
|
$value |
|
327
|
|
|
); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
8 |
|
return $value; |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|