|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/ko-ko-ko/php-assert |
|
4
|
|
|
* @copyright Copyright (c) 2015 Roman Levishchenko <[email protected]> |
|
5
|
|
|
* @license https://raw.github.com/ko-ko-ko/php-assert/master/LICENSE |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace KoKoKo\assert; |
|
9
|
|
|
|
|
10
|
|
|
use KoKoKo\assert\exceptions\ArrayKeyNotExistsException; |
|
11
|
|
|
use KoKoKo\assert\exceptions\InvalidArrayCountException; |
|
12
|
|
|
use KoKoKo\assert\exceptions\InvalidArrayException; |
|
13
|
|
|
use KoKoKo\assert\exceptions\InvalidBoolException; |
|
14
|
|
|
use KoKoKo\assert\exceptions\InvalidDigitException; |
|
15
|
|
|
use KoKoKo\assert\exceptions\InvalidEmptyException; |
|
16
|
|
|
use KoKoKo\assert\exceptions\InvalidFloatException; |
|
17
|
|
|
use KoKoKo\assert\exceptions\InvalidIntException; |
|
18
|
|
|
use KoKoKo\assert\exceptions\InvalidIntOrFloatException; |
|
19
|
|
|
use KoKoKo\assert\exceptions\InvalidIntOrFloatOrStringException; |
|
20
|
|
|
use KoKoKo\assert\exceptions\InvalidIntOrStringException; |
|
21
|
|
|
use KoKoKo\assert\exceptions\InvalidNotArrayException; |
|
22
|
|
|
use KoKoKo\assert\exceptions\InvalidNotEmptyException; |
|
23
|
|
|
use KoKoKo\assert\exceptions\InvalidNotNullException; |
|
24
|
|
|
use KoKoKo\assert\exceptions\InvalidNotObjectException; |
|
25
|
|
|
use KoKoKo\assert\exceptions\InvalidNotSameValueException; |
|
26
|
|
|
use KoKoKo\assert\exceptions\InvalidNullException; |
|
27
|
|
|
use KoKoKo\assert\exceptions\InvalidNumericException; |
|
28
|
|
|
use KoKoKo\assert\exceptions\InvalidRegExpPatternException; |
|
29
|
|
|
use KoKoKo\assert\exceptions\InvalidResourceException; |
|
30
|
|
|
use KoKoKo\assert\exceptions\InvalidSameValueException; |
|
31
|
|
|
use KoKoKo\assert\exceptions\InvalidStringException; |
|
32
|
|
|
use KoKoKo\assert\exceptions\InvalidStringLengthException; |
|
33
|
|
|
use KoKoKo\assert\exceptions\LengthNotBetweenException; |
|
34
|
|
|
use KoKoKo\assert\exceptions\LengthNotGreaterException; |
|
35
|
|
|
use KoKoKo\assert\exceptions\LengthNotLessException; |
|
36
|
|
|
use KoKoKo\assert\exceptions\NumberNotBetweenException; |
|
37
|
|
|
use KoKoKo\assert\exceptions\NumberNotBetweenStrictlyException; |
|
38
|
|
|
use KoKoKo\assert\exceptions\NumberNotGreaterException; |
|
39
|
|
|
use KoKoKo\assert\exceptions\NumberNotGreaterStrictlyException; |
|
40
|
|
|
use KoKoKo\assert\exceptions\NumberNotLessException; |
|
41
|
|
|
use KoKoKo\assert\exceptions\NumberNotLessStrictlyException; |
|
42
|
|
|
use KoKoKo\assert\exceptions\NumberNotNegativeException; |
|
43
|
|
|
use KoKoKo\assert\exceptions\NumberNotPositiveException; |
|
44
|
|
|
use KoKoKo\assert\exceptions\StringNotMatchGlobException; |
|
45
|
|
|
use KoKoKo\assert\exceptions\StringNotMatchRegExpException; |
|
46
|
|
|
use KoKoKo\assert\exceptions\ValueNotInArrayException; |
|
47
|
|
|
|
|
48
|
|
|
class Assert |
|
49
|
|
|
{ |
|
50
|
|
|
/** @var Assert */ |
|
51
|
|
|
protected static $validator; |
|
52
|
|
|
|
|
53
|
|
|
/** @var string */ |
|
54
|
|
|
protected $name; |
|
55
|
|
|
|
|
56
|
|
|
/** @var int|float|bool|string|resource|array|null */ |
|
57
|
|
|
protected $value; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Creates validator instance for variable, first fail check will throw an exception |
|
61
|
|
|
* |
|
62
|
|
|
* @param int|float|string|resource|array|null $value |
|
63
|
|
|
* @param string $name |
|
64
|
|
|
* @return static |
|
65
|
|
|
* @throws InvalidNotObjectException |
|
66
|
|
|
* @throws InvalidStringException |
|
67
|
|
|
*/ |
|
68
|
133 |
|
public static function assert($value, $name = 'value') |
|
69
|
|
|
{ |
|
70
|
133 |
|
if (is_object($value)) { |
|
71
|
1 |
|
throw new InvalidNotObjectException($name); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
132 |
|
if (!is_string($name)) { |
|
75
|
1 |
|
throw new InvalidStringException('name', $name); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
131 |
|
if (empty(self::$validator)) { |
|
79
|
1 |
|
self::$validator = new static; |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
131 |
|
self::$validator->name = $name; |
|
83
|
131 |
|
self::$validator->value = $value; |
|
84
|
|
|
|
|
85
|
131 |
|
return self::$validator; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Return current validation value |
|
90
|
|
|
* |
|
91
|
|
|
* @return int|float|bool|string|resource|array |
|
92
|
|
|
*/ |
|
93
|
5 |
|
public function get() |
|
94
|
|
|
{ |
|
95
|
5 |
|
return $this->value; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param \Closure $callback (Assert $value) |
|
100
|
|
|
* @return $this |
|
101
|
|
|
* @throws InvalidArrayException |
|
102
|
|
|
* @throws InvalidIntException |
|
103
|
|
|
*/ |
|
104
|
4 |
|
public function forList(\Closure $callback) |
|
105
|
|
|
{ |
|
106
|
4 |
|
if (!is_array($this->value)) { |
|
107
|
1 |
|
throw new InvalidArrayException($this->name, $this->value); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
3 |
|
if (empty($this->value)) { |
|
111
|
1 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
$valueAssert = clone self::$validator; |
|
115
|
|
|
|
|
116
|
2 |
|
foreach ($this->value as $key => $value) { |
|
117
|
2 |
|
$valueAssert->value = $value; |
|
118
|
2 |
|
$valueAssert->name = sprintf("%s[%s]", $this->name, $key); |
|
119
|
|
|
|
|
120
|
2 |
|
$callback($valueAssert); |
|
121
|
1 |
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param \Closure $callback (Assert $key, Assert $value) |
|
128
|
|
|
* @return $this |
|
129
|
|
|
* @throws InvalidArrayException |
|
130
|
|
|
*/ |
|
131
|
5 |
|
public function forMap(\Closure $callback) |
|
132
|
|
|
{ |
|
133
|
5 |
|
if (!is_array($this->value)) { |
|
134
|
1 |
|
throw new InvalidArrayException($this->name, $this->value); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
4 |
|
if (empty($this->value)) { |
|
138
|
1 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
3 |
|
$keyAssert = clone self::$validator; |
|
142
|
3 |
|
$valueAssert = clone self::$validator; |
|
143
|
|
|
|
|
144
|
3 |
|
foreach ($this->value as $key => $value) { |
|
145
|
3 |
|
$keyAssert->value = $key; |
|
146
|
3 |
|
$valueAssert->value = $value; |
|
147
|
|
|
|
|
148
|
3 |
|
$keyAssert->name = sprintf("%s: key '%s'", $this->name, $key); |
|
149
|
3 |
|
$valueAssert->name = sprintf("%s['%s']", $this->name, $key); |
|
150
|
|
|
|
|
151
|
3 |
|
$callback($keyAssert, $valueAssert); |
|
152
|
1 |
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param int $length |
|
159
|
|
|
* @return $this |
|
160
|
|
|
* @throws InvalidIntException |
|
161
|
|
|
* @throws InvalidStringLengthException |
|
162
|
|
|
* @throws NumberNotPositiveException |
|
163
|
|
|
* @throws InvalidStringException |
|
164
|
|
|
*/ |
|
165
|
5 |
|
public function length($length) |
|
166
|
|
|
{ |
|
167
|
5 |
|
if (!is_int($length)) { |
|
168
|
1 |
|
throw new InvalidIntException('length', $length); |
|
169
|
4 |
|
} elseif ($length < 0) { |
|
170
|
1 |
|
throw new NumberNotPositiveException('length', $length); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
3 |
|
if (!is_string($this->value)) { |
|
174
|
1 |
|
throw new InvalidStringException($this->name, $this->value); |
|
175
|
2 |
|
} elseif (strlen($this->value) !== $length) { |
|
176
|
1 |
|
throw new InvalidStringLengthException($this->name, $this->value, $length); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Soft check if value has length $from <= $length <= to. Runs only after string validation |
|
184
|
|
|
* |
|
185
|
|
|
* @param int $from |
|
186
|
|
|
* @param int $to |
|
187
|
|
|
* @return $this |
|
188
|
|
|
* @throws InvalidIntException |
|
189
|
|
|
* @throws LengthNotBetweenException |
|
190
|
|
|
* @throws NumberNotPositiveException |
|
191
|
|
|
* @throws NumberNotGreaterException |
|
192
|
|
|
* @throws NumberNotLessException |
|
193
|
|
|
* @throws InvalidStringException |
|
194
|
|
|
*/ |
|
195
|
7 |
|
public function lengthBetween($from, $to) |
|
196
|
|
|
{ |
|
197
|
7 |
|
if (!is_int($from)) { |
|
198
|
1 |
|
throw new InvalidIntException('from', $from); |
|
199
|
6 |
|
} elseif (!is_int($to)) { |
|
200
|
1 |
|
throw new InvalidIntException('to', $to); |
|
201
|
5 |
|
} elseif ($from > $to) { |
|
202
|
1 |
|
throw new NumberNotLessException('from', $from, $to); |
|
203
|
4 |
|
} elseif ($from < 0) { |
|
204
|
1 |
|
throw new NumberNotGreaterException('from', $from, 0); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
3 |
|
if (!is_string($this->value)) { |
|
208
|
1 |
|
throw new InvalidStringException($this->name, $this->value); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
2 |
|
$length = strlen($this->value); |
|
212
|
|
|
|
|
213
|
2 |
|
if ($length < $from || $length > $to) { |
|
214
|
1 |
|
throw new LengthNotBetweenException($this->name, $this->value, $from, $to); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
1 |
|
return $this; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Soft check if value has length less than $length. Runs only after string validation |
|
222
|
|
|
* |
|
223
|
|
|
* @param int $length |
|
224
|
|
|
* @return $this |
|
225
|
|
|
* @throws InvalidIntException |
|
226
|
|
|
* @throws LengthNotLessException |
|
227
|
|
|
* @throws NumberNotPositiveException |
|
228
|
|
|
* @throws InvalidStringException |
|
229
|
|
|
*/ |
|
230
|
5 |
|
public function lengthLess($length) |
|
231
|
|
|
{ |
|
232
|
5 |
|
if (!is_int($length)) { |
|
233
|
1 |
|
throw new InvalidIntException('length', $length); |
|
234
|
4 |
|
} elseif ($length < 0) { |
|
235
|
1 |
|
throw new NumberNotPositiveException('length', $length); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
3 |
|
if (!is_string($this->value)) { |
|
239
|
1 |
|
throw new InvalidStringException($this->name, $this->value); |
|
240
|
2 |
|
} elseif (strlen($this->value) > $length) { |
|
241
|
1 |
|
throw new LengthNotLessException($this->name, $this->value, $length); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
1 |
|
return $this; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Soft check if value has length less than $length. Runs only after notEmpty and string validations |
|
249
|
|
|
* |
|
250
|
|
|
* @param int $length |
|
251
|
|
|
* @return $this |
|
252
|
|
|
* @throws InvalidIntException |
|
253
|
|
|
* @throws LengthNotGreaterException |
|
254
|
|
|
* @throws NumberNotPositiveException |
|
255
|
|
|
* @throws InvalidStringException |
|
256
|
|
|
*/ |
|
257
|
5 |
|
public function lengthGreater($length) |
|
258
|
|
|
{ |
|
259
|
5 |
|
if (!is_int($length)) { |
|
260
|
1 |
|
throw new InvalidIntException('length', $length); |
|
261
|
4 |
|
} elseif ($length < 0) { |
|
262
|
1 |
|
throw new NumberNotPositiveException('length', $length); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
3 |
|
if (!is_string($this->value)) { |
|
266
|
1 |
|
throw new InvalidStringException($this->name, $this->value); |
|
267
|
2 |
|
} elseif (strlen($this->value) < $length) { |
|
268
|
1 |
|
throw new LengthNotGreaterException($this->name, $this->value, $length); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
1 |
|
return $this; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Check if value is in array (in_array strict) |
|
276
|
|
|
* |
|
277
|
|
|
* @param array $range |
|
278
|
|
|
* @return $this |
|
279
|
|
|
* @throws InvalidArrayException |
|
280
|
|
|
* @throws InvalidNotEmptyException |
|
281
|
|
|
* @throws ValueNotInArrayException |
|
282
|
|
|
*/ |
|
283
|
4 |
|
public function inArray($range) |
|
284
|
|
|
{ |
|
285
|
4 |
|
if (!is_array($range)) { |
|
286
|
1 |
|
throw new InvalidArrayException('range', $range); |
|
287
|
3 |
|
} elseif (empty($range)) { |
|
288
|
1 |
|
throw new InvalidNotEmptyException('range'); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
2 |
|
if (!in_array($this->value, $range, true)) { |
|
292
|
1 |
|
throw new ValueNotInArrayException($this->name, $this->value, $range); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
1 |
|
return $this; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Check if value is array |
|
300
|
|
|
* |
|
301
|
|
|
* @return $this |
|
302
|
|
|
* @throws InvalidArrayException |
|
303
|
|
|
*/ |
|
304
|
2 |
|
public function isArray() |
|
305
|
|
|
{ |
|
306
|
2 |
|
if (!is_array($this->value)) { |
|
307
|
1 |
|
throw new InvalidArrayException($this->name, $this->value); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
1 |
|
return $this; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Check if array key exists |
|
315
|
|
|
* |
|
316
|
|
|
* @param string|int $key |
|
317
|
|
|
* @return $this |
|
318
|
|
|
* @throws ArrayKeyNotExistsException |
|
319
|
|
|
* @throws InvalidArrayException |
|
320
|
|
|
* @throws InvalidIntOrStringException |
|
321
|
|
|
*/ |
|
322
|
4 |
|
public function hasKey($key) |
|
323
|
|
|
{ |
|
324
|
4 |
|
if (!is_string($key) && !is_int($key)) { |
|
325
|
1 |
|
throw new InvalidIntOrStringException('key', $key); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
3 |
|
if (!is_array($this->value)) { |
|
329
|
1 |
|
throw new InvalidArrayException($this->name, $this->value); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
2 |
|
if (!array_key_exists($key, $this->value)) { |
|
333
|
1 |
|
throw new ArrayKeyNotExistsException($this->name, $key); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
1 |
|
return $this; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* Check if array elements count is same as $count |
|
341
|
|
|
* |
|
342
|
|
|
* @param int $count |
|
343
|
|
|
* @return $this |
|
344
|
|
|
* @throws InvalidArrayCountException |
|
345
|
|
|
* @throws InvalidArrayException |
|
346
|
|
|
* @throws InvalidIntException |
|
347
|
|
|
* @throws NumberNotGreaterException |
|
348
|
|
|
*/ |
|
349
|
5 |
|
public function count($count) |
|
350
|
|
|
{ |
|
351
|
5 |
|
if (!is_int($count)) { |
|
352
|
1 |
|
throw new InvalidIntException('count', $count); |
|
353
|
4 |
|
} elseif ($count < 0) { |
|
354
|
1 |
|
throw new NumberNotGreaterException('count', $count, 0); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
3 |
|
if (!is_array($this->value)) { |
|
358
|
1 |
|
throw new InvalidArrayException($this->name, $this->value); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
2 |
|
if (count($this->value) !== $count) { |
|
362
|
1 |
|
throw new InvalidArrayCountException($this->name, $this->value, $count); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
1 |
|
return $this; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Soft check that $from <= value <= $to |
|
370
|
|
|
* |
|
371
|
|
|
* @param float|int $from |
|
372
|
|
|
* @param float|int $to |
|
373
|
|
|
* @return $this |
|
374
|
|
|
* @throws NumberNotBetweenException |
|
375
|
|
|
* @throws InvalidIntOrFloatException |
|
376
|
|
|
* @throws NumberNotLessStrictlyException |
|
377
|
|
|
*/ |
|
378
|
6 |
|
public function between($from, $to) |
|
379
|
|
|
{ |
|
380
|
6 |
|
if (!is_int($from) && !is_float($from)) { |
|
381
|
1 |
|
throw new InvalidIntOrFloatException('from', $from); |
|
382
|
5 |
|
} elseif (!is_int($to) && !is_float($to)) { |
|
383
|
1 |
|
throw new InvalidIntOrFloatException('to', $to); |
|
384
|
4 |
|
} elseif ($from > $to) { |
|
385
|
1 |
|
throw new NumberNotLessStrictlyException('from', $from, $to); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
3 |
|
if (!is_int($this->value) && !is_float($this->value)) { |
|
389
|
1 |
|
throw new InvalidIntOrFloatException($this->name, $this->value); |
|
390
|
2 |
|
} elseif ($this->value < $from || $this->value > $to) { |
|
391
|
1 |
|
throw new NumberNotBetweenException($this->name, $this->value, $from, $to); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
1 |
|
return $this; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* Strict check that $from < value < $to |
|
399
|
|
|
* |
|
400
|
|
|
* @param float|int $from |
|
401
|
|
|
* @param float|int $to |
|
402
|
|
|
* @return $this |
|
403
|
|
|
* @throws InvalidIntOrFloatException |
|
404
|
|
|
* @throws NumberNotBetweenStrictlyException |
|
405
|
|
|
* @throws NumberNotLessStrictlyException |
|
406
|
|
|
*/ |
|
407
|
6 |
|
public function betweenStrict($from, $to) |
|
408
|
|
|
{ |
|
409
|
6 |
|
if (!is_int($from) && !is_float($from)) { |
|
410
|
1 |
|
throw new InvalidIntOrFloatException('from', $from); |
|
411
|
5 |
|
} elseif (!is_int($to) && !is_float($to)) { |
|
412
|
1 |
|
throw new InvalidIntOrFloatException('to', $to); |
|
413
|
4 |
|
} elseif ($from > $to) { |
|
414
|
1 |
|
throw new NumberNotLessStrictlyException('from', $from, $to); |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
3 |
|
if (!is_int($this->value) && !is_float($this->value)) { |
|
418
|
1 |
|
throw new InvalidIntOrFloatException($this->name, $this->value); |
|
419
|
2 |
|
} elseif ($this->value <= $from || $this->value >= $to) { |
|
420
|
1 |
|
throw new NumberNotBetweenStrictlyException($this->name, $this->value, $from, $to); |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
1 |
|
return $this; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
/** |
|
427
|
|
|
* Check if value is boolean (is_bool) |
|
428
|
|
|
* |
|
429
|
|
|
* @return $this |
|
430
|
|
|
* @throws InvalidBoolException |
|
431
|
|
|
*/ |
|
432
|
2 |
|
public function bool() |
|
433
|
|
|
{ |
|
434
|
2 |
|
if (!is_bool($this->value)) { |
|
435
|
1 |
|
throw new InvalidBoolException($this->name, $this->value); |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
1 |
|
return $this; |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* Check if value is digit (ctype_digit) |
|
443
|
|
|
* |
|
444
|
|
|
* @return $this |
|
445
|
|
|
* @throws InvalidDigitException |
|
446
|
|
|
* @throws InvalidStringException |
|
447
|
|
|
*/ |
|
448
|
3 |
|
public function digit() |
|
449
|
|
|
{ |
|
450
|
3 |
|
if (!is_string($this->value)) { |
|
451
|
1 |
|
throw new InvalidStringException($this->name, $this->value); |
|
452
|
2 |
|
} elseif (!ctype_digit($this->value)) { |
|
453
|
1 |
|
throw new InvalidDigitException($this->name, $this->value); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
1 |
|
return $this; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
/** |
|
460
|
|
|
* Check if value is empty (empty) |
|
461
|
|
|
* |
|
462
|
|
|
* @return $this |
|
463
|
|
|
* @throws InvalidEmptyException |
|
464
|
|
|
*/ |
|
465
|
2 |
|
public function isEmpty() |
|
466
|
|
|
{ |
|
467
|
2 |
|
if (!empty($this->value)) { |
|
468
|
1 |
|
throw new InvalidEmptyException($this->name); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
1 |
|
return $this; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
/** |
|
475
|
|
|
* Check if value is not empty (empty) |
|
476
|
|
|
* |
|
477
|
|
|
* @return $this |
|
478
|
|
|
* @throws InvalidNotEmptyException |
|
479
|
|
|
*/ |
|
480
|
2 |
|
public function notEmpty() |
|
481
|
|
|
{ |
|
482
|
2 |
|
if (empty($this->value)) { |
|
483
|
1 |
|
throw new InvalidNotEmptyException($this->name); |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
1 |
|
return $this; |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* Check if value is float (is_float) |
|
491
|
|
|
* |
|
492
|
|
|
* @return $this |
|
493
|
|
|
* @throws InvalidFloatException |
|
494
|
|
|
*/ |
|
495
|
2 |
|
public function float() |
|
496
|
|
|
{ |
|
497
|
2 |
|
if (!is_float($this->value)) { |
|
498
|
1 |
|
throw new InvalidFloatException($this->name, $this->value); |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
1 |
|
return $this; |
|
502
|
|
|
} |
|
503
|
|
|
|
|
504
|
|
|
/** |
|
505
|
|
|
* Check if value is integer (is_int) |
|
506
|
|
|
* |
|
507
|
|
|
* @return $this |
|
508
|
|
|
* @throws InvalidIntException |
|
509
|
|
|
*/ |
|
510
|
7 |
|
public function int() |
|
511
|
|
|
{ |
|
512
|
7 |
|
if (!is_int($this->value)) { |
|
513
|
4 |
|
throw new InvalidIntException($this->name, $this->value); |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
3 |
|
return $this; |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
/** |
|
520
|
|
|
* Soft check that value <= $max |
|
521
|
|
|
* |
|
522
|
|
|
* @param float|int $number |
|
523
|
|
|
* @return $this |
|
524
|
|
|
* @throws InvalidIntOrFloatException |
|
525
|
|
|
* @throws NumberNotLessException |
|
526
|
|
|
*/ |
|
527
|
4 |
|
public function less($number) |
|
528
|
|
|
{ |
|
529
|
4 |
|
if (!is_int($number) && !is_float($number)) { |
|
530
|
1 |
|
throw new InvalidIntOrFloatException('number', $number); |
|
531
|
|
|
} |
|
532
|
|
|
|
|
533
|
3 |
|
if (!is_int($this->value) && !is_float($this->value)) { |
|
534
|
1 |
|
throw new InvalidIntOrFloatException($this->name, $this->value); |
|
535
|
2 |
|
} elseif ($this->value > $number) { |
|
536
|
1 |
|
throw new NumberNotLessException($this->name, $this->value, $number); |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
1 |
|
return $this; |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
/** |
|
543
|
|
|
* Soft check that value >= $min |
|
544
|
|
|
* |
|
545
|
|
|
* @param float|int $number |
|
546
|
|
|
* @return $this |
|
547
|
|
|
* @throws NumberNotGreaterException |
|
548
|
|
|
* @throws InvalidIntOrFloatException |
|
549
|
|
|
*/ |
|
550
|
4 |
|
public function greater($number) |
|
551
|
|
|
{ |
|
552
|
4 |
|
if (!is_int($number) && !is_float($number)) { |
|
553
|
1 |
|
throw new InvalidIntOrFloatException('number', $number); |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
3 |
|
if (!is_int($this->value) && !is_float($this->value)) { |
|
557
|
1 |
|
throw new InvalidIntOrFloatException($this->name, $this->value); |
|
558
|
2 |
|
} elseif ($this->value < $number) { |
|
559
|
1 |
|
throw new NumberNotGreaterException($this->name, $this->value, $number); |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
1 |
|
return $this; |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
/** |
|
566
|
|
|
* Strict check that value < $max |
|
567
|
|
|
* |
|
568
|
|
|
* @param float|int $number |
|
569
|
|
|
* @return $this |
|
570
|
|
|
* @throws InvalidIntOrFloatException |
|
571
|
|
|
* @throws NumberNotLessStrictlyException |
|
572
|
|
|
*/ |
|
573
|
4 |
|
public function lessStrict($number) |
|
574
|
|
|
{ |
|
575
|
4 |
|
if (!is_int($number) && !is_float($number)) { |
|
576
|
1 |
|
throw new InvalidIntOrFloatException('number', $number); |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
3 |
|
if (!is_int($this->value) && !is_float($this->value)) { |
|
580
|
1 |
|
throw new InvalidIntOrFloatException($this->name, $this->value); |
|
581
|
2 |
|
} elseif ($this->value >= $number) { |
|
582
|
1 |
|
throw new NumberNotLessStrictlyException($this->name, $this->value, $number); |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
1 |
|
return $this; |
|
586
|
|
|
} |
|
587
|
|
|
|
|
588
|
|
|
/** |
|
589
|
|
|
* Strict check that value > $min |
|
590
|
|
|
* |
|
591
|
|
|
* @param float|int $number |
|
592
|
|
|
* @return $this |
|
593
|
|
|
* @throws InvalidIntOrFloatException |
|
594
|
|
|
* @throws NumberNotGreaterStrictlyException |
|
595
|
|
|
*/ |
|
596
|
4 |
|
public function greaterStrict($number) |
|
597
|
|
|
{ |
|
598
|
4 |
|
if (!is_int($number) && !is_float($number)) { |
|
599
|
1 |
|
throw new InvalidIntOrFloatException('number', $number); |
|
600
|
|
|
} |
|
601
|
|
|
|
|
602
|
3 |
|
if (!is_int($this->value) && !is_float($this->value)) { |
|
603
|
1 |
|
throw new InvalidIntOrFloatException($this->name, $this->value); |
|
604
|
2 |
|
} elseif ($this->value <= $number) { |
|
605
|
1 |
|
throw new NumberNotGreaterStrictlyException($this->name, $this->value, $number); |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
1 |
|
return $this; |
|
609
|
|
|
} |
|
610
|
|
|
|
|
611
|
|
|
/** |
|
612
|
|
|
* Check if value match regexp pattern |
|
613
|
|
|
* |
|
614
|
|
|
* @param string $pattern |
|
615
|
|
|
* @return $this |
|
616
|
|
|
* @throws InvalidNotEmptyException |
|
617
|
|
|
* @throws InvalidRegexpPatternException |
|
618
|
|
|
* @throws InvalidStringException |
|
619
|
|
|
* @throws StringNotMatchRegExpException |
|
620
|
|
|
*/ |
|
621
|
6 |
|
public function match($pattern) |
|
622
|
|
|
{ |
|
623
|
6 |
|
if (!is_string($pattern)) { |
|
624
|
1 |
|
throw new InvalidStringException('pattern', $pattern); |
|
625
|
5 |
|
} elseif (empty($pattern)) { |
|
626
|
1 |
|
throw new InvalidNotEmptyException('pattern'); |
|
627
|
|
|
} |
|
628
|
|
|
|
|
629
|
4 |
|
if (!is_string($this->value)) { |
|
630
|
1 |
|
throw new InvalidStringException($this->name, $this->value); |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
// God please sorry for this @ |
|
634
|
3 |
|
$checkResult = @preg_match($pattern, $this->value); |
|
635
|
|
|
|
|
636
|
3 |
|
if ((preg_last_error() !== PREG_NO_ERROR) || ($checkResult === false)) { |
|
637
|
1 |
|
throw new InvalidRegExpPatternException('pattern', $pattern); |
|
638
|
|
|
} |
|
639
|
|
|
|
|
640
|
2 |
|
if ($checkResult === 0) { |
|
641
|
1 |
|
throw new StringNotMatchRegExpException($this->name, $this->value, $pattern); |
|
642
|
|
|
} |
|
643
|
|
|
|
|
644
|
1 |
|
return $this; |
|
645
|
|
|
} |
|
646
|
|
|
|
|
647
|
|
|
/** |
|
648
|
|
|
* Check if value match glob pattern |
|
649
|
|
|
* |
|
650
|
|
|
* @param string $pattern |
|
651
|
|
|
* @return $this |
|
652
|
|
|
* @throws InvalidNotEmptyException |
|
653
|
|
|
* @throws InvalidStringException |
|
654
|
|
|
* @throws StringNotMatchGlobException |
|
655
|
|
|
*/ |
|
656
|
5 |
|
public function glob($pattern) |
|
657
|
|
|
{ |
|
658
|
5 |
|
if (!is_string($pattern)) { |
|
659
|
1 |
|
throw new InvalidStringException('pattern', $pattern); |
|
660
|
4 |
|
} elseif (empty($pattern)) { |
|
661
|
1 |
|
throw new InvalidNotEmptyException('pattern'); |
|
662
|
|
|
} |
|
663
|
|
|
|
|
664
|
3 |
|
if (!is_string($this->value)) { |
|
665
|
1 |
|
throw new InvalidStringException($this->name, $this->value); |
|
666
|
2 |
|
} elseif (!fnmatch($pattern, $this->value)) { |
|
667
|
1 |
|
throw new StringNotMatchGlobException($this->name, $this->value, $pattern); |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
1 |
|
return $this; |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
/** |
|
674
|
|
|
* Check if value < 0 |
|
675
|
|
|
* |
|
676
|
|
|
* @return $this |
|
677
|
|
|
* @throws InvalidIntOrFloatException |
|
678
|
|
|
* @throws NumberNotNegativeException |
|
679
|
|
|
*/ |
|
680
|
3 |
|
public function negative() |
|
681
|
|
|
{ |
|
682
|
3 |
|
if (!is_int($this->value) && !is_float($this->value)) { |
|
683
|
1 |
|
throw new InvalidIntOrFloatException($this->name, $this->value); |
|
684
|
2 |
|
} elseif ($this->value >= 0) { |
|
685
|
1 |
|
throw new NumberNotNegativeException($this->name, $this->value); |
|
686
|
|
|
} |
|
687
|
|
|
|
|
688
|
1 |
|
return $this; |
|
689
|
|
|
} |
|
690
|
|
|
|
|
691
|
|
|
/** |
|
692
|
|
|
* Check if value > 0 |
|
693
|
|
|
* |
|
694
|
|
|
* @return $this |
|
695
|
|
|
* @throws InvalidIntOrFloatException |
|
696
|
|
|
* @throws NumberNotPositiveException |
|
697
|
|
|
*/ |
|
698
|
3 |
|
public function positive() |
|
699
|
|
|
{ |
|
700
|
3 |
|
if (!is_int($this->value) && !is_float($this->value)) { |
|
701
|
1 |
|
throw new InvalidIntOrFloatException($this->name, $this->value); |
|
702
|
2 |
|
} elseif ($this->value <= 0) { |
|
703
|
1 |
|
throw new NumberNotPositiveException($this->name, $this->value); |
|
704
|
|
|
} |
|
705
|
|
|
|
|
706
|
1 |
|
return $this; |
|
707
|
|
|
} |
|
708
|
|
|
|
|
709
|
|
|
/** |
|
710
|
|
|
* Check if value is same as $anotherValue |
|
711
|
|
|
* |
|
712
|
|
|
* @param int|float|bool|string|resource|array|null $anotherValue |
|
713
|
|
|
* @return $this |
|
714
|
|
|
* @throws InvalidNotObjectException |
|
715
|
|
|
* @throws InvalidSameValueException |
|
716
|
|
|
*/ |
|
717
|
3 |
|
public function isSame($anotherValue) |
|
718
|
|
|
{ |
|
719
|
3 |
|
if (is_object($anotherValue)) { |
|
720
|
1 |
|
throw new InvalidNotObjectException('anotherValue'); |
|
721
|
|
|
} |
|
722
|
|
|
|
|
723
|
2 |
|
if ($this->value !== $anotherValue) { |
|
724
|
1 |
|
throw new InvalidSameValueException($this->name, $this->value, $anotherValue); |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
1 |
|
return $this; |
|
728
|
|
|
} |
|
729
|
|
|
|
|
730
|
|
|
/** |
|
731
|
|
|
* Check if value is not same as $anotherValue |
|
732
|
|
|
* |
|
733
|
|
|
* @param int|float|bool|string|resource|array|null $anotherValue |
|
734
|
|
|
* @return $this |
|
735
|
|
|
* @throws InvalidNotObjectException |
|
736
|
|
|
* @throws InvalidNotSameValueException |
|
737
|
|
|
*/ |
|
738
|
3 |
|
public function notSame($anotherValue) |
|
739
|
|
|
{ |
|
740
|
3 |
|
if (is_object($anotherValue)) { |
|
741
|
1 |
|
throw new InvalidNotObjectException('anotherValue'); |
|
742
|
|
|
} |
|
743
|
|
|
|
|
744
|
2 |
|
if ($this->value === $anotherValue) { |
|
745
|
1 |
|
throw new InvalidNotSameValueException($this->name, $this->value); |
|
746
|
|
|
} |
|
747
|
|
|
|
|
748
|
1 |
|
return $this; |
|
749
|
|
|
} |
|
750
|
|
|
|
|
751
|
|
|
/** |
|
752
|
|
|
* Check if value is null |
|
753
|
|
|
* |
|
754
|
|
|
* @return $this |
|
755
|
|
|
* @throws InvalidNullException |
|
756
|
|
|
*/ |
|
757
|
2 |
|
public function isNull() |
|
758
|
|
|
{ |
|
759
|
2 |
|
if (!is_null($this->value)) { |
|
760
|
1 |
|
throw new InvalidNullException($this->name, $this->value); |
|
761
|
|
|
} |
|
762
|
|
|
|
|
763
|
1 |
|
return $this; |
|
764
|
|
|
} |
|
765
|
|
|
|
|
766
|
|
|
/** |
|
767
|
|
|
* Check if value is not null |
|
768
|
|
|
* |
|
769
|
|
|
* @return $this |
|
770
|
|
|
* @throws InvalidNotNullException |
|
771
|
|
|
*/ |
|
772
|
2 |
|
public function notNull() |
|
773
|
|
|
{ |
|
774
|
2 |
|
if (is_null($this->value)) { |
|
775
|
1 |
|
throw new InvalidNotNullException($this->name); |
|
776
|
|
|
} |
|
777
|
|
|
|
|
778
|
1 |
|
return $this; |
|
779
|
|
|
} |
|
780
|
|
|
|
|
781
|
|
|
/** |
|
782
|
|
|
* Check if value is numeric (is_numeric) |
|
783
|
|
|
* |
|
784
|
|
|
* @return $this |
|
785
|
|
|
* @throws InvalidIntOrFloatOrStringException |
|
786
|
|
|
* @throws InvalidNumericException |
|
787
|
|
|
*/ |
|
788
|
3 |
|
public function numeric() |
|
789
|
|
|
{ |
|
790
|
3 |
|
if (!is_int($this->value) && !is_float($this->value) && !is_string($this->value)) { |
|
791
|
1 |
|
throw new InvalidIntOrFloatOrStringException($this->name, $this->value); |
|
792
|
2 |
|
} elseif (!is_numeric($this->value)) { |
|
793
|
1 |
|
throw new InvalidNumericException($this->name, $this->value); |
|
794
|
|
|
} |
|
795
|
|
|
|
|
796
|
1 |
|
return $this; |
|
797
|
|
|
} |
|
798
|
|
|
|
|
799
|
|
|
/** |
|
800
|
|
|
* Check if value is resource (is_resource) |
|
801
|
|
|
* |
|
802
|
|
|
* @return $this |
|
803
|
|
|
* @throws InvalidResourceException |
|
804
|
|
|
*/ |
|
805
|
2 |
|
public function resource() |
|
806
|
|
|
{ |
|
807
|
2 |
|
if (!is_resource($this->value)) { |
|
808
|
1 |
|
throw new InvalidResourceException($this->name, $this->value); |
|
809
|
|
|
} |
|
810
|
|
|
|
|
811
|
1 |
|
return $this; |
|
812
|
|
|
} |
|
813
|
|
|
|
|
814
|
|
|
/** |
|
815
|
|
|
* Check if value is string (is_string) |
|
816
|
|
|
* |
|
817
|
|
|
* @return $this |
|
818
|
|
|
* @throws InvalidStringException |
|
819
|
|
|
*/ |
|
820
|
3 |
|
public function string() |
|
821
|
|
|
{ |
|
822
|
3 |
|
if (!is_string($this->value)) { |
|
823
|
1 |
|
throw new InvalidStringException($this->name, $this->value); |
|
824
|
|
|
} |
|
825
|
|
|
|
|
826
|
2 |
|
return $this; |
|
827
|
|
|
} |
|
828
|
|
|
|
|
829
|
|
|
/** |
|
830
|
|
|
* Cast value to bool |
|
831
|
|
|
* |
|
832
|
|
|
* @return $this |
|
833
|
|
|
*/ |
|
834
|
1 |
|
public function toBool() |
|
835
|
|
|
{ |
|
836
|
1 |
|
$this->value = (bool) $this->value; |
|
837
|
|
|
|
|
838
|
1 |
|
return $this; |
|
839
|
|
|
} |
|
840
|
|
|
|
|
841
|
|
|
/** |
|
842
|
|
|
* Cast value to float. If it's not numeric - there will be fail cast |
|
843
|
|
|
* |
|
844
|
|
|
* @return $this |
|
845
|
|
|
* @throws InvalidNotArrayException |
|
846
|
|
|
* @throws InvalidNumericException |
|
847
|
|
|
*/ |
|
848
|
3 |
|
public function toFloat() |
|
849
|
|
|
{ |
|
850
|
3 |
|
if (is_array($this->value)) { |
|
851
|
1 |
|
throw new InvalidNotArrayException($this->name); |
|
852
|
2 |
|
} elseif (!empty($this->value) && !is_numeric($this->value)) { |
|
853
|
1 |
|
throw new InvalidNumericException($this->name, $this->value); |
|
854
|
|
|
} |
|
855
|
|
|
|
|
856
|
1 |
|
$this->value = (float) $this->value; |
|
857
|
|
|
|
|
858
|
1 |
|
return $this; |
|
859
|
|
|
} |
|
860
|
|
|
|
|
861
|
|
|
/** |
|
862
|
|
|
* Cast value to int. If it's not numeric - there will be fail cast |
|
863
|
|
|
* |
|
864
|
|
|
* @return $this |
|
865
|
|
|
* @throws InvalidNotArrayException |
|
866
|
|
|
* @throws InvalidNumericException |
|
867
|
|
|
*/ |
|
868
|
3 |
|
public function toInt() |
|
869
|
|
|
{ |
|
870
|
3 |
|
if (is_array($this->value)) { |
|
871
|
1 |
|
throw new InvalidNotArrayException($this->name); |
|
872
|
2 |
|
} elseif (!empty($this->value) && !is_numeric($this->value)) { |
|
873
|
1 |
|
throw new InvalidNumericException($this->name, $this->value); |
|
874
|
|
|
} |
|
875
|
|
|
|
|
876
|
1 |
|
$this->value = (int) $this->value; |
|
877
|
|
|
|
|
878
|
1 |
|
return $this; |
|
879
|
|
|
} |
|
880
|
|
|
|
|
881
|
|
|
/** |
|
882
|
|
|
* Cast value to string. If it's array - there will be fail cast |
|
883
|
|
|
* |
|
884
|
|
|
* @return $this |
|
885
|
|
|
* @throws InvalidNotArrayException |
|
886
|
|
|
*/ |
|
887
|
2 |
|
public function toString() |
|
888
|
|
|
{ |
|
889
|
2 |
|
if (is_array($this->value)) { |
|
890
|
1 |
|
throw new InvalidNotArrayException($this->name); |
|
891
|
|
|
} |
|
892
|
|
|
|
|
893
|
1 |
|
$this->value = (string) $this->value; |
|
894
|
|
|
|
|
895
|
1 |
|
return $this; |
|
896
|
|
|
} |
|
897
|
|
|
} |
|
898
|
|
|
|