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