Failed Conditions
Pull Request — master (#18)
by Mateusz
03:02
created

Assert::file()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
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
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
    {
150 66
        if (!is_string($value)) {
151 14
            throw static::createInvalidArgumentException(sprintf(
152 14
                $message ?: 'Expected a string. Got: %s',
153 14
                static::typeToString($value)
154
            ));
155
        }
156 52
    }
157
158 12
    public static function stringNotEmpty($value, $message = '')
159
    {
160 12
        static::string($value, $message);
161 8
        static::notEmpty($value, $message);
162 4
    }
163
164 17 View Code Duplication
    public static function integer($value, $message = '')
165
    {
166 17
        if (!is_int($value)) {
167 13
            throw static::createInvalidArgumentException(sprintf(
168 13
                $message ?: 'Expected an integer. Got: %s',
169 13
                static::typeToString($value)
170
            ));
171
        }
172 4
    }
173
174 16 View Code Duplication
    public static function integerish($value, $message = '')
175
    {
176 16
        if (!is_numeric($value) || $value != (int) $value) {
177 4
            throw static::createInvalidArgumentException(sprintf(
178 4
                $message ?: 'Expected an integerish value. Got: %s',
179 4
                static::typeToString($value)
180
            ));
181
        }
182 12
    }
183
184 16 View Code Duplication
    public static function float($value, $message = '')
185
    {
186 16
        if (!is_float($value)) {
187 8
            throw static::createInvalidArgumentException(sprintf(
188 8
                $message ?: 'Expected a float. Got: %s',
189 8
                static::typeToString($value)
190
            ));
191
        }
192 8
    }
193
194 20 View Code Duplication
    public static function numeric($value, $message = '')
195
    {
196 20
        if (!is_numeric($value)) {
197 4
            throw static::createInvalidArgumentException(sprintf(
198 4
                $message ?: 'Expected a numeric. Got: %s',
199 4
                static::typeToString($value)
200
            ));
201
        }
202 16
    }
203
204 16 View Code Duplication
    public static function boolean($value, $message = '')
205
    {
206 16
        if (!is_bool($value)) {
207 8
            throw static::createInvalidArgumentException(sprintf(
208 8
                $message ?: 'Expected a boolean. Got: %s',
209 8
                static::typeToString($value)
210
            ));
211
        }
212 8
    }
213
214 23 View Code Duplication
    public static function scalar($value, $message = '')
215
    {
216 23
        if (!is_scalar($value)) {
217 11
            throw static::createInvalidArgumentException(sprintf(
218 11
                $message ?: 'Expected a scalar. Got: %s',
219 11
                static::typeToString($value)
220
            ));
221
        }
222 12
    }
223
224 23 View Code Duplication
    public static function object($value, $message = '')
225
    {
226 23
        if (!is_object($value)) {
227 15
            throw static::createInvalidArgumentException(sprintf(
228 15
                $message ?: 'Expected an object. Got: %s',
229 15
                static::typeToString($value)
230
            ));
231
        }
232 8
    }
233
234 16
    public static function resource($value, $type = null, $message = '')
235
    {
236 16
        if (!is_resource($value)) {
237 4
            throw static::createInvalidArgumentException(sprintf(
238 4
                $message ?: 'Expected a resource. Got: %s',
239 4
                static::typeToString($value)
240
            ));
241
        }
242
243 12
        if ($type && $type !== get_resource_type($value)) {
244 4
            throw static::createInvalidArgumentException(sprintf(
245 4
                $message ?: 'Expected a resource of type %2$s. Got: %s',
246 4
                static::typeToString($value),
247
                $type
248
            ));
249
        }
250 8
    }
251
252 20 View Code Duplication
    public static function isCallable($value, $message = '')
253
    {
254 20
        if (!is_callable($value)) {
255 8
            throw static::createInvalidArgumentException(sprintf(
256 8
                $message ?: 'Expected a callable. Got: %s',
257 8
                static::typeToString($value)
258
            ));
259
        }
260 12
    }
261
262 20 View Code Duplication
    public static function isArray($value, $message = '')
263
    {
264 20
        if (!is_array($value)) {
265 12
            throw static::createInvalidArgumentException(sprintf(
266 12
                $message ?: 'Expected an array. Got: %s',
267 12
                static::typeToString($value)
268
            ));
269
        }
270 8
    }
271
272 494
    public static function isTraversable($value, $message = '')
273
    {
274 494
        if (!is_array($value) && !($value instanceof Traversable)) {
275 8
            throw static::createInvalidArgumentException(sprintf(
276 8
                $message ?: 'Expected a traversable. Got: %s',
277 8
                static::typeToString($value)
278
            ));
279
        }
280 490
    }
281
282 16 View Code Duplication
    public static function isInstanceOf($value, $class, $message = '')
283
    {
284 16
        if (!($value instanceof $class)) {
285 12
            throw static::createInvalidArgumentException(sprintf(
286 12
                $message ?: 'Expected an instance of %2$s. Got: %s',
287 12
                static::typeToString($value),
288
                $class
289
            ));
290
        }
291 4
    }
292
293 16 View Code Duplication
    public static function notInstanceOf($value, $class, $message = '')
294
    {
295 16
        if ($value instanceof $class) {
296 4
            throw static::createInvalidArgumentException(sprintf(
297 4
                $message ?: 'Expected an instance other than %2$s. Got: %s',
298 4
                static::typeToString($value),
299
                $class
300
            ));
301
        }
302 12
    }
303
304 23
    public static function isEmpty($value, $message = '')
305
    {
306 23
        if (!empty($value)) {
307 8
            throw static::createInvalidArgumentException(sprintf(
308 8
                $message ?: 'Expected an empty value. Got: %s',
309 8
                static::valueToString($value)
310
            ));
311
        }
312 15
    }
313
314 31
    public static function notEmpty($value, $message = '')
315
    {
316 31
        if (empty($value)) {
317 19
            throw static::createInvalidArgumentException(sprintf(
318 19
                $message ?: 'Expected a non-empty value. Got: %s',
319 19
                static::valueToString($value)
320
            ));
321
        }
322 12
    }
323
324 11 View Code Duplication
    public static function null($value, $message = '')
325
    {
326 11
        if (null !== $value) {
327 8
            throw static::createInvalidArgumentException(sprintf(
328 8
                $message ?: 'Expected null. Got: %s',
329 8
                static::valueToString($value)
330
            ));
331
        }
332 3
    }
333
334 11
    public static function notNull($value, $message = '')
335
    {
336 11
        if (null === $value) {
337 3
            throw static::createInvalidArgumentException(
338 3
                $message ?: 'Expected a value other than null.'
339
            );
340
        }
341 8
    }
342
343 15 View Code Duplication
    public static function true($value, $message = '')
344
    {
345 15
        if (true !== $value) {
346 11
            throw static::createInvalidArgumentException(sprintf(
347 11
                $message ?: 'Expected a value to be true. Got: %s',
348 11
                static::valueToString($value)
349
            ));
350
        }
351 4
    }
352
353 19 View Code Duplication
    public static function false($value, $message = '')
354
    {
355 19
        if (false !== $value) {
356 15
            throw static::createInvalidArgumentException(sprintf(
357 15
                $message ?: 'Expected a value to be false. Got: %s',
358 15
                static::valueToString($value)
359
            ));
360
        }
361 4
    }
362
363 32
    public static function eq($value, $value2, $message = '')
364
    {
365 32
        if ($value2 != $value) {
366 16
            throw static::createInvalidArgumentException(sprintf(
367 16
                $message ?: 'Expected a value equal to %2$s. Got: %s',
368 16
                static::valueToString($value),
369 16
                static::valueToString($value2)
370
            ));
371
        }
372 16
    }
373
374 16
    public static function notEq($value, $value2, $message = '')
375
    {
376 16
        if ($value2 == $value) {
377 12
            throw static::createInvalidArgumentException(sprintf(
378 12
                $message ?: 'Expected a different value than %s.',
379 12
                static::valueToString($value2)
380
            ));
381
        }
382 4
    }
383
384 16
    public static function same($value, $value2, $message = '')
385
    {
386 16
        if ($value2 !== $value) {
387 12
            throw static::createInvalidArgumentException(sprintf(
388 12
                $message ?: 'Expected a value identical to %2$s. Got: %s',
389 12
                static::valueToString($value),
390 12
                static::valueToString($value2)
391
            ));
392
        }
393 4
    }
394
395 16
    public static function notSame($value, $value2, $message = '')
396
    {
397 16
        if ($value2 === $value) {
398 4
            throw static::createInvalidArgumentException(sprintf(
399 4
                $message ?: 'Expected a value not identical to %s.',
400 4
                static::valueToString($value2)
401
            ));
402
        }
403 12
    }
404
405 8
    public static function greaterThan($value, $limit, $message = '')
406
    {
407 8
        if ($value <= $limit) {
408 4
            throw static::createInvalidArgumentException(sprintf(
409 4
                $message ?: 'Expected a value greater than %2$s. Got: %s',
410 4
                static::valueToString($value),
411 4
                static::valueToString($limit)
412
            ));
413
        }
414 4
    }
415
416 12
    public static function greaterThanEq($value, $limit, $message = '')
417
    {
418 12
        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 4
                static::valueToString($value),
422 4
                static::valueToString($limit)
423
            ));
