Failed Conditions
Pull Request — master (#18)
by Mateusz
38:24 queued 18:26
created

Assert::null()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
crap 3
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 InvalidArgumentException;
16
use Traversable;
17
18
/**
19
 * Efficient assertions to validate the input/output of your methods.
20
 *
21
 * @method static void nullOrString($value, $message = '')
22
 * @method static void nullOrStringNotEmpty($value, $message = '')
23
 * @method static void nullOrInteger($value, $message = '')
24
 * @method static void nullOrIntegerish($value, $message = '')
25
 * @method static void nullOrFloat($value, $message = '')
26
 * @method static void nullOrNumeric($value, $message = '')
27
 * @method static void nullOrBoolean($value, $message = '')
28
 * @method static void nullOrScalar($value, $message = '')
29
 * @method static void nullOrObject($value, $message = '')
30
 * @method static void nullOrResource($value, $type = null, $message = '')
31
 * @method static void nullOrIsCallable($value, $message = '')
32
 * @method static void nullOrIsArray($value, $message = '')
33
 * @method static void nullOrIsTraversable($value, $message = '')
34
 * @method static void nullOrIsInstanceOf($value, $class, $message = '')
35
 * @method static void nullOrNotInstanceOf($value, $class, $message = '')
36
 * @method static void nullOrIsEmpty($value, $message = '')
37
 * @method static void nullOrNotEmpty($value, $message = '')
38
 * @method static void nullOrTrue($value, $message = '')
39
 * @method static void nullOrFalse($value, $message = '')
40
 * @method static void nullOrEq($value, $value2, $message = '')
41
 * @method static void nullOrNotEq($value,$value2,  $message = '')
42
 * @method static void nullOrSame($value, $value2, $message = '')
43
 * @method static void nullOrNotSame($value, $value2, $message = '')
44
 * @method static void nullOrGreaterThan($value, $value2, $message = '')
45
 * @method static void nullOrGreaterThanEq($value, $value2, $message = '')
46
 * @method static void nullOrLessThan($value, $value2, $message = '')
47
 * @method static void nullOrLessThanEq($value, $value2, $message = '')
48
 * @method static void nullOrRange($value, $min, $max, $message = '')
49
 * @method static void nullOrOneOf($value, $values, $message = '')
50
 * @method static void nullOrContains($value, $subString, $message = '')
51
 * @method static void nullOrStartsWith($value, $prefix, $message = '')
52
 * @method static void nullOrStartsWithLetter($value, $message = '')
53
 * @method static void nullOrEndsWith($value, $suffix, $message = '')
54
 * @method static void nullOrRegex($value, $pattern, $message = '')
55
 * @method static void nullOrAlpha($value, $message = '')
56
 * @method static void nullOrDigits($value, $message = '')
57
 * @method static void nullOrAlnum($value, $message = '')
58
 * @method static void nullOrLower($value, $message = '')
59
 * @method static void nullOrUpper($value, $message = '')
60
 * @method static void nullOrLength($value, $length, $message = '')
61
 * @method static void nullOrMinLength($value, $min, $message = '')
62
 * @method static void nullOrMaxLength($value, $max, $message = '')
63
 * @method static void nullOrLengthBetween($value, $min, $max, $message = '')
64
 * @method static void nullOrFileExists($value, $message = '')
65
 * @method static void nullOrFile($value, $message = '')
66
 * @method static void nullOrDirectory($value, $message = '')
67
 * @method static void nullOrReadable($value, $message = '')
68
 * @method static void nullOrWritable($value, $message = '')
69
 * @method static void nullOrClassExists($value, $message = '')
70
 * @method static void nullOrSubclassOf($value, $class, $message = '')
71
 * @method static void nullOrImplementsInterface($value, $interface, $message = '')
72
 * @method static void nullOrPropertyExists($value, $property, $message = '')
73
 * @method static void nullOrPropertyNotExists($value, $property, $message = '')
74
 * @method static void nullOrMethodExists($value, $method, $message = '')
75
 * @method static void nullOrMethodNotExists($value, $method, $message = '')
76
 * @method static void nullOrKeyExists($value, $key, $message = '')
77
 * @method static void nullOrKeyNotExists($value, $key, $message = '')
78
 * @method static void nullOrCount($value, $key, $message = '')
79
 * @method static void nullOrUuid($values, $message = '')
80
 * @method static void allString($values, $message = '')
81
 * @method static void allStringNotEmpty($values, $message = '')
82
 * @method static void allInteger($values, $message = '')
83
 * @method static void allIntegerish($values, $message = '')
84
 * @method static void allFloat($values, $message = '')
85
 * @method static void allNumeric($values, $message = '')
86
 * @method static void allBoolean($values, $message = '')
87
 * @method static void allScalar($values, $message = '')
88
 * @method static void allObject($values, $message = '')
89
 * @method static void allResource($values, $type = null, $message = '')
90
 * @method static void allIsCallable($values, $message = '')
91
 * @method static void allIsArray($values, $message = '')
92
 * @method static void allIsTraversable($values, $message = '')
93
 * @method static void allIsInstanceOf($values, $class, $message = '')
94
 * @method static void allNotInstanceOf($values, $class, $message = '')
95
 * @method static void allNull($values, $message = '')
96
 * @method static void allNotNull($values, $message = '')
97
 * @method static void allIsEmpty($values, $message = '')
98
 * @method static void allNotEmpty($values, $message = '')
99
 * @method static void allTrue($values, $message = '')
100
 * @method static void allFalse($values, $message = '')
101
 * @method static void allEq($values, $value2, $message = '')
102
 * @method static void allNotEq($values,$value2,  $message = '')
103
 * @method static void allSame($values, $value2, $message = '')
104
 * @method static void allNotSame($values, $value2, $message = '')
105
 * @method static void allGreaterThan($values, $value2, $message = '')
106
 * @method static void allGreaterThanEq($values, $value2, $message = '')
107
 * @method static void allLessThan($values, $value2, $message = '')
108
 * @method static void allLessThanEq($values, $value2, $message = '')
109
 * @method static void allRange($values, $min, $max, $message = '')
110
 * @method static void allOneOf($values, $values, $message = '')
111
 * @method static void allContains($values, $subString, $message = '')
112
 * @method static void allStartsWith($values, $prefix, $message = '')
113
 * @method static void allStartsWithLetter($values, $message = '')
114
 * @method static void allEndsWith($values, $suffix, $message = '')
115
 * @method static void allRegex($values, $pattern, $message = '')
116
 * @method static void allAlpha($values, $message = '')
117
 * @method static void allDigits($values, $message = '')
118
 * @method static void allAlnum($values, $message = '')
119
 * @method static void allLower($values, $message = '')
120
 * @method static void allUpper($values, $message = '')
121
 * @method static void allLength($values, $length, $message = '')
122
 * @method static void allMinLength($values, $min, $message = '')
123
 * @method static void allMaxLength($values, $max, $message = '')
124
 * @method static void allLengthBetween($values, $min, $max, $message = '')
125
 * @method static void allFileExists($values, $message = '')
126
 * @method static void allFile($values, $message = '')
127
 * @method static void allDirectory($values, $message = '')
128
 * @method static void allReadable($values, $message = '')
129
 * @method static void allWritable($values, $message = '')
130
 * @method static void allClassExists($values, $message = '')
131
 * @method static void allSubclassOf($values, $class, $message = '')
132
 * @method static void allImplementsInterface($values, $interface, $message = '')
133
 * @method static void allPropertyExists($values, $property, $message = '')
134
 * @method static void allPropertyNotExists($values, $property, $message = '')
135
 * @method static void allMethodExists($values, $method, $message = '')
136
 * @method static void allMethodNotExists($values, $method, $message = '')
137
 * @method static void allKeyExists($values, $key, $message = '')
138
 * @method static void allKeyNotExists($values, $key, $message = '')
139
 * @method static void allCount($values, $key, $message = '')
140
 * @method static void allUuid($values, $message = '')
141
 *
142
 * @since  1.0
143
 *
144
 * @author Bernhard Schussek <[email protected]>
145
 */
146 66
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...
147
{
148 66 View Code Duplication
    public static function string($value, $message = '')
149 14
    {
150 14
        if (!is_string($value)) {
151 14
            throw static::createInvalidArgumentException(sprintf(
152
                $message ?: 'Expected a string. Got: %s',
153
                static::typeToString($value)
154 52
            ));
155
        }
156 12
    }
157
158 12
    public static function stringNotEmpty($value, $message = '')
159 8
    {
160 4
        static::string($value, $message);
161
        static::notEmpty($value, $message);
162 17
    }
163
164 17 View Code Duplication
    public static function integer($value, $message = '')
165 13
    {
166 13
        if (!is_int($value)) {
167 13
            throw static::createInvalidArgumentException(sprintf(
168
                $message ?: 'Expected an integer. Got: %s',
169
                static::typeToString($value)
170 4
            ));
171
        }
172 16
    }
173
174 16 View Code Duplication
    public static function integerish($value, $message = '')
175 4
    {
176 4
        if (!is_numeric($value) || $value != (int) $value) {
177 4
            throw static::createInvalidArgumentException(sprintf(
178
                $message ?: 'Expected an integerish value. Got: %s',
179
                static::typeToString($value)
180 12
            ));
181
        }
182 16
    }
183
184 16 View Code Duplication
    public static function float($value, $message = '')
185 8
    {
186 8
        if (!is_float($value)) {
187 8
            throw static::createInvalidArgumentException(sprintf(
188
                $message ?: 'Expected a float. Got: %s',
189
                static::typeToString($value)
190 8
            ));
191
        }
192 20
    }
193
194 20 View Code Duplication
    public static function numeric($value, $message = '')
195 4
    {
196 4
        if (!is_numeric($value)) {
197 4
            throw static::createInvalidArgumentException(sprintf(
198
                $message ?: 'Expected a numeric. Got: %s',
199
                static::typeToString($value)
200 16
            ));
201
        }
202 16
    }
203
204 16 View Code Duplication
    public static function boolean($value, $message = '')
205 8
    {
206 8
        if (!is_bool($value)) {
207 8
            throw static::createInvalidArgumentException(sprintf(
208
                $message ?: 'Expected a boolean. Got: %s',
209
                static::typeToString($value)
210 8
            ));
211
        }
212 23
    }
213
214 23 View Code Duplication
    public static function scalar($value, $message = '')
215 11
    {
216 11
        if (!is_scalar($value)) {
217 11
            throw static::createInvalidArgumentException(sprintf(
218
                $message ?: 'Expected a scalar. Got: %s',
219
                static::typeToString($value)
220 12
            ));
221
        }
222 23
    }
223
224 23 View Code Duplication
    public static function object($value, $message = '')
225 15
    {
226 15
        if (!is_object($value)) {
227 15
            throw static::createInvalidArgumentException(sprintf(
228
                $message ?: 'Expected an object. Got: %s',
229
                static::typeToString($value)
230 8
            ));
231
        }
232 16
    }
233
234 16
    public static function resource($value, $type = null, $message = '')
235 4
    {
236 4
        if (!is_resource($value)) {
237 4
            throw static::createInvalidArgumentException(sprintf(
238
                $message ?: 'Expected a resource. Got: %s',
239
                static::typeToString($value)
240
            ));
241 12
        }
242 4
243 4
        if ($type && $type !== get_resource_type($value)) {
244 4
            throw static::createInvalidArgumentException(sprintf(
245
                $message ?: 'Expected a resource of type %2$s. Got: %s',
246
                static::typeToString($value),
247
                $type
248 8
            ));
249
        }
250 20
    }
251
252 20 View Code Duplication
    public static function isCallable($value, $message = '')
253 8
    {
254 8
        if (!is_callable($value)) {
255 8
            throw static::createInvalidArgumentException(sprintf(
256
                $message ?: 'Expected a callable. Got: %s',
257
                static::typeToString($value)
258 12
            ));
259
        }
260 20
    }
261
262 20 View Code Duplication
    public static function isArray($value, $message = '')
263 12
    {
264 12
        if (!is_array($value)) {
265 12
            throw static::createInvalidArgumentException(sprintf(
266
                $message ?: 'Expected an array. Got: %s',
267
                static::typeToString($value)
268 8
            ));
269
        }
270 490
    }
271
272 490
    public static function isTraversable($value, $message = '')
273 8
    {
274 8
        if (!is_array($value) && !($value instanceof Traversable)) {
275 8
            throw static::createInvalidArgumentException(sprintf(
276
                $message ?: 'Expected a traversable. Got: %s',
277
                static::typeToString($value)
278 486
            ));
279
        }
280 16
    }
281
282 16 View Code Duplication
    public static function isInstanceOf($value, $class, $message = '')
283 12
    {
284 12
        if (!($value instanceof $class)) {
285 12
            throw static::createInvalidArgumentException(sprintf(
286
                $message ?: 'Expected an instance of %2$s. Got: %s',
287
                static::typeToString($value),
288
                $class
289 4
            ));
290
        }
291 16
    }
292
293 16 View Code Duplication
    public static function notInstanceOf($value, $class, $message = '')
294 4
    {
295 4
        if ($value instanceof $class) {
296 4
            throw static::createInvalidArgumentException(sprintf(
297
                $message ?: 'Expected an instance other than %2$s. Got: %s',
298
                static::typeToString($value),
299
                $class
300 12
            ));
301
        }
302 23
    }
303
304 23
    public static function isEmpty($value, $message = '')
305 8
    {
306 8
        if (!empty($value)) {
307 8
            throw static::createInvalidArgumentException(sprintf(
308
                $message ?: 'Expected an empty value. Got: %s',
309
                static::valueToString($value)
310 15
            ));
311
        }
312 31
    }
313
314 31
    public static function notEmpty($value, $message = '')
315 19
    {
316 19
        if (empty($value)) {
317 19
            throw static::createInvalidArgumentException(sprintf(
318
                $message ?: 'Expected a non-empty value. Got: %s',
319
                static::valueToString($value)
320 12
            ));
321
        }
322 11
    }
323
324 11 View Code Duplication
    public static function null($value, $message = '')
325 8
    {
326 8
        if (null !== $value) {
327 8
            throw static::createInvalidArgumentException(sprintf(
328
                $message ?: 'Expected null. Got: %s',
329
                static::valueToString($value)
330 3
            ));
331
        }
332 11
    }
333
334 11
    public static function notNull($value, $message = '')
335 3
    {
336 3
        if (null === $value) {
337
            throw static::createInvalidArgumentException(
338
                $message ?: 'Expected a value other than null.'
339 8
            );
340
        }
341 15
    }
342
343 15 View Code Duplication
    public static function true($value, $message = '')
344 11
    {
345 11
        if (true !== $value) {
346 11
            throw static::createInvalidArgumentException(sprintf(
347
                $message ?: 'Expected a value to be true. Got: %s',
348
                static::valueToString($value)
349 4
            ));
350
        }
351 19
    }
352
353 19 View Code Duplication
    public static function false($value, $message = '')
354 15
    {
355 15
        if (false !== $value) {
356 15
            throw static::createInvalidArgumentException(sprintf(
357
                $message ?: 'Expected a value to be false. Got: %s',
358
                static::valueToString($value)
359 4
            ));
360
        }
361 24
    }
362
363 24
    public static function eq($value, $value2, $message = '')
364 12
    {
365 12
        if ($value2 != $value) {
366 12
            throw static::createInvalidArgumentException(sprintf(
367 12
                $message ?: 'Expected a value equal to %2$s. Got: %s',
368
                static::valueToString($value),
369
                static::valueToString($value2)
370 12
            ));
371
        }
372 16
    }
373
374 16
    public static function notEq($value, $value2, $message = '')
375 12
    {
376 12
        if ($value2 == $value) {
377 12
            throw static::createInvalidArgumentException(sprintf(
378
                $message ?: 'Expected a different value than %s.',
379
                static::valueToString($value2)
380 4
            ));
381
        }
382 16
    }
383
384 16
    public static function same($value, $value2, $message = '')
385 12
    {
386 12
        if ($value2 !== $value) {
387 12
            throw static::createInvalidArgumentException(sprintf(
388 12
                $message ?: 'Expected a value identical to %2$s. Got: %s',
389
                static::valueToString($value),
390
                static::valueToString($value2)
391 4
            ));
392
        }
393 16
    }
394
395 16
    public static function notSame($value, $value2, $message = '')
396 4
    {
397 4
        if ($value2 === $value) {
398 4
            throw static::createInvalidArgumentException(sprintf(
399
                $message ?: 'Expected a value not identical to %s.',
400
                static::valueToString($value2)
401 12
            ));
402
        }
403 8
    }
404
405 8
    public static function greaterThan($value, $limit, $message = '')
406 4
    {
407 4
        if ($value <= $limit) {
408 4
            throw static::createInvalidArgumentException(sprintf(
409 4
                $message ?: 'Expected a value greater than %2$s. Got: %s',
410
                static::valueToString($value),
411
                static::valueToString($limit)
412 4
            ));
413
        }
414 12
    }
415
416 12
    public static function greaterThanEq($value, $limit, $message = '')
417 4
    {
418 4
        if ($value < $limit) {
419 4
            throw static::createInvalidArgumentException(sprintf(
420 4
                $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
421
                static::valueToString($value),
422
                static::valueToString($limit)
423 8
            ));
424
        }
425 8
    }
426
427 8
    public static function lessThan($value, $limit, $message = '')
428 4
    {
429 4
        if ($value >= $limit) {
430 4
            throw static::createInvalidArgumentException(sprintf(
431 4
                $message ?: 'Expected a value less than %2$s. Got: %s',
432
                static::valueToString($value),
433
                static::valueToString($limit)
434 4
            ));
435
        }
436 12
    }
437
438 12
    public static function lessThanEq($value, $limit, $message = '')
439 4
    {
440 4
        if ($value > $limit) {
441 4
            throw static::createInvalidArgumentException(sprintf(
442 4
                $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
443
                static::valueToString($value),
444
                static::valueToString($limit)
445 8
            ));
446
        }
447 16
    }
448
449 16 View Code Duplication
    public static function range($value, $min, $max, $message = '')
450 8
    {
451 8
        if ($value < $min || $value > $max) {
452 8
            throw static::createInvalidArgumentException(sprintf(
453 8
                $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
454 8
                static::valueToString($value),
455
                static::valueToString($min),
456
                static::valueToString($max)
457 8
            ));
458
        }
459 8
    }
460
461 8
    public static function oneOf($value, array $values, $message = '')
462 4
    {
463 4
        if (!in_array($value, $values, true)) {
464 4
            throw static::createInvalidArgumentException(sprintf(
465 4
                $message ?: 'Expected one of: %2$s. Got: %s',
466
                static::valueToString($value),
467
                implode(', ', array_map(array('static', 'valueToString'), $values))
468 4
            ));
469
        }
470 20
    }
471
472 20 View Code Duplication
    public static function contains($value, $subString, $message = '')
473 8
    {
474 8
        if (false === strpos($value, $subString)) {
475 8
            throw static::createInvalidArgumentException(sprintf(
476 8
                $message ?: 'Expected a value to contain %2$s. Got: %s',
477
                static::valueToString($value),
478
                static::valueToString($subString)
479 12
            ));
480
        }
481 12
    }
482
483 12 View Code Duplication
    public static function startsWith($value, $prefix, $message = '')
484 8
    {
485 8
        if (0 !== strpos($value, $prefix)) {
486 8
            throw static::createInvalidArgumentException(sprintf(
487 8
                $message ?: 'Expected a value to start with %2$s. Got: %s',
488
                static::valueToString($value),
489
                static::valueToString($prefix)
490 4
            ));
491
        }
492 12
    }
493
494 12
    public static function startsWithLetter($value, $message = '')
495
    {
496 12
        $valid = isset($value[0]);
497 8
498 8
        if ($valid) {
499 8
            $locale = setlocale(LC_CTYPE, 0);
500 8
            setlocale(LC_CTYPE, 'C');
501
            $valid = ctype_alpha($value[0]);
502
            setlocale(LC_CTYPE, $locale);
503 12
        }
504 8
505 8
        if (!$valid) {
506 8
            throw static::createInvalidArgumentException(sprintf(
507
                $message ?: 'Expected a value to start with a letter. Got: %s',
508
                static::valueToString($value)
509 4
            ));
510
        }
511 12
    }
512
513 12 View Code Duplication
    public static function endsWith($value, $suffix, $message = '')
514 8
    {
515 8
        if ($suffix !== substr($value, -static::strlen($suffix))) {
516 8
            throw static::createInvalidArgumentException(sprintf(
517 8
                $message ?: 'Expected a value to end with %2$s. Got: %s',
518
                static::valueToString($value),
519
                static::valueToString($suffix)
520 4
            ));
521
        }
522 12
    }
523
524 12
    public static function regex($value, $pattern, $message = '')
525 8
    {
526 8
        if (!preg_match($pattern, $value)) {
527 8
            throw static::createInvalidArgumentException(sprintf(
528
                $message ?: 'The value %s does not match the expected pattern.',
529
                static::valueToString($value)
530 4
            ));
531
        }
532 12
    }
533
534 12 View Code Duplication
    public static function alpha($value, $message = '')
535 12
    {
536 12
        $locale = setlocale(LC_CTYPE, 0);
537 12
        setlocale(LC_CTYPE, 'C');
538
        $valid = !ctype_alpha($value);
539 12
        setlocale(LC_CTYPE, $locale);
540 8
541 8
        if ($valid) {
542 8
            throw static::createInvalidArgumentException(sprintf(
543
                $message ?: 'Expected a value to contain only letters. Got: %s',
544
                static::valueToString($value)
545 4
            ));
546
        }
547 12
    }
548
549 12 View Code Duplication
    public static function digits($value, $message = '')
550 12
    {
551 12
        $locale = setlocale(LC_CTYPE, 0);
552 12
        setlocale(LC_CTYPE, 'C');
553
        $valid = !ctype_digit($value);
554 12
        setlocale(LC_CTYPE, $locale);
555 8
556 8
        if ($valid) {
557 8
            throw static::createInvalidArgumentException(sprintf(
558
                $message ?: 'Expected a value to contain digits only. Got: %s',
559
                static::valueToString($value)
560 4
            ));
561
        }
562 12
    }
563
564 12 View Code Duplication
    public static function alnum($value, $message = '')
565 12
    {
566 12
        $locale = setlocale(LC_CTYPE, 0);
567 12
        setlocale(LC_CTYPE, 'C');
568
        $valid = !ctype_alnum($value);
569 12
        setlocale(LC_CTYPE, $locale);
570 8
571 8
        if ($valid) {
572 8
            throw static::createInvalidArgumentException(sprintf(
573
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
574
                static::valueToString($value)
575 4
            ));
576
        }
577 16
    }
578
579 16 View Code Duplication
    public static function lower($value, $message = '')
580 16
    {
581 16
        $locale = setlocale(LC_CTYPE, 0);
582 16
        setlocale(LC_CTYPE, 'C');
583
        $valid = !ctype_lower($value);
584 16
        setlocale(LC_CTYPE, $locale);
585 12
586 12
        if ($valid) {
587 12
            throw static::createInvalidArgumentException(sprintf(
588
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
589
                static::valueToString($value)
590 4
            ));
591
        }
592 16
    }
593
594 16 View Code Duplication
    public static function upper($value, $message = '')
595 16
    {
596 16
        $locale = setlocale(LC_CTYPE, 0);
597 16
        setlocale(LC_CTYPE, 'C');
598
        $valid = !ctype_upper($value);
599 16
        setlocale(LC_CTYPE, $locale);
600 12
601 12
        if ($valid) {
602 12
            throw static::createInvalidArgumentException(sprintf(
603
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
604
                static::valueToString($value)
605 4
            ));
606
        }
607 24
    }
608
609 24 View Code Duplication
    public static function length($value, $length, $message = '')
610 16
    {
611 16
        if ($length !== static::strlen($value)) {
612 16
            throw static::createInvalidArgumentException(sprintf(
613
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
614
                static::valueToString($value),
615
                $length
616 8
            ));
617
        }
618 24
    }
619
620 24 View Code Duplication
    public static function minLength($value, $min, $message = '')
621 8
    {
622 8
        if (static::strlen($value) < $min) {
623 8
            throw static::createInvalidArgumentException(sprintf(
624
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
625
                static::valueToString($value),
626
                $min
627 16
            ));
628
        }
629 24
    }
630
631 24 View Code Duplication
    public static function maxLength($value, $max, $message = '')
632 8
    {
633 8
        if (static::strlen($value) > $max) {
634 8
            throw static::createInvalidArgumentException(sprintf(
635
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
636
                static::valueToString($value),
637
                $max
638 16
            ));
639
        }
640 40
    }
641
642 40 View Code Duplication
    public static function lengthBetween($value, $min, $max, $message = '')
643
    {
644 40
        $length = static::strlen($value);
645 16
646 16
        if ($length < $min || $length > $max) {
647 16
            throw static::createInvalidArgumentException(sprintf(
648
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
649
                static::valueToString($value),
650
                $min,
651
                $max
652 24
            ));
653
        }
654 36
    }
655
656 36
    public static function fileExists($value, $message = '')
657
    {
658 36
        static::string($value);
659 12
660 12
        if (!file_exists($value)) {
661 12
            throw static::createInvalidArgumentException(sprintf(
662
                $message ?: 'The file %s does not exist.',
663
                static::valueToString($value)
664 24
            ));
665
        }
666 12
    }
667
668 12 View Code Duplication
    public static function file($value, $message = '')
669
    {
670 8
        static::fileExists($value, $message);
671 4
672 4
        if (!is_file($value)) {
673 4
            throw static::createInvalidArgumentException(sprintf(
674
                $message ?: 'The path %s is not a file.',
675
                static::valueToString($value)
676 4
            ));
677
        }
678 12
    }
679
680 12 View Code Duplication
    public static function directory($value, $message = '')
681
    {
682 8
        static::fileExists($value, $message);
683 4
684 4
        if (!is_dir($value)) {
685 4
            throw static::createInvalidArgumentException(sprintf(
686
                $message ?: 'The path %s is no directory.',
687
                static::valueToString($value)
688 4
            ));
689
        }
690
    }
691
692
    public static function readable($value, $message = '')
693
    {
694
        if (!is_readable($value)) {
695
            throw static::createInvalidArgumentException(sprintf(
696
                $message ?: 'The path %s is not readable.',
697
                static::valueToString($value)
698
            ));
699
        }
700
    }
701
702
    public static function writable($value, $message = '')
703
    {
704
        if (!is_writable($value)) {
705
            throw static::createInvalidArgumentException(sprintf(
706
                $message ?: 'The path %s is not writable.',
707
                static::valueToString($value)
708
            ));
709
        }
710 8
    }
711
712 8
    public static function classExists($value, $message = '')
713 4
    {
714 4
        if (!class_exists($value)) {
715 4
            throw static::createInvalidArgumentException(sprintf(
716
                $message ?: 'Expected an existing class name. Got: %s',
717
                static::valueToString($value)
718 4
            ));
719
        }
720 8
    }
721
722 8 View Code Duplication
    public static function subclassOf($value, $class, $message = '')
723 4
    {
724 4
        if (!is_subclass_of($value, $class)) {
725 4
            throw static::createInvalidArgumentException(sprintf(
726 4
                $message ?: 'Expected a sub-class of %2$s. Got: %s',
727
                static::valueToString($value),
728
                static::valueToString($class)
729 4
            ));
730
        }
731 8
    }
732
733 8 View Code Duplication
    public static function implementsInterface($value, $interface, $message = '')
734 4
    {
735 4
        if (!in_array($interface, class_implements($value))) {
736 4
            throw static::createInvalidArgumentException(sprintf(
737 4
                $message ?: 'Expected an implementation of %2$s. Got: %s',
738
                static::valueToString($value),
739
                static::valueToString($interface)
740 4
            ));
741
        }
742 12
    }
743
744 12 View Code Duplication
    public static function propertyExists($classOrObject, $property, $message = '')
745 4
    {
746 4
        if (!property_exists($classOrObject, $property)) {
747 4
            throw static::createInvalidArgumentException(sprintf(
748
                $message ?: 'Expected the property %s to exist.',
749
                static::valueToString($property)
750 8
            ));
751
        }
752 12
    }
753
754 12 View Code Duplication
    public static function propertyNotExists($classOrObject, $property, $message = '')
755 8
    {
756 8
        if (property_exists($classOrObject, $property)) {
757 8
            throw static::createInvalidArgumentException(sprintf(
758
                $message ?: 'Expected the property %s to not exist.',
759
                static::valueToString($property)
760 4
            ));
761
        }
762 27
    }
763
764 27 View Code Duplication
    public static function methodExists($classOrObject, $method, $message = '')
765 19
    {
766 19
        if (!method_exists($classOrObject, $method)) {
767 19
            throw static::createInvalidArgumentException(sprintf(
768
                $message ?: 'Expected the method %s to exist.',
769
                static::valueToString($method)
770 8
            ));
771
        }
772 27
    }
773
774 27 View Code Duplication
    public static function methodNotExists($classOrObject, $method, $message = '')
775 8
    {
776 8
        if (method_exists($classOrObject, $method)) {
777 8
            throw static::createInvalidArgumentException(sprintf(
778
                $message ?: 'Expected the method %s to not exist.',
779
                static::valueToString($method)
780 19
            ));
781
        }
782 12
    }
783
784 12 View Code Duplication
    public static function keyExists($array, $key, $message = '')
785 4
    {
786 4
        if (!array_key_exists($key, $array)) {
787 4
            throw static::createInvalidArgumentException(sprintf(
788
                $message ?: 'Expected the key %s to exist.',
789
                static::valueToString($key)
790 8
            ));
791
        }
792 12
    }
793
794 12 View Code Duplication
    public static function keyNotExists($array, $key, $message = '')
795 8
    {
796 8
        if (array_key_exists($key, $array)) {
797 8
            throw static::createInvalidArgumentException(sprintf(
798
                $message ?: 'Expected the key %s to not exist.',
799
                static::valueToString($key)
800 4
            ));
801
        }
802 48
    }
803
804 48
    public static function count($array, $number, $message = '')
805
    {
806
        static::eq(
807
            count($array),
808 48
            $number,
809 4
            $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array))
810
        );
811
    }
