Failed Conditions
Pull Request — master (#35)
by Tobias
08:12 queued 06:44
created

Assert::natural()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 2
nop 2
crap 4
1
<?php
2
3
/*
4
 * This file is part of the webmozart/assert package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
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 Webmozart\Assert;
13
14
use BadMethodCallException;
15
use Closure;
16
use Countable;
17
use Exception;
18
use InvalidArgumentException;
19
use Throwable;
20
use Traversable;
21
22
/**
23
 * Efficient assertions to validate the input/output of your methods.
24
 *
25
 * @method static void nullOrString($value, $message = '')
26
 * @method static void nullOrStringNotEmpty($value, $message = '')
27
 * @method static void nullOrInteger($value, $message = '')
28
 * @method static void nullOrIntegerish($value, $message = '')
29
 * @method static void nullOrFloat($value, $message = '')
30
 * @method static void nullOrNumeric($value, $message = '')
31
 * @method static void nullOrBoolean($value, $message = '')
32
 * @method static void nullOrScalar($value, $message = '')
33
 * @method static void nullOrObject($value, $message = '')
34
 * @method static void nullOrResource($value, $type = null, $message = '')
35
 * @method static void nullOrIsCallable($value, $message = '')
36
 * @method static void nullOrIsArray($value, $message = '')
37
 * @method static void nullOrIsTraversable($value, $message = '')
38
 * @method static void nullOrIsCountable($value, $message = '')
39
 * @method static void nullOrIsInstanceOf($value, $class, $message = '')
40
 * @method static void nullOrNotInstanceOf($value, $class, $message = '')
41
 * @method static void nullOrIsEmpty($value, $message = '')
42
 * @method static void nullOrNotEmpty($value, $message = '')
43
 * @method static void nullOrTrue($value, $message = '')
44
 * @method static void nullOrFalse($value, $message = '')
45
 * @method static void nullOrEq($value, $value2, $message = '')
46
 * @method static void nullOrNotEq($value,$value2,  $message = '')
47
 * @method static void nullOrSame($value, $value2, $message = '')
48
 * @method static void nullOrNotSame($value, $value2, $message = '')
49
 * @method static void nullOrGreaterThan($value, $value2, $message = '')
50
 * @method static void nullOrGreaterThanEq($value, $value2, $message = '')
51
 * @method static void nullOrLessThan($value, $value2, $message = '')
52
 * @method static void nullOrLessThanEq($value, $value2, $message = '')
53
 * @method static void nullOrRange($value, $min, $max, $message = '')
54
 * @method static void nullOrOneOf($value, $values, $message = '')
55
 * @method static void nullOrContains($value, $subString, $message = '')
56
 * @method static void nullOrNotWhitespaceOnly($value, $message = '')
57
 * @method static void nullOrStartsWith($value, $prefix, $message = '')
58
 * @method static void nullOrStartsWithLetter($value, $message = '')
59
 * @method static void nullOrEndsWith($value, $suffix, $message = '')
60
 * @method static void nullOrRegex($value, $pattern, $message = '')
61
 * @method static void nullOrAlpha($value, $message = '')
62
 * @method static void nullOrDigits($value, $message = '')
63
 * @method static void nullOrAlnum($value, $message = '')
64
 * @method static void nullOrLower($value, $message = '')
65
 * @method static void nullOrUpper($value, $message = '')
66
 * @method static void nullOrLength($value, $length, $message = '')
67
 * @method static void nullOrMinLength($value, $min, $message = '')
68
 * @method static void nullOrMaxLength($value, $max, $message = '')
69
 * @method static void nullOrLengthBetween($value, $min, $max, $message = '')
70
 * @method static void nullOrFileExists($value, $message = '')
71
 * @method static void nullOrFile($value, $message = '')
72
 * @method static void nullOrDirectory($value, $message = '')
73
 * @method static void nullOrReadable($value, $message = '')
74
 * @method static void nullOrWritable($value, $message = '')
75
 * @method static void nullOrClassExists($value, $message = '')
76
 * @method static void nullOrSubclassOf($value, $class, $message = '')
77
 * @method static void nullOrImplementsInterface($value, $interface, $message = '')
78
 * @method static void nullOrPropertyExists($value, $property, $message = '')
79
 * @method static void nullOrPropertyNotExists($value, $property, $message = '')
80
 * @method static void nullOrMethodExists($value, $method, $message = '')
81
 * @method static void nullOrMethodNotExists($value, $method, $message = '')
82
 * @method static void nullOrKeyExists($value, $key, $message = '')
83
 * @method static void nullOrKeyNotExists($value, $key, $message = '')
84
 * @method static void nullOrCount($value, $key, $message = '')
85
 * @method static void nullOrMinCount($value, $min, $message = '')
86
 * @method static void nullOrMaxCount($value, $max, $message = '')
87
 * @method static void nullCountBetween($value, $min, $max, $message = '')
88
 * @method static void nullOrUuid($values, $message = '')
89
 * @method static void allString($values, $message = '')
90
 * @method static void allStringNotEmpty($values, $message = '')
91
 * @method static void allInteger($values, $message = '')
92
 * @method static void allIntegerish($values, $message = '')
93
 * @method static void allFloat($values, $message = '')
94
 * @method static void allNumeric($values, $message = '')
95
 * @method static void allBoolean($values, $message = '')
96
 * @method static void allScalar($values, $message = '')
97
 * @method static void allObject($values, $message = '')
98
 * @method static void allResource($values, $type = null, $message = '')
99
 * @method static void allIsCallable($values, $message = '')
100
 * @method static void allIsArray($values, $message = '')
101
 * @method static void allIsTraversable($values, $message = '')
102
 * @method static void allIsCountable($values, $message = '')
103
 * @method static void allIsInstanceOf($values, $class, $message = '')
104
 * @method static void allNotInstanceOf($values, $class, $message = '')
105
 * @method static void allNull($values, $message = '')
106
 * @method static void allNotNull($values, $message = '')
107
 * @method static void allIsEmpty($values, $message = '')
108
 * @method static void allNotEmpty($values, $message = '')
109
 * @method static void allTrue($values, $message = '')
110
 * @method static void allFalse($values, $message = '')
111
 * @method static void allEq($values, $value2, $message = '')
112
 * @method static void allNotEq($values,$value2,  $message = '')
113
 * @method static void allSame($values, $value2, $message = '')
114
 * @method static void allNotSame($values, $value2, $message = '')
115
 * @method static void allGreaterThan($values, $value2, $message = '')
116
 * @method static void allGreaterThanEq($values, $value2, $message = '')
117
 * @method static void allLessThan($values, $value2, $message = '')
118
 * @method static void allLessThanEq($values, $value2, $message = '')
119
 * @method static void allRange($values, $min, $max, $message = '')
120
 * @method static void allOneOf($values, $values, $message = '')
121
 * @method static void allContains($values, $subString, $message = '')
122
 * @method static void allNotWhitespaceOnly($values, $message = '')
123
 * @method static void allStartsWith($values, $prefix, $message = '')
124
 * @method static void allStartsWithLetter($values, $message = '')
125
 * @method static void allEndsWith($values, $suffix, $message = '')
126
 * @method static void allRegex($values, $pattern, $message = '')
127
 * @method static void allAlpha($values, $message = '')
128
 * @method static void allDigits($values, $message = '')
129
 * @method static void allAlnum($values, $message = '')
130
 * @method static void allLower($values, $message = '')
131
 * @method static void allUpper($values, $message = '')
132
 * @method static void allLength($values, $length, $message = '')
133
 * @method static void allMinLength($values, $min, $message = '')
134
 * @method static void allMaxLength($values, $max, $message = '')
135
 * @method static void allLengthBetween($values, $min, $max, $message = '')
136
 * @method static void allFileExists($values, $message = '')
137
 * @method static void allFile($values, $message = '')
138
 * @method static void allDirectory($values, $message = '')
139
 * @method static void allReadable($values, $message = '')
140
 * @method static void allWritable($values, $message = '')
141
 * @method static void allClassExists($values, $message = '')
142
 * @method static void allSubclassOf($values, $class, $message = '')
143
 * @method static void allImplementsInterface($values, $interface, $message = '')
144
 * @method static void allPropertyExists($values, $property, $message = '')
145
 * @method static void allPropertyNotExists($values, $property, $message = '')
146
 * @method static void allMethodExists($values, $method, $message = '')
147
 * @method static void allMethodNotExists($values, $method, $message = '')
148
 * @method static void allKeyExists($values, $key, $message = '')
149
 * @method static void allKeyNotExists($values, $key, $message = '')
150
 * @method static void allCount($values, $key, $message = '')
151
 * @method static void allMinCount($values, $min, $message = '')
152
 * @method static void allMaxCount($values, $max, $message = '')
153
 * @method static void allCountBetween($values, $min, $max, $message = '')
154
 * @method static void allUuid($values, $message = '')
155
 *
156
 * @since  1.0
157
 *
158
 * @author Bernhard Schussek <[email protected]>
159
 */
160
class Assert
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
161
{
162 94
    public static function string($value, $message = '')
163
    {
164 94
        if (!is_string($value)) {
165 14
            static::reportInvalidArgument(sprintf(
166 14
                $message ?: 'Expected a string. Got: %s',
167 14
                static::typeToString($value)
168
            ));
169
        }
170 80
    }
171
172 16
    public static function stringNotEmpty($value, $message = '')
173
    {
174 16
        static::string($value, $message);
175 12
        static::notEq($value, '', $message);
176 8
    }
177
178 17 View Code Duplication
    public static function integer($value, $message = '')
179
    {
180 17
        if (!is_int($value)) {
181 13
            static::reportInvalidArgument(sprintf(
182 13
                $message ?: 'Expected an integer. Got: %s',
183 13
                static::typeToString($value)
184
            ));
185
        }
186 4
    }
187
188 16 View Code Duplication
    public static function integerish($value, $message = '')
189
    {
190 16
        if (!is_numeric($value) || $value != (int) $value) {
191 4
            static::reportInvalidArgument(sprintf(
192 4
                $message ?: 'Expected an integerish value. Got: %s',
193 4
                static::typeToString($value)
194
            ));
195
        }
196 12
    }
197
198 16 View Code Duplication
    public static function float($value, $message = '')
199
    {
200 16
        if (!is_float($value)) {
201 8
            static::reportInvalidArgument(sprintf(
202 8
                $message ?: 'Expected a float. Got: %s',
203 8
                static::typeToString($value)
204
            ));
205
        }
206 8
    }
207
208 20 View Code Duplication
    public static function numeric($value, $message = '')
209
    {
210 20
        if (!is_numeric($value)) {
211 4
            static::reportInvalidArgument(sprintf(
212 4
                $message ?: 'Expected a numeric. Got: %s',
213 4
                static::typeToString($value)
214
            ));
215
        }
216 16
    }
217
218 24 View Code Duplication
    public static function natural($value, $message = '')
219
    {
220 24
        if (!is_int($value) || $value < 0) {
221 16
            static::reportInvalidArgument(sprintf(
222 16
                $message ?: 'Expected a non-negative integer. Got %s',
223 16
                static::valueToString($value)
224
            ));
225
        }
226 8
    }
227
228 16 View Code Duplication
    public static function boolean($value, $message = '')
229
    {
230 16
        if (!is_bool($value)) {
231 8
            static::reportInvalidArgument(sprintf(
232 8
                $message ?: 'Expected a boolean. Got: %s',
233 8
                static::typeToString($value)
234
            ));
235
        }
236 8
    }
237
238 23 View Code Duplication
    public static function scalar($value, $message = '')
239
    {
240 23
        if (!is_scalar($value)) {
241 11
            static::reportInvalidArgument(sprintf(
242 11
                $message ?: 'Expected a scalar. Got: %s',
243 11
                static::typeToString($value)
244
            ));
245
        }
246 12
    }
247
248 23 View Code Duplication
    public static function object($value, $message = '')
249
    {
250 23
        if (!is_object($value)) {
251 15
            static::reportInvalidArgument(sprintf(
252 15
                $message ?: 'Expected an object. Got: %s',
253 15
                static::typeToString($value)
254
            ));
255
        }
256 8
    }
257
258 16
    public static function resource($value, $type = null, $message = '')
259
    {
260 16
        if (!is_resource($value)) {
261 4
            static::reportInvalidArgument(sprintf(
262 4
                $message ?: 'Expected a resource. Got: %s',
263 4
                static::typeToString($value)
264
            ));
265
        }
266
267 12
        if ($type && $type !== get_resource_type($value)) {
268 4
            static::reportInvalidArgument(sprintf(
269 4
                $message ?: 'Expected a resource of type %2$s. Got: %s',
270 4
                static::typeToString($value),
271 4
                $type
272
            ));
273
        }
274 8
    }
275
276 20 View Code Duplication
    public static function isCallable($value, $message = '')
277
    {
278 20
        if (!is_callable($value)) {
279 8
            static::reportInvalidArgument(sprintf(
280 8
                $message ?: 'Expected a callable. Got: %s',
281 8
                static::typeToString($value)
282
            ));
283
        }
284 12
    }
285
286 20 View Code Duplication
    public static function isArray($value, $message = '')
287
    {
288 20
        if (!is_array($value)) {
289 12
            static::reportInvalidArgument(sprintf(
290 12
                $message ?: 'Expected an array. Got: %s',
291 12
                static::typeToString($value)
292
            ));
293
        }
294 8
    }
295
296 20
    public static function isTraversable($value, $message = '')
297
    {
298 20
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
299 20
            sprintf(
300 20
                'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.',
301 20
                __METHOD__
302
            ),
303 20
            E_USER_DEPRECATED
304
        );
305
306 20
        if (!is_array($value) && !($value instanceof Traversable)) {
307 8
            static::reportInvalidArgument(sprintf(
308 8
                $message ?: 'Expected a traversable. Got: %s',
309 8
                static::typeToString($value)
310
            ));
311
        }
312 12
    }
313
314 24 View Code Duplication
    public static function isCountable($value, $message = '')
315
    {
316 24
        if (!is_array($value) && !($value instanceof Countable)) {
317 12
            static::reportInvalidArgument(sprintf(
318 12
                $message ?: 'Expected a countable. Got: %s',
319 12
                static::typeToString($value)
320
            ));
321
        }
322 12
    }
323
324 580 View Code Duplication
    public static function isIterable($value, $message = '')
325
    {
326 580
        if (!is_array($value) && !($value instanceof Traversable)) {
327 8
            static::reportInvalidArgument(sprintf(
328 8
                $message ?: 'Expected an iterable. Got: %s',
329 8
                static::typeToString($value)
330
            ));
331
        }
332 576
    }
333
334 16 View Code Duplication
    public static function isInstanceOf($value, $class, $message = '')
335
    {
336 16
        if (!($value instanceof $class)) {
337 12
            static::reportInvalidArgument(sprintf(
338 12
                $message ?: 'Expected an instance of %2$s. Got: %s',
339 12
                static::typeToString($value),
340 12
                $class
341
            ));
342
        }
343 4
    }
344
345 16 View Code Duplication
    public static function notInstanceOf($value, $class, $message = '')
346
    {
347 16
        if ($value instanceof $class) {
348 4
            static::reportInvalidArgument(sprintf(
349 4
                $message ?: 'Expected an instance other than %2$s. Got: %s',
350 4
                static::typeToString($value),
351 4
                $class
352
            ));
353
        }
354 12
    }
355
356 23
    public static function isEmpty($value, $message = '')
357
    {
358 23
        if (!empty($value)) {
359 8
            static::reportInvalidArgument(sprintf(
360 8
                $message ?: 'Expected an empty value. Got: %s',
361 8
                static::valueToString($value)
362
            ));
363
        }
364 15
    }
365
366 23
    public static function notEmpty($value, $message = '')
367
    {
368 23
        if (empty($value)) {
369 15
            static::reportInvalidArgument(sprintf(
370 15
                $message ?: 'Expected a non-empty value. Got: %s',
371 15
                static::valueToString($value)
372
            ));
373
        }
374 8
    }
375
376 11 View Code Duplication
    public static function null($value, $message = '')
377
    {
378 11
        if (null !== $value) {
379 8
            static::reportInvalidArgument(sprintf(
380 8
                $message ?: 'Expected null. Got: %s',
381 8
                static::valueToString($value)
382
            ));
383
        }
384 3
    }
385
386 11
    public static function notNull($value, $message = '')
387
    {
388 11
        if (null === $value) {
389 3
            static::reportInvalidArgument(
390 3
                $message ?: 'Expected a value other than null.'
391
            );
392
        }
393 8
    }
394
395 15 View Code Duplication
    public static function true($value, $message = '')
396
    {
397 15
        if (true !== $value) {
398 11
            static::reportInvalidArgument(sprintf(
399 11
                $message ?: 'Expected a value to be true. Got: %s',
400 11
                static::valueToString($value)
401
            ));
402
        }
403 4
    }
404
405 19 View Code Duplication
    public static function false($value, $message = '')
406
    {
407 19
        if (false !== $value) {
408 15
            static::reportInvalidArgument(sprintf(
409 15
                $message ?: 'Expected a value to be false. Got: %s',
410 15
                static::valueToString($value)
411
            ));
412
        }
413 4
    }
414
415 32 View Code Duplication
    public static function eq($value, $value2, $message = '')
416
    {
417 32
        if ($value2 != $value) {
418 16
            static::reportInvalidArgument(sprintf(
419 16
                $message ?: 'Expected a value equal to %2$s. Got: %s',
420 16
                static::valueToString($value),
421 16
                static::valueToString($value2)
422
            ));
423
        }
424 16
    }
425
426 28
    public static function notEq($value, $value2, $message = '')
427
    {
428 28
        if ($value2 == $value) {
429 16
            static::reportInvalidArgument(sprintf(
430 16
                $message ?: 'Expected a different value than %s.',
431 16
                static::valueToString($value2)
432
            ));
433
        }
434 12
    }
435
436 16 View Code Duplication
    public static function same($value, $value2, $message = '')
437
    {
438 16
        if ($value2 !== $value) {
439 12
            static::reportInvalidArgument(sprintf(
440 12
                $message ?: 'Expected a value identical to %2$s. Got: %s',
441 12
                static::valueToString($value),
442 12
                static::valueToString($value2)
443
            ));
444
        }
445 4
    }
446
447 16
    public static function notSame($value, $value2, $message = '')
448
    {
449 16
        if ($value2 === $value) {
450 4
            static::reportInvalidArgument(sprintf(
451 4
                $message ?: 'Expected a value not identical to %s.',
452 4
                static::valueToString($value2)
453
            ));
454
        }
455 12
    }
456
457 8 View Code Duplication
    public static function greaterThan($value, $limit, $message = '')
458
    {
459 8
        if ($value <= $limit) {
460 4
            static::reportInvalidArgument(sprintf(
461 4
                $message ?: 'Expected a value greater than %2$s. Got: %s',
462 4
                static::valueToString($value),
463 4
                static::valueToString($limit)
464
            ));
465
        }
466 4
    }
467
468 12 View Code Duplication
    public static function greaterThanEq($value, $limit, $message = '')
469
    {
470 12
        if ($value < $limit) {
471 4
            static::reportInvalidArgument(sprintf(
472 4
                $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
473 4
                static::valueToString($value),
474 4
                static::valueToString($limit)
475
            ));
476
        }
477 8
    }
478
479 8 View Code Duplication
    public static function lessThan($value, $limit, $message = '')
480
    {
481 8
        if ($value >= $limit) {
482 4
            static::reportInvalidArgument(sprintf(
483 4
                $message ?: 'Expected a value less than %2$s. Got: %s',
484 4
                static::valueToString($value),
485 4
                static::valueToString($limit)
486
            ));
487
        }
488 4
    }
489
490 12 View Code Duplication
    public static function lessThanEq($value, $limit, $message = '')
491
    {
492 12
        if ($value > $limit) {
493 4
            static::reportInvalidArgument(sprintf(
494 4
                $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
495 4
                static::valueToString($value),
496 4
                static::valueToString($limit)
497
            ));
498
        }
499 8
    }
500
501 16 View Code Duplication
    public static function range($value, $min, $max, $message = '')
502
    {
503 16
        if ($value < $min || $value > $max) {
504 8
            static::reportInvalidArgument(sprintf(
505 8
                $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
506 8
                static::valueToString($value),
507 8
                static::valueToString($min),
508 8
                static::valueToString($max)
509
            ));
510
        }
511 8
    }
512
513 8
    public static function oneOf($value, array $values, $message = '')
514
    {
515 8
        if (!in_array($value, $values, true)) {
516 4
            static::reportInvalidArgument(sprintf(
517 4
                $message ?: 'Expected one of: %2$s. Got: %s',
518 4
                static::valueToString($value),
519 4
                implode(', ', array_map(array('static', 'valueToString'), $values))
520
            ));
521
        }
522 4
    }
523
524 20 View Code Duplication
    public static function contains($value, $subString, $message = '')
525
    {
526 20
        if (false === strpos($value, $subString)) {
527 8
            static::reportInvalidArgument(sprintf(
528 8
                $message ?: 'Expected a value to contain %2$s. Got: %s',
529 8
                static::valueToString($value),
530 8
                static::valueToString($subString)
531
            ));
532
        }
533 12
    }
534
535 40 View Code Duplication
    public static function notWhitespaceOnly($value, $message = '')
536
    {
537 40
        if (preg_match('/^\s*$/', $value)) {
538 24
            static::reportInvalidArgument(sprintf(
539 24
                $message ?: 'Expected a non-whitespace string. Got: %s',
540 24
                static::valueToString($value)
541
            ));
542
        }
543 16
    }
544
545 12 View Code Duplication
    public static function startsWith($value, $prefix, $message = '')
546
    {
547 12
        if (0 !== strpos($value, $prefix)) {
548 8
            static::reportInvalidArgument(sprintf(
549 8
                $message ?: 'Expected a value to start with %2$s. Got: %s',
550 8
                static::valueToString($value),
551 8
                static::valueToString($prefix)
552
            ));
553
        }
554 4
    }
555
556 12
    public static function startsWithLetter($value, $message = '')
557
    {
558 12
        $valid = isset($value[0]);
559
560 12
        if ($valid) {
561 8
            $locale = setlocale(LC_CTYPE, 0);
562 8
            setlocale(LC_CTYPE, 'C');
563 8
            $valid = ctype_alpha($value[0]);
564 8
            setlocale(LC_CTYPE, $locale);
565
        }
566
567 12
        if (!$valid) {
568 8
            static::reportInvalidArgument(sprintf(
569 8
                $message ?: 'Expected a value to start with a letter. Got: %s',
570 8
                static::valueToString($value)
571
            ));
572
        }
573 4
    }
574
575 12 View Code Duplication
    public static function endsWith($value, $suffix, $message = '')
576
    {
577 12
        if ($suffix !== substr($value, -static::strlen($suffix))) {
578 8
            static::reportInvalidArgument(sprintf(
579 8
                $message ?: 'Expected a value to end with %2$s. Got: %s',
580 8
                static::valueToString($value),
581 8
                static::valueToString($suffix)
582
            ));
583
        }
584 4
    }
585
586 12 View Code Duplication
    public static function regex($value, $pattern, $message = '')
587
    {
588 12
        if (!preg_match($pattern, $value)) {
589 8
            static::reportInvalidArgument(sprintf(
590 8
                $message ?: 'The value %s does not match the expected pattern.',
591 8
                static::valueToString($value)
592
            ));
593
        }
594 4
    }
595
596 12 View Code Duplication
    public static function alpha($value, $message = '')
597
    {
598 12
        $locale = setlocale(LC_CTYPE, 0);
599 12
        setlocale(LC_CTYPE, 'C');
600 12
        $valid = !ctype_alpha($value);
601 12
        setlocale(LC_CTYPE, $locale);
602
603 12
        if ($valid) {
604 8
            static::reportInvalidArgument(sprintf(
605 8
                $message ?: 'Expected a value to contain only letters. Got: %s',
606 8
                static::valueToString($value)
607
            ));
608
        }
609 4
    }
610
611 12 View Code Duplication
    public static function digits($value, $message = '')
612
    {
613 12
        $locale = setlocale(LC_CTYPE, 0);
614 12
        setlocale(LC_CTYPE, 'C');
615 12
        $valid = !ctype_digit($value);
616 12
        setlocale(LC_CTYPE, $locale);
617
618 12
        if ($valid) {
619 8
            static::reportInvalidArgument(sprintf(
620 8
                $message ?: 'Expected a value to contain digits only. Got: %s',
621 8
                static::valueToString($value)
622
            ));
623
        }
624 4
    }
625
626 12 View Code Duplication
    public static function alnum($value, $message = '')
627
    {
628 12
        $locale = setlocale(LC_CTYPE, 0);
629 12
        setlocale(LC_CTYPE, 'C');
630 12
        $valid = !ctype_alnum($value);
631 12
        setlocale(LC_CTYPE, $locale);
632
633 12
        if ($valid) {
634 8
            static::reportInvalidArgument(sprintf(
635 8
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
636 8
                static::valueToString($value)
637
            ));
638
        }
639 4
    }
640
641 16 View Code Duplication
    public static function lower($value, $message = '')
642
    {
643 16
        $locale = setlocale(LC_CTYPE, 0);
644 16
        setlocale(LC_CTYPE, 'C');
645 16
        $valid = !ctype_lower($value);
646 16
        setlocale(LC_CTYPE, $locale);
647
648 16
        if ($valid) {
649 12
            static::reportInvalidArgument(sprintf(
650 12
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
651 12
                static::valueToString($value)
652
            ));
653
        }
654 4
    }
655
656 16 View Code Duplication
    public static function upper($value, $message = '')
657
    {
658 16
        $locale = setlocale(LC_CTYPE, 0);
659 16
        setlocale(LC_CTYPE, 'C');
660 16
        $valid = !ctype_upper($value);
661 16
        setlocale(LC_CTYPE, $locale);
662
663 16
        if ($valid) {
664 12
            static::reportInvalidArgument(sprintf(
665 12
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
666 12
                static::valueToString($value)
667
            ));
668
        }
669 4
    }
670
671 24
    public static function length($value, $length, $message = '')
672
    {
673 24
        if ($length !== static::strlen($value)) {
674 16
            static::reportInvalidArgument(sprintf(
675 16
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
676 16
                static::valueToString($value),
677 16
                $length
678
            ));
679
        }
680 8
    }
681
682 24 View Code Duplication
    public static function minLength($value, $min, $message = '')
683
    {
684 24
        if (static::strlen($value) < $min) {
685 8
            static::reportInvalidArgument(sprintf(
686 8
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
687 8
                static::valueToString($value),
688 8
                $min
689
            ));
690
        }
691 16
    }
692
693 24 View Code Duplication
    public static function maxLength($value, $max, $message = '')
694
    {
695 24
        if (static::strlen($value) > $max) {
696 8
            static::reportInvalidArgument(sprintf(
697 8
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
698 8
                static::valueToString($value),
699 8
                $max
700
            ));
701
        }
702 16
    }
703
704 40 View Code Duplication
    public static function lengthBetween($value, $min, $max, $message = '')
705
    {
706 40
        $length = static::strlen($value);
707
708 40
        if ($length < $min || $length > $max) {
709 16
            static::reportInvalidArgument(sprintf(
710 16
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
711 16
                static::valueToString($value),
712 16
                $min,
713 16
                $max
714
            ));
715
        }
716 24
    }
717
718 36
    public static function fileExists($value, $message = '')
719
    {
720 36
        static::string($value);
721
722 36
        if (!file_exists($value)) {
723 12
            static::reportInvalidArgument(sprintf(
724 12
                $message ?: 'The file %s does not exist.',
725 12
                static::valueToString($value)
726
            ));
727
        }
728 24
    }
729
730 12 View Code Duplication
    public static function file($value, $message = '')
731
    {
732 12
        static::fileExists($value, $message);
733
734 8
        if (!is_file($value)) {
735 4
            static::reportInvalidArgument(sprintf(
736 4
                $message ?: 'The path %s is not a file.',
737 4
                static::valueToString($value)
738
            ));
739
        }
740 4
    }
741
742 12 View Code Duplication
    public static function directory($value, $message = '')
743
    {
744 12
        static::fileExists($value, $message);
745
746 8
        if (!is_dir($value)) {
747 4
            static::reportInvalidArgument(sprintf(
748 4
                $message ?: 'The path %s is no directory.',
749 4
                static::valueToString($value)
750
            ));
751
        }
752 4
    }
753
754 View Code Duplication
    public static function readable($value, $message = '')
755
    {
756
        if (!is_readable($value)) {
757
            static::reportInvalidArgument(sprintf(
758
                $message ?: 'The path %s is not readable.',
759
                static::valueToString($value)
760
            ));
761
        }
762
    }
763
764 View Code Duplication
    public static function writable($value, $message = '')
765
    {
766
        if (!is_writable($value)) {
767
            static::reportInvalidArgument(sprintf(
768
                $message ?: 'The path %s is not writable.',
769
                static::valueToString($value)
770
            ));
771
        }
772
    }
773
774 8 View Code Duplication
    public static function classExists($value, $message = '')
775
    {
776 8
        if (!class_exists($value)) {
777 4
            static::reportInvalidArgument(sprintf(
778 4
                $message ?: 'Expected an existing class name. Got: %s',
779 4
                static::valueToString($value)
780
            ));
781
        }
782 4
    }
783
784 8
    public static function subclassOf($value, $class, $message = '')
785
    {
786 8
        if (!is_subclass_of($value, $class)) {
787 4
            static::reportInvalidArgument(sprintf(
788 4
                $message ?: 'Expected a sub-class of %2$s. Got: %s',
789 4
                static::valueToString($value),
790 4
                static::valueToString($class)
791
            ));
792
        }
793 4
    }
794
795 8 View Code Duplication
    public static function implementsInterface($value, $interface, $message = '')
796
    {
797 8
        if (!in_array($interface, class_implements($value))) {
798 4
            static::reportInvalidArgument(sprintf(
799 4
                $message ?: 'Expected an implementation of %2$s. Got: %s',
800 4
                static::valueToString($value),
801 4
                static::valueToString($interface)
802
            ));
803
        }
804 4
    }
805
806 12 View Code Duplication
    public static function propertyExists($classOrObject, $property, $message = '')
807
    {
808 12
        if (!property_exists($classOrObject, $property)) {
809 4
            static::reportInvalidArgument(sprintf(
810 4
                $message ?: 'Expected the property %s to exist.',
811 4
                static::valueToString($property)
812
            ));
813
        }
814 8
    }
815
816 12 View Code Duplication
    public static function propertyNotExists($classOrObject, $property, $message = '')
817
    {
818 12
        if (property_exists($classOrObject, $property)) {
819 8
            static::reportInvalidArgument(sprintf(
820 8
                $message ?: 'Expected the property %s to not exist.',
821 8
                static::valueToString($property)
822
            ));
823
        }
824 4
    }
825
826 27 View Code Duplication
    public static function methodExists($classOrObject, $method, $message = '')
827
    {
828 27
        if (!method_exists($classOrObject, $method)) {
829 19
            static::reportInvalidArgument(sprintf(
830 19
                $message ?: 'Expected the method %s to exist.',
831 19
                static::valueToString($method)
832
            ));
833
        }
834 8
    }
835
836 27 View Code Duplication
    public static function methodNotExists($classOrObject, $method, $message = '')
837
    {
838 27
        if (method_exists($classOrObject, $method)) {
839 8
            static::reportInvalidArgument(sprintf(
840 8
                $message ?: 'Expected the method %s to not exist.',
841 8
                static::valueToString($method)
842
            ));
843
        }
844 19
    }
845
846 12 View Code Duplication
    public static function keyExists($array, $key, $message = '')
847
    {
848 12
        if (!array_key_exists($key, $array)) {
849 4
            static::reportInvalidArgument(sprintf(
850 4
                $message ?: 'Expected the key %s to exist.',
851 4
                static::valueToString($key)
852
            ));
853
        }
854 8
    }
855
856 12 View Code Duplication
    public static function keyNotExists($array, $key, $message = '')
857
    {
858 12
        if (array_key_exists($key, $array)) {
859 8
            static::reportInvalidArgument(sprintf(
860 8
                $message ?: 'Expected the key %s to not exist.',
861 8
                static::valueToString($key)
862
            ));
863
        }
864 4
    }
865
866 8
    public static function count($array, $number, $message = '')
867
    {
868 8
        static::eq(
869 8
            count($array),
870 8
            $number,
871 8
            $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array))
872
        );
