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