424
        }
425 8
    }
426
427 8
    public static function lessThan($value, $limit, $message = '')
428
    {
429 8
        if ($value >= $limit) {
430 4
            throw static::createInvalidArgumentException(sprintf(
431 4
                $message ?: 'Expected a value less than %2$s. Got: %s',
432 4
                static::valueToString($value),
433 4
                static::valueToString($limit)
434
            ));
435
        }
436 4
    }
437
438 12
    public static function lessThanEq($value, $limit, $message = '')
439
    {
440 12
        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 4
                static::valueToString($value),
444 4
                static::valueToString($limit)
445
            ));
446
        }
447 8
    }
448
449 16 View Code Duplication
    public static function range($value, $min, $max, $message = '')
450
    {
451 16
        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 8
                static::valueToString($min),
456 8
                static::valueToString($max)
457
            ));
458
        }
459 8
    }
460
461 8
    public static function oneOf($value, array $values, $message = '')
462
    {
463 8
        if (!in_array($value, $values, true)) {
464 4
            throw static::createInvalidArgumentException(sprintf(
465 4
                $message ?: 'Expected one of: %2$s. Got: %s',
466 4
                static::valueToString($value),
467 4
                implode(', ', array_map(array('static', 'valueToString'), $values))
468
            ));
469
        }
470 4
    }
