Completed
Push — master ( 373348...10583b )
by Bernhard
04:11
created

Assert::object()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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