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