471
472 20 View Code Duplication
    public static function contains($value, $subString, $message = '')
473
    {
474 20
        if (false === strpos($value, $subString)) {
475 8
            throw static::createInvalidArgumentException(sprintf(
476 8
                $message ?: 'Expected a value to contain %2$s. Got: %s',
477 8
                static::valueToString($value),
478 8
                static::valueToString($subString)
479
            ));
480
        }
481 12
    }
482
483 12 View Code Duplication
    public static function startsWith($value, $prefix, $message = '')
484
    {
485 12
        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 8
                static::valueToString($value),
489 8
                static::valueToString($prefix)
490
            ));
491
        }
492 4
    }
493
494 12
    public static function startsWithLetter($value, $message = '')
495
    {
496 12
        $valid = isset($value[0]);
497
498 12
        if ($valid) {
499 8
            $locale = setlocale(LC_CTYPE, 0);
500 8
            setlocale(LC_CTYPE, 'C');
501 8
            $valid = ctype_alpha($value[0]);
502 8
            setlocale(LC_CTYPE, $locale);
503
        }
504
505 12
        if (!$valid) {
506 8
            throw static::createInvalidArgumentException(sprintf(
507 8
                $message ?: 'Expected a value to start with a letter. Got: %s',
508 8
                static::valueToString($value)
509
            ));
510
        }
511 4
    }
