Failed Conditions
Pull Request — master (#28)
by Dariusz
03:22
created

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