812 44
813 20
    public static function uuid($value, $message = '')
814 20
    {
815 20
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
816
817
        // The nil UUID is special form of UUID that is specified to have all
818 24
        // 128 bits set to zero.
819
        if ('00000000-0000-0000-0000-000000000000' === $value) {
820 778
            return;
821
        }
822 778
823 298
        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)) {
824 230
            throw static::createInvalidArgumentException(sprintf(
825 230
                $message ?: 'Value "%s" is not a valid UUID.',
826
                static::valueToString($value)
827
            ));
828 182
        }
829
    }
830
831 480
    public static function __callStatic($name, $arguments)
832 480
    {
833
        if ('nullOr' === substr($name, 0, 6)) {
834 480
            if (null !== $arguments[0]) {
835 480
                $method = lcfirst(substr($name, 6));
836
                call_user_func_array(array('static', $method), $arguments);
837 480
            }
838 480
839
            return;
840 480
        }
841
842
        if ('all' === substr($name, 0, 3)) {
843 234
            static::isTraversable($arguments[0]);
844
845
            $method = lcfirst(substr($name, 3));
846
            $args = $arguments;
847
848
            foreach ($arguments[0] as $entry) {
849 368
                $args[0] = $entry;
850
851 368
                call_user_func_array(array('static', $method), $args);
852 11
            }
853
854
            return;
855 359
        }
856 15
857
        throw new BadMethodCallException('No such method: '.$name);
858
    }