512
513 12 View Code Duplication
    public static function endsWith($value, $suffix, $message = '')
514
    {
515 12
        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 8
                static::valueToString($value),
519 8
                static::valueToString($suffix)
520
            ));
521
        }
522 4
    }
523
524 12
    public static function regex($value, $pattern, $message = '')
525
    {
526 12
        if (!preg_match($pattern, $value)) {
527 8
            throw static::createInvalidArgumentException(sprintf(
528 8
                $message ?: 'The value %s does not match the expected pattern.',
529 8
                static::valueToString($value)
530
            ));
531
        }
532 4
    }
533
534 12 View Code Duplication
    public static function alpha($value, $message = '')
535
    {
536 12
        $locale = setlocale(LC_CTYPE, 0);
537 12
        setlocale(LC_CTYPE, 'C');
538 12
        $valid = !ctype_alpha($value);
539 12
        setlocale(LC_CTYPE, $locale);
540
541 12
        if ($valid) {
542 8
            throw static::createInvalidArgumentException(sprintf(
543 8
                $message ?: 'Expected a value to contain only letters. Got: %s',
544 8
                static::valueToString($value)
545
            ));
546
        }
547 4
    }
548
549 12 View Code Duplication
    public static function digits($value, $message = '')
550
    {
551 12
        $locale = setlocale(LC_CTYPE, 0);
552 12
        setlocale(LC_CTYPE, 'C');
553 12
        $valid = !ctype_digit($value);
554 12
        setlocale(LC_CTYPE, $locale);
555
556 12
        if ($valid) {
557 8
            throw static::createInvalidArgumentException(sprintf(
558 8
                $message ?: 'Expected a value to contain digits only. Got: %s',
559 8
                static::valueToString($value)
560
            ));
561
        }
562 4
    }
563
564 12 View Code Duplication
    public static function alnum($value, $message = '')
565
    {
566 12
        $locale = setlocale(LC_CTYPE, 0);
567 12
        setlocale(LC_CTYPE, 'C');
568 12
        $valid = !ctype_alnum($value);
569 12
        setlocale(LC_CTYPE, $locale);
570
571 12
        if ($valid) {
572 8
            throw static::createInvalidArgumentException(sprintf(
573 8
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
574 8
                static::valueToString($value)
575
            ));
576
        }
577 4
    }
578
579 16 View Code Duplication
    public static function lower($value, $message = '')
580
    {
581 16
        $locale = setlocale(LC_CTYPE, 0);
582 16
        setlocale(LC_CTYPE, 'C');
583 16
        $valid = !ctype_lower($value);
584 16
        setlocale(LC_CTYPE, $locale);
585
586 16
        if ($valid) {
587 12
            throw static::createInvalidArgumentException(sprintf(
588 12
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
589 12
                static::valueToString($value)
590
            ));
591
        }
592 4
    }
593
594 16 View Code Duplication
    public static function upper($value, $message = '')
595
    {
596 16
        $locale = setlocale(LC_CTYPE, 0);
597 16
        setlocale(LC_CTYPE, 'C');
598 16
        $valid = !ctype_upper($value);
599 16
        setlocale(LC_CTYPE, $locale);
600
601 16
        if ($valid) {
602 12
            throw static::createInvalidArgumentException(sprintf(
603 12
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
604 12
                static::valueToString($value)
605
            ));
606
        }
607 4
    }
608
609 24 View Code Duplication
    public static function length($value, $length, $message = '')
610
    {
611 24
        if ($length !== static::strlen($value)) {
612 16
            throw static::createInvalidArgumentException(sprintf(
613 16
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
614 16
                static::valueToString($value),
615
                $length
616
            ));
617
        }
618 8
    }
619
620 24 View Code Duplication
    public static function minLength($value, $min, $message = '')
621
    {
622 24
        if (static::strlen($value) < $min) {
623 8
            throw static::createInvalidArgumentException(sprintf(
624 8
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
625 8
                static::valueToString($value),
626
                $min
627
            ));
628
        }
629 16
    }
