|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the core-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2017 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Library\Core\Argument; |
|
13
|
|
|
|
|
14
|
|
|
use WBW\Library\Core\Exception\Argument\ArrayArgumentException; |
|
15
|
|
|
use WBW\Library\Core\Exception\Argument\BooleanArgumentException; |
|
16
|
|
|
use WBW\Library\Core\Exception\Argument\DateArgumentException; |
|
17
|
|
|
use WBW\Library\Core\Exception\Argument\DoubleArgumentException; |
|
18
|
|
|
use WBW\Library\Core\Exception\Argument\FloatArgumentException; |
|
19
|
|
|
use WBW\Library\Core\Exception\Argument\IllegalArgumentException; |
|
20
|
|
|
use WBW\Library\Core\Exception\Argument\IntegerArgumentException; |
|
21
|
|
|
use WBW\Library\Core\Exception\Argument\NumberArgumentException; |
|
22
|
|
|
use WBW\Library\Core\Exception\Argument\ObjectArgumentException; |
|
23
|
|
|
use WBW\Library\Core\Exception\Argument\ResourceArgumentException; |
|
24
|
|
|
use WBW\Library\Core\Exception\Argument\StringArgumentException; |
|
25
|
|
|
use WBW\Library\Core\Exception\Argument\TimestampArgumentException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Argument validator. |
|
29
|
|
|
* |
|
30
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
31
|
|
|
* @package WBW\Library\Core\Argument |
|
32
|
|
|
* @final |
|
33
|
|
|
*/ |
|
34
|
|
|
final class ArgumentValidator implements ArgumentInterface { |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Determines if a value is an array. |
|
38
|
|
|
* |
|
39
|
|
|
* @param mixed $value The value. |
|
40
|
|
|
* @throws ArrayArgumentException Throws an Array argument exception if the value is not of expected type. |
|
41
|
|
|
*/ |
|
42
|
|
|
private static function isArray($value) { |
|
43
|
|
|
if (false === is_array($value)) { |
|
44
|
|
|
throw new ArrayArgumentException($value); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Determines if a value is a boolean. |
|
50
|
|
|
* |
|
51
|
|
|
* @param mixed $value The value. |
|
52
|
|
|
* @throws BooleanArgumentException Throws a Boolean argument exception if the value is not of expected type. |
|
53
|
|
|
*/ |
|
54
|
|
|
private static function isBoolean($value) { |
|
55
|
|
|
if (false === is_bool($value)) { |
|
56
|
|
|
throw new BooleanArgumentException($value); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Determines if a value is a date. |
|
62
|
|
|
* |
|
63
|
|
|
* @param mixed $value The value. |
|
64
|
|
|
* @throws DateArgumentException Throws a Date argument exception if the value is not of expected type. |
|
65
|
|
|
*/ |
|
66
|
|
|
private static function isDate($value) { |
|
67
|
|
|
if (false === strtotime($value)) { |
|
68
|
|
|
throw new DateArgumentException($value); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Determines if a value is a double. |
|
74
|
|
|
* |
|
75
|
|
|
* @param mixed $value The value. |
|
76
|
|
|
* @throws DoubleArgumentException Throws a Double argument exception if the value is not of expected type. |
|
77
|
|
|
*/ |
|
78
|
|
|
private static function isDouble($value) { |
|
79
|
|
|
if (false === is_double($value)) { |
|
80
|
|
|
throw new DoubleArgumentException($value); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Determines if a value is a float. |
|
86
|
|
|
* |
|
87
|
|
|
* @param mixed $value The value. |
|
88
|
|
|
* @throws FloatArgumentException Throws a Float argument exception if the value is not of expected type. |
|
89
|
|
|
*/ |
|
90
|
|
|
private static function isFloat($value) { |
|
91
|
|
|
if (false === is_float($value)) { |
|
92
|
|
|
throw new FloatArgumentException($value); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Determines if a value is an integer. |
|
98
|
|
|
* |
|
99
|
|
|
* @param mixed $value The value. |
|
100
|
|
|
* @throws IntegerArgumentException Throws a Integer argument exception if the value is not of expected type. |
|
101
|
|
|
*/ |
|
102
|
|
|
private static function isInteger($value) { |
|
103
|
|
|
if (false === is_integer($value)) { |
|
104
|
|
|
throw new IntegerArgumentException($value); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Determines if a value is a number. |
|
110
|
|
|
* |
|
111
|
|
|
* @param mixed $value The value. |
|
112
|
|
|
* @throws NumberArgumentException Throws a Number argument exception if the value is not of expected type. |
|
113
|
|
|
*/ |
|
114
|
|
|
private static function isNumber($value) { |
|
115
|
|
|
if (false === is_numeric($value)) { |
|
116
|
|
|
throw new NumberArgumentException($value); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Determines if a value is an object. |
|
122
|
|
|
* |
|
123
|
|
|
* @param mixed $value The value. |
|
124
|
|
|
* @throws ObjectArgumentException Throws an Object argument exception if the value is not of expected type. |
|
125
|
|
|
*/ |
|
126
|
|
|
private static function isObject($value) { |
|
127
|
|
|
if (false === is_object($value)) { |
|
128
|
|
|
throw new ObjectArgumentException($value); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Determines if a value is a resource. |
|
134
|
|
|
* |
|
135
|
|
|
* @param mixed $value The value. |
|
136
|
|
|
* @throws ResourceArgumentException Throws a Resource argument exception if the value is not of expected type. |
|
137
|
|
|
*/ |
|
138
|
|
|
private static function isResource($value) { |
|
139
|
|
|
if (false === is_resource($value)) { |
|
140
|
|
|
throw new ResourceArgumentException($value); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Determines if a value is a string. |
|
146
|
|
|
* |
|
147
|
|
|
* @param mixed $value The value. |
|
148
|
|
|
* @throws StringArgumentException Throws a String argument exception if the value is not of expected type. |
|
149
|
|
|
*/ |
|
150
|
|
|
private static function isString($value) { |
|
151
|
|
|
if (false === is_string($value)) { |
|
152
|
|
|
throw new StringArgumentException($value); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Determines if a value is a timestamp. |
|
158
|
|
|
* |
|
159
|
|
|
* @param mixed $value The value. |
|
160
|
|
|
* @throws TimestampArgumentException Throws a Timestamp argument exception if the value is not of expected type. |
|
161
|
|
|
*/ |
|
162
|
|
|
private static function isTimestamp($value) { |
|
163
|
|
|
if (false === strtotime($value)) { |
|
164
|
|
|
throw new TimestampArgumentException($value); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Determines if the $value is of type $type. |
|
170
|
|
|
* |
|
171
|
|
|
* @param mixed $value The value. |
|
172
|
|
|
* @param integer $type The type. |
|
173
|
|
|
* @throws IllegalArgumentException Throws an illegal argument exception if the value is not of expected type. |
|
174
|
|
|
*/ |
|
175
|
|
|
public static function isTypeOf($value, $type) { |
|
176
|
|
|
switch ($type) { |
|
177
|
|
|
case self::TYPE_ARRAY: |
|
178
|
|
|
self::isArray($value); |
|
179
|
|
|
break; |
|
180
|
|
|
case self::TYPE_BOOLEAN: |
|
181
|
|
|
self::isBoolean($value); |
|
182
|
|
|
break; |
|
183
|
|
|
case self::TYPE_DATE: |
|
184
|
|
|
self::isDate($value); |
|
185
|
|
|
break; |
|
186
|
|
|
case self::TYPE_DOUBLE: |
|
187
|
|
|
self::isDouble($value); |
|
188
|
|
|
break; |
|
189
|
|
|
case self::TYPE_FLOAT: |
|
190
|
|
|
self::isFloat($value); |
|
191
|
|
|
break; |
|
192
|
|
|
case self::TYPE_INTEGER: |
|
193
|
|
|
self::isInteger($value); |
|
194
|
|
|
break; |
|
195
|
|
|
case self::TYPE_NUMBER: |
|
196
|
|
|
self::isNumber($value); |
|
197
|
|
|
break; |
|
198
|
|
|
case self::TYPE_OBJECT: |
|
199
|
|
|
self::isObject($value); |
|
200
|
|
|
break; |
|
201
|
|
|
case self::TYPE_RESOURCE: |
|
202
|
|
|
self::isResource($value); |
|
203
|
|
|
break; |
|
204
|
|
|
case self::TYPE_STRING: |
|
205
|
|
|
self::isString($value); |
|
206
|
|
|
break; |
|
207
|
|
|
case self::TYPE_TIMESTAMP: |
|
208
|
|
|
self::isTimestamp($value); |
|
209
|
|
|
break; |
|
210
|
|
|
default: |
|
211
|
|
|
throw new IllegalArgumentException("The type \"" . $type . "\" is not implemented"); |
|
212
|
|
|
} |
|
213
|
|
|
return true; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
|