859 349
860 13
    protected static function valueToString($value)
861
    {
862
        if (null === $value) {
863 336
            return 'null';
864 1
        }
865
866
        if (true === $value) {
867 335
            return 'true';
868 1
        }
869
870
        if (false === $value) {
871 334
            return 'false';
872 1
        }
873
874
        if (is_array($value)) {
875 334
            return 'array';
876 264
        }
877
878
        if (is_object($value)) {
879 78
            return get_class($value);
880
        }
881
882 129
        if (is_resource($value)) {
883
            return 'resource';
884 129
        }
885
886
        if (is_string($value)) {
887 124
            return '"'.$value.'"';
888
        }
889 124
890
        return (string) $value;
891
    }
892
893 124
    protected static function typeToString($value)
894
    {
895
        return is_object($value) ? get_class($value) : gettype($value);
896
    }
897 124
898
    protected static function strlen($value)
899
    {
900 500
        if (!function_exists('mb_detect_encoding')) {
901
            return strlen($value);
902 500
        }
903
904
        if (false === $encoding = mb_detect_encoding($value)) {
905
            return strlen($value);
906
        }
907
908
        return mb_strwidth($value, $encoding);
909
    }
910
911
    protected static function createInvalidArgumentException($message)
912
    {
913
        return new InvalidArgumentException($message);
914
    }
915
916
    private function __construct()
917
    {
918
    }
919
}
920