Failed Conditions
Pull Request — master (#20)
by Florian
22:36 queued 20:29
created

Assert::throws()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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