1
|
|
|
<?php |
2
|
|
|
namespace GenericCollections\Utils; |
3
|
|
|
|
4
|
|
|
class TypeChecker |
5
|
|
|
{ |
6
|
|
|
protected $map = [ |
7
|
|
|
'null' => 'checkNullValue', |
8
|
|
|
'mixed' => 'checkMixed', |
9
|
|
|
'scalar' => 'checkScalar', |
10
|
|
|
'numeric' => 'checkNumeric', |
11
|
|
|
'boolean' => 'checkBoolean', |
12
|
|
|
'bool' => 'checkBoolean', |
13
|
|
|
'integer' => 'checkInteger', |
14
|
|
|
'int' => 'checkInteger', |
15
|
|
|
'long' => 'checkInteger', |
16
|
|
|
'float' => 'checkFloat', |
17
|
|
|
'double' => 'checkFloat', |
18
|
|
|
'real' => 'checkFloat', |
19
|
|
|
'string' => 'checkString', |
20
|
|
|
'array' => 'checkArray', |
21
|
|
|
'object' => 'checkObject', |
22
|
|
|
'resource' => 'checkResource', |
23
|
|
|
'callable' => 'checkCallable', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Check if a value match with an specific type |
28
|
|
|
* |
29
|
|
|
* @param string $type |
30
|
|
|
* @param mixed $value |
31
|
|
|
* @param bool $allowNull |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
138 |
|
public function checkType($type, $value, $allowNull = false) |
35
|
|
|
{ |
36
|
138 |
|
if (null === $value && $allowNull) { |
37
|
10 |
|
return true; |
38
|
|
|
} |
39
|
137 |
|
if (! array_key_exists($type, $this->map)) { |
40
|
100 |
|
return $this->checkInstanceOf($value, $type); |
41
|
|
|
} |
42
|
61 |
|
$call = [$this, $this->map[$type]]; |
43
|
61 |
|
return call_user_func($call, $value); |
44
|
|
|
} |
45
|
|
|
|
46
|
100 |
|
protected function checkInstanceOf($value, $type) |
47
|
|
|
{ |
48
|
100 |
|
return ($value instanceof $type); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
protected function checkNullValue($value) |
52
|
|
|
{ |
53
|
1 |
|
return is_null($value); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
protected function checkMixed() |
57
|
|
|
{ |
58
|
1 |
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
protected function checkScalar($value) |
62
|
|
|
{ |
63
|
1 |
|
return is_scalar($value); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
protected function checkNumeric($value) |
67
|
|
|
{ |
68
|
1 |
|
return is_numeric($value); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
protected function checkBoolean($value) |
72
|
|
|
{ |
73
|
1 |
|
return is_bool($value); |
74
|
|
|
} |
75
|
|
|
|
76
|
17 |
|
protected function checkInteger($value) |
77
|
|
|
{ |
78
|
17 |
|
return is_int($value); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
protected function checkFloat($value) |
82
|
|
|
{ |
83
|
1 |
|
return is_float($value); |
84
|
|
|
} |
85
|
|
|
|
86
|
36 |
|
protected function checkString($value) |
87
|
|
|
{ |
88
|
36 |
|
return is_string($value); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
protected function checkArray($value) |
92
|
|
|
{ |
93
|
1 |
|
return is_array($value); |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
protected function checkObject($value) |
97
|
|
|
{ |
98
|
2 |
|
return is_object($value); |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
protected function checkResource($value) |
102
|
|
|
{ |
103
|
1 |
|
return is_resource($value); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
protected function checkCallable($value) |
107
|
|
|
{ |
108
|
1 |
|
return is_callable($value); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|