630
631 24 View Code Duplication
    public static function maxLength($value, $max, $message = '')
632
    {
633 24
        if (static::strlen($value) > $max) {
634 8
            throw static::createInvalidArgumentException(sprintf(
635 8
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
636 8
                static::valueToString($value),
637
                $max
638
            ));
639
        }
640 16
    }
641
642 40 View Code Duplication
    public static function lengthBetween($value, $min, $max, $message = '')
643
    {
644 40
        $length = static::strlen($value);
645
646 40
        if ($length < $min || $length > $max) {
647 16
            throw static::createInvalidArgumentException(sprintf(
648 16
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
649 16
                static::valueToString($value),
650
                $min,
651
                $max
652
            ));
653
        }
654 24
    }
655
656 36
    public static function fileExists($value, $message = '')
657
    {
658 36
        static::string($value);
659
660 36
        if (!file_exists($value)) {
661 12
            throw static::createInvalidArgumentException(sprintf(
662 12
                $message ?: 'The file %s does not exist.',
663 12
                static::valueToString($value)
664
            ));
665
        }
666 24
    }
667
668 12 View Code Duplication
    public static function file($value, $message = '')
669
    {
670 12
        static::fileExists($value, $message);
671
672 8
        if (!is_file($value)) {
673 4
            throw static::createInvalidArgumentException(sprintf(
674 4
                $message ?: 'The path %s is not a file.',
675 4
                static::valueToString($value)
676
            ));
677
        }
678 4
    }
679
680 12 View Code Duplication
    public static function directory($value, $message = '')
681
    {
682 12
        static::fileExists($value, $message);
683
684 8
        if (!is_dir($value)) {
685 4
            throw static::createInvalidArgumentException(sprintf(
686 4
                $message ?: 'The path %s is no directory.',
687 4
                static::valueToString($value)
688
            ));
689
        }
690 4
    }
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
    }
711
712 8
    public static function classExists($value, $message = '')
713
    {
714 8
        if (!class_exists($value)) {
715 4
            throw static::createInvalidArgumentException(sprintf(
716 4
                $message ?: 'Expected an existing class name. Got: %s',
717 4
                static::valueToString($value)
718
            ));
719
        }
720 4
    }
721
722 8 View Code Duplication
    public static function subclassOf($value, $class, $message = '')
723
    {
724 8
        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 4
                static::valueToString($value),
728 4
                static::valueToString($class)
729
            ));
730
        }
731 4
    }
732
733 8 View Code Duplication
    public static function implementsInterface($value, $interface, $message = '')
734
    {
735 8
        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 4
                static::valueToString($value),
739 4
                static::valueToString($interface)
740
            ));
741
        }
742 4
    }
743
744 12 View Code Duplication
    public static function propertyExists($classOrObject, $property, $message = '')
745
    {
746 12
        if (!property_exists($classOrObject, $property)) {
747 4
            throw static::createInvalidArgumentException(sprintf(
748 4
                $message ?: 'Expected the property %s to exist.',
749 4
                static::valueToString($property)
750
            ));
751
        }
752 8
    }
753
754 12 View Code Duplication
    public static function propertyNotExists($classOrObject, $property, $message = '')
755
    {
756 12
        if (property_exists($classOrObject, $property)) {
757 8
            throw static::createInvalidArgumentException(sprintf(
758 8
                $message ?: 'Expected the property %s to not exist.',
759 8
                static::valueToString($property)
760
            ));
761
        }
762 4
    }
763
764 27 View Code Duplication
    public static function methodExists($classOrObject, $method, $message = '')
765
    {
766 27
        if (!method_exists($classOrObject, $method)) {
767 19
            throw static::createInvalidArgumentException(sprintf(
768 19
                $message ?: 'Expected the method %s to exist.',
769 19
                static::valueToString($method)
770
            ));
771
        }
772 8
    }
773
774 27 View Code Duplication
    public static function methodNotExists($classOrObject, $method, $message = '')
