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