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