|
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
|
|
|
/** |
|
20
|
|
|
* @param mixed $needle |
|
21
|
|
|
* @param mixed $haystack |
|
22
|
|
|
*/ |
|
23
|
114 |
|
public static function MaybeInMaybeArray($needle, $haystack) : bool |
|
24
|
|
|
{ |
|
25
|
114 |
|
$haystack = self::EnsureArgumentIsArray($haystack, self::INDEX_SECOND_ARG, __METHOD__); |
|
26
|
|
|
|
|
27
|
114 |
|
return static::MaybeInArray($needle, $haystack); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param mixed $needle |
|
32
|
|
|
*/ |
|
33
|
420 |
|
public static function MaybeInArray($needle, array $haystack) : bool |
|
34
|
|
|
{ |
|
35
|
420 |
|
return in_array($needle, $haystack, true); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param mixed $maybe |
|
40
|
|
|
*/ |
|
41
|
358 |
|
public static function EnsureArgumentIsArray($maybe, int $argument = null, string $method = __METHOD__) : array |
|
42
|
|
|
{ |
|
43
|
358 |
|
if ( ! is_array($maybe)) { |
|
44
|
|
|
throw new InvalidArgumentException( |
|
45
|
|
|
'Argument ' . |
|
46
|
|
|
(is_int($argument) ? $argument : self::INDEX_FIRST_ARG) . |
|
47
|
|
|
' passed to ' . |
|
48
|
|
|
$method . |
|
49
|
|
|
' must be an array, ' . |
|
50
|
|
|
(is_object($maybe) ? get_class($maybe) : gettype($maybe)) . |
|
51
|
|
|
' given!' |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
358 |
|
return $maybe; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param mixed $maybe |
|
60
|
|
|
*/ |
|
61
|
20 |
|
public static function ForceArgumentAsArray($maybe) : array |
|
62
|
|
|
{ |
|
63
|
20 |
|
return is_array($maybe) ? $maybe : [$maybe]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param mixed $maybe |
|
68
|
|
|
* |
|
69
|
|
|
* @psalm-suppress InvalidNullableReturnType |
|
70
|
|
|
* @psalm-suppress NullableReturnStatement |
|
71
|
|
|
*/ |
|
72
|
4 |
|
public static function VarExportNonScalars($maybe) : string |
|
73
|
|
|
{ |
|
74
|
4 |
|
if (is_string($maybe)) { |
|
75
|
4 |
|
return $maybe; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return |
|
79
|
2 |
|
is_scalar($maybe) |
|
80
|
2 |
|
? (string) $maybe |
|
81
|
2 |
|
: var_export($maybe, self::BOOL_VAR_EXPORT_RETURN); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param mixed $maybe |
|
86
|
|
|
*/ |
|
87
|
8 |
|
public static function EnsureArgumentIsString($maybe) : string |
|
88
|
|
|
{ |
|
89
|
8 |
|
if ( ! is_string($maybe)) { |
|
90
|
|
|
throw new InvalidArgumentException( |
|
91
|
|
|
'Argument 1 passed to ' . |
|
92
|
|
|
__METHOD__ . |
|
93
|
|
|
' must be a string, ' . |
|
94
|
|
|
(is_object($maybe) ? get_class($maybe) : gettype($maybe)) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
8 |
|
return $maybe; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
528 |
|
public static function IsThingStrings(string $maybe, string $thing) : bool |
|
102
|
|
|
{ |
|
103
|
528 |
|
return is_a($maybe, $thing, true); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public static function IsSubThingStrings(string $maybe, string $thing) : bool |
|
107
|
|
|
{ |
|
108
|
|
|
return is_subclass_of($maybe, $thing, true); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|