775
    {
776 27
        if (method_exists($classOrObject, $method)) {
777 8
            throw static::createInvalidArgumentException(sprintf(
778 8
                $message ?: 'Expected the method %s to not exist.',
779 8
                static::valueToString($method)
780
            ));
781
        }
782 19
    }
783
784 12 View Code Duplication
    public static function keyExists($array, $key, $message = '')
785
    {
786 12
        if (!array_key_exists($key, $array)) {
787 4
            throw static::createInvalidArgumentException(sprintf(
788 4
                $message ?: 'Expected the key %s to exist.',
789 4
                static::valueToString($key)
790
            ));
791
        }
792 8
    }
793
794 12 View Code Duplication
    public static function keyNotExists($array, $key, $message = '')
795
    {
796 12
        if (array_key_exists($key, $array)) {
797 8
            throw static::createInvalidArgumentException(sprintf(
798 8
                $message ?: 'Expected the key %s to not exist.',
799 8
                static::valueToString($key)
800
            ));
801
        }
802 4
    }
803
804 8
    public static function count($array, $number, $message = '')
805
    {
806 8
        static::eq(
807
            count($array),
808
            $number,
809 8
            $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array))
810
        );
811 4
    }
812
813 48
    public static function uuid($value, $message = '')
814
    {
815 48
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
816
817
        // The nil UUID is special form of UUID that is specified to have all
818
        // 128 bits set to zero.
819 48
        if ('00000000-0000-0000-0000-000000000000' === $value) {
820 4
            return;
821
        }
822
823 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)) {
824 20
            throw static::createInvalidArgumentException(sprintf(
825 20
                $message ?: 'Value "%s" is not a valid UUID.',
826 20
                static::valueToString($value)
827
            ));
828
        }
829 24
    }
830
831 785
    public static function __callStatic($name, $arguments)
832
    {
833 785
        if ('nullOr' === substr($name, 0, 6)) {
834 301
            if (null !== $arguments[0]) {
835 232
                $method = lcfirst(substr($name, 6));
836 232
                call_user_func_array(array('static', $method), $arguments);
837
            }
838
839 184
            return;
840
        }
841
842 484
        if ('all' === substr($name, 0, 3)) {
843 484
            static::isTraversable($arguments[0]);
844
845 484
            $method = lcfirst(substr($name, 3));
846 484
            $args = $arguments;
847
848 484
            foreach ($arguments[0] as $entry) {
849 484
                $args[0] = $entry;
850
851 484
                call_user_func_array(array('static', $method), $args);
852
            }
853
854 236
            return;
855
        }
856
857
        throw new BadMethodCallException('No such method: '.$name);
858
    }
859
860 372
    protected static function valueToString($value)
861
    {
862 372
        if (null === $value) {
863 11
            return 'null';
864
        }
865
866 363
        if (true === $value) {
867 15
            return 'true';
868
        }
869
870 353
        if (false === $value) {
871 13
            return 'false';
872
        }
873
874 340
        if (is_array($value)) {
875 1
            return 'array';
876
        }
877
878 339
        if (is_object($value)) {
879 1
            return get_class($value);
880
        }
881
882 338
        if (is_resource($value)) {
883 1
            return 'resource';
884
        }
885
886 338
        if (is_string($value)) {
887 264
            return '"'.$value.'"';
888
        }
889
890 82
        return (string) $value;
891
    }
892
893 129
    protected static function typeToString($value)
894
    {
895 129
        return is_object($value) ? get_class($value) : gettype($value);
896
    }
897
898 124
    protected static function strlen($value)
899
    {
900 124
        if (!function_exists('mb_detect_encoding')) {
901
            return strlen($value);
902
        }
903
904 124
        if (false === $encoding = mb_detect_encoding($value)) {
905
            return strlen($value);
906
        }
907
908 124
        return mb_strwidth($value, $encoding);
909
    }
910
911 504
    protected static function createInvalidArgumentException($message)
912
    {
913 504
        return new InvalidArgumentException($message);
914
    }
915
916
    private function __construct()
917
    {
918
    }
919
}
920