|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LM\Common\Type; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use LM\Common\Enum\Scalar; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait for checking types. |
|
12
|
|
|
* |
|
13
|
|
|
* @todo Turn it into a class instead? |
|
14
|
|
|
* @deprecated |
|
15
|
|
|
*/ |
|
16
|
|
|
trait TypeCheckerTrait |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $type The type to check. |
|
20
|
|
|
* @return bool Whether the type is the array scalar type. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function isArrayType(string $type): bool |
|
23
|
|
|
{ |
|
24
|
|
|
return Scalar::_ARRAY === $type; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $type The type to check. |
|
29
|
|
|
* @return bool Whether the type is the boolean scalar type. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function isBoolType(string $type): bool |
|
32
|
|
|
{ |
|
33
|
|
|
return Scalar::_BOOL === $type; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $type The type to check. |
|
38
|
|
|
* @return bool Whether the type is the FQCN of a class or of an interface. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function isClassOrInterfaceName(string $type): bool |
|
41
|
|
|
{ |
|
42
|
|
|
return class_exists($type) || interface_exists($type); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $type The type to check. |
|
47
|
|
|
* @return bool Whether the type is the scalar integer type. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function isIntegerType(string $type): bool |
|
50
|
|
|
{ |
|
51
|
|
|
return Scalar::_INT === $type; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $type The type to check. |
|
56
|
|
|
* @return bool Whether the type is the string type. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function isStringType(string $type): bool |
|
59
|
|
|
{ |
|
60
|
|
|
return Scalar::_STR === $type; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Throws an exception if the value is not of the expected type. |
|
65
|
|
|
* @param mixed $value The value to check. |
|
66
|
|
|
* @param string $type The type to check against. |
|
67
|
|
|
* @throws InvalidArgumentException if the value is not of the specified |
|
68
|
|
|
* type. |
|
69
|
|
|
* @todo Use is_a or instanceof instead? |
|
70
|
|
|
*/ |
|
71
|
|
|
public function checkType($value, string $type): void |
|
72
|
|
|
{ |
|
73
|
|
|
if ($this->isArrayType($type)) { |
|
74
|
|
|
if (!is_array($value)) { |
|
75
|
|
|
throw new InvalidArgumentException(); |
|
76
|
|
|
} |
|
77
|
|
|
} elseif ($this->isStringType($type)) { |
|
78
|
|
|
if (!is_string($value)) { |
|
79
|
|
|
throw new InvalidArgumentException(); |
|
80
|
|
|
} |
|
81
|
|
|
} elseif ($this->isIntegerType($type)) { |
|
82
|
|
|
if (!is_int($value)) { |
|
83
|
|
|
throw new InvalidArgumentException(); |
|
84
|
|
|
} |
|
85
|
|
|
} elseif ($this->isBoolType($type)) { |
|
86
|
|
|
if (!is_bool($value)) { |
|
87
|
|
|
throw new InvalidArgumentException(); |
|
88
|
|
|
} |
|
89
|
|
|
} elseif ($this->isClassOrInterfaceName($type)) { |
|
90
|
|
|
if (!is_object($value) || !is_a($value, $type)) { |
|
91
|
|
|
throw new InvalidArgumentException(); |
|
92
|
|
|
} |
|
93
|
|
|
} else { |
|
94
|
|
|
throw new InvalidArgumentException(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|