873 4
    }
874
875 12 View Code Duplication
    public static function minCount($array, $min, $message = '')
876
    {
877 12
        if (count($array) < $min) {
878 4
            static::reportInvalidArgument(sprintf(
879 4
                $message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
880 4
                count($array),
881 4
                $min
882
            ));
883
        }
884 8
    }
885
886 12 View Code Duplication
    public static function maxCount($array, $max, $message = '')
887
    {
888 12
        if (count($array) > $max) {
889 4
            static::reportInvalidArgument(sprintf(
890 4
                $message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
891 4
                count($array),
892 4
                $max
893
            ));
894
        }
895 8
    }
896
897 12
    public static function countBetween($array, $min, $max, $message = '')
898
    {
899 12
        $count = count($array);
900
901 12
        if ($count < $min || $count > $max) {
902 8
            static::reportInvalidArgument(sprintf(
903 8
                $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
904 8
                $count,
905 8
                $min,
906 8
                $max
907
            ));
908
        }
909 4
    }
910
911 48
    public static function uuid($value, $message = '')
912
    {
913 48
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
914
915
        // The nil UUID is special form of UUID that is specified to have all
916
        // 128 bits set to zero.
917 48
        if ('00000000-0000-0000-0000-000000000000' === $value) {
918 4
            return;
919
        }
920
921 44
        if (!preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) {
922 20
            static::reportInvalidArgument(sprintf(
923 20
                $message ?: 'Value %s is not a valid UUID.',
924 20
                static::valueToString($value)
925
            ));
926
        }
927 24
    }
928
929 24
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
930
    {
931 24
        static::string($class);
932
933 24
        $actual = 'none';
934
935
        try {
936 24
            $expression();
937 24
        } catch (Exception $e) {
938 20
            $actual = get_class($e);
939 20
            if ($e instanceof $class) {
940 20
                return;
941
            }
942 4
        } catch (Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
943 4
            $actual = get_class($e);
944 4
            if ($e instanceof $class) {
945 4
                return;
946
            }
947
        }
948
949 8
        static::reportInvalidArgument($message ?: sprintf(
950 8
            'Expected to throw "%s", got "%s"',
951 8
            $class,
952 8
            $actual
953
        ));
954
    }
955
956 922
    public static function __callStatic($name, $arguments)
957
    {
958 922
        if ('nullOr' === substr($name, 0, 6)) {
959 352
            if (null !== $arguments[0]) {
960 275
                $method = lcfirst(substr($name, 6));
961 275
                call_user_func_array(array('static', $method), $arguments);
962
            }
963
964 214
            return;
965
        }
966
967 570
        if ('all' === substr($name, 0, 3)) {
968 570
            static::isIterable($arguments[0]);
969
970 570
            $method = lcfirst(substr($name, 3));
971 570
            $args = $arguments;
972
973 570
            foreach ($arguments[0] as $entry) {
974 570
                $args[0] = $entry;
975
976 570
                call_user_func_array(array('static', $method), $args);
977
            }
978
979 280
            return;
980
        }
981
982
        throw new BadMethodCallException('No such method: '.$name);
983
    }
984
985 412
    protected static function valueToString($value)
986
    {
987 412
        if (null === $value) {
988 11
            return 'null';
989
        }
990
991 403
        if (true === $value) {
992 15
            return 'true';
993
        }
994
995 393
        if (false === $value) {
996 13
            return 'false';
997
        }
998
999 380
        if (is_array($value)) {
1000 1
            return 'array';
1001
        }
1002
1003 379
        if (is_object($value)) {
1004 1
            return get_class($value);
1005
        }
1006
1007 378
        if (is_resource($value)) {
1008 1
            return 'resource';
1009
        }
1010
1011 378
        if (is_string($value)) {
1012 292
            return '"'.$value.'"';
1013
        }
1014
1015 94
        return (string) $value;
1016
    }
1017
1018 149
    protected static function typeToString($value)
1019
    {
1020 149
        return is_object($value) ? get_class($value) : gettype($value);
1021
    }
1022
1023 124
    protected static function strlen($value)
1024
    {
1025 124
        if (!function_exists('mb_detect_encoding')) {
1026
            return strlen($value);
1027
        }
1028
1029 124
        if (false === $encoding = mb_detect_encoding($value)) {
1030
            return strlen($value);
1031
        }
1032
1033 124
        return mb_strwidth($value, $encoding);
1034
    }
1035
1036 588
    protected static function reportInvalidArgument($message)
1037
    {
1038 588
        throw new InvalidArgumentException($message);
1039
    }
1040
1041
    private function __construct()
1042
    {
1043
    }
1044
}
1045