Failed Conditions
Pull Request — master (#28)
by Dariusz
19:47
created

src/Assert.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
use Throwable;
19
use Closure;
20
21
/**
22
 * Efficient assertions to validate the input/output of your methods.
23
 *
24
 * @method static void nullOrString($value, $message = '')
25
 * @method static void nullOrStringNotEmpty($value, $message = '')
26
 * @method static void nullOrInteger($value, $message = '')
27
 * @method static void nullOrIntegerish($value, $message = '')
28
 * @method static void nullOrFloat($value, $message = '')
29
 * @method static void nullOrNumeric($value, $message = '')
30
 * @method static void nullOrBoolean($value, $message = '')
31
 * @method static void nullOrScalar($value, $message = '')
32
 * @method static void nullOrObject($value, $message = '')
33
 * @method static void nullOrResource($value, $type = null, $message = '')
34
 * @method static void nullOrIsCallable($value, $message = '')
35
 * @method static void nullOrIsArray($value, $message = '')
36
 * @method static void nullOrIsTraversable($value, $message = '')
37
 * @method static void nullOrIsInstanceOf($value, $class, $message = '')
38
 * @method static void nullOrNotInstanceOf($value, $class, $message = '')
39
 * @method static void nullOrIsEmpty($value, $message = '')
40
 * @method static void nullOrNotEmpty($value, $message = '')
41
 * @method static void nullOrTrue($value, $message = '')
42
 * @method static void nullOrFalse($value, $message = '')
43
 * @method static void nullOrEq($value, $value2, $message = '')
44
 * @method static void nullOrNotEq($value,$value2,  $message = '')
45
 * @method static void nullOrSame($value, $value2, $message = '')
46
 * @method static void nullOrNotSame($value, $value2, $message = '')
47
 * @method static void nullOrGreaterThan($value, $value2, $message = '')
48
 * @method static void nullOrGreaterThanEq($value, $value2, $message = '')
49
 * @method static void nullOrLessThan($value, $value2, $message = '')
50
 * @method static void nullOrLessThanEq($value, $value2, $message = '')
51
 * @method static void nullOrRange($value, $min, $max, $message = '')
52
 * @method static void nullOrOneOf($value, $values, $message = '')
53
 * @method static void nullOrContains($value, $subString, $message = '')
54
 * @method static void nullOrStartsWith($value, $prefix, $message = '')
55
 * @method static void nullOrStartsWithLetter($value, $message = '')
56
 * @method static void nullOrEndsWith($value, $suffix, $message = '')
57
 * @method static void nullOrRegex($value, $pattern, $message = '')
58
 * @method static void nullOrAlpha($value, $message = '')
59
 * @method static void nullOrDigits($value, $message = '')
60
 * @method static void nullOrAlnum($value, $message = '')
61
 * @method static void nullOrLower($value, $message = '')
62
 * @method static void nullOrUpper($value, $message = '')
63
 * @method static void nullOrLength($value, $length, $message = '')
64
 * @method static void nullOrMinLength($value, $min, $message = '')
65
 * @method static void nullOrMaxLength($value, $max, $message = '')
66
 * @method static void nullOrLengthBetween($value, $min, $max, $message = '')
67
 * @method static void nullOrFileExists($value, $message = '')
68
 * @method static void nullOrFile($value, $message = '')
69
 * @method static void nullOrDirectory($value, $message = '')
70
 * @method static void nullOrReadable($value, $message = '')
71
 * @method static void nullOrWritable($value, $message = '')
72
 * @method static void nullOrClassExists($value, $message = '')
73
 * @method static void nullOrSubclassOf($value, $class, $message = '')
74
 * @method static void nullOrImplementsInterface($value, $interface, $message = '')
75
 * @method static void nullOrPropertyExists($value, $property, $message = '')
76
 * @method static void nullOrPropertyNotExists($value, $property, $message = '')
77
 * @method static void nullOrMethodExists($value, $method, $message = '')
78
 * @method static void nullOrMethodNotExists($value, $method, $message = '')
79
 * @method static void nullOrKeyExists($value, $key, $message = '')
80
 * @method static void nullOrKeyNotExists($value, $key, $message = '')
81
 * @method static void nullOrCount($value, $key, $message = '')
82
 * @method static void nullOrUuid($values, $message = '')
83
 * @method static void allString($values, $message = '')
84
 * @method static void allStringNotEmpty($values, $message = '')
85
 * @method static void allInteger($values, $message = '')
86
 * @method static void allIntegerish($values, $message = '')
87
 * @method static void allFloat($values, $message = '')
88
 * @method static void allNumeric($values, $message = '')
89
 * @method static void allBoolean($values, $message = '')
90
 * @method static void allScalar($values, $message = '')
91
 * @method static void allObject($values, $message = '')
92
 * @method static void allResource($values, $type = null, $message = '')
93
 * @method static void allIsCallable($values, $message = '')
94
 * @method static void allIsArray($values, $message = '')
95
 * @method static void allIsTraversable($values, $message = '')
96
 * @method static void allIsInstanceOf($values, $class, $message = '')
97
 * @method static void allNotInstanceOf($values, $class, $message = '')
98
 * @method static void allNull($values, $message = '')
99
 * @method static void allNotNull($values, $message = '')
100
 * @method static void allIsEmpty($values, $message = '')
101
 * @method static void allNotEmpty($values, $message = '')
102
 * @method static void allTrue($values, $message = '')
103
 * @method static void allFalse($values, $message = '')
104
 * @method static void allEq($values, $value2, $message = '')
105
 * @method static void allNotEq($values,$value2,  $message = '')
106
 * @method static void allSame($values, $value2, $message = '')
107
 * @method static void allNotSame($values, $value2, $message = '')
108
 * @method static void allGreaterThan($values, $value2, $message = '')
109
 * @method static void allGreaterThanEq($values, $value2, $message = '')
110
 * @method static void allLessThan($values, $value2, $message = '')
111
 * @method static void allLessThanEq($values, $value2, $message = '')
112
 * @method static void allRange($values, $min, $max, $message = '')
113
 * @method static void allOneOf($values, $values, $message = '')
114
 * @method static void allContains($values, $subString, $message = '')
115
 * @method static void allStartsWith($values, $prefix, $message = '')
116
 * @method static void allStartsWithLetter($values, $message = '')
117
 * @method static void allEndsWith($values, $suffix, $message = '')
118
 * @method static void allRegex($values, $pattern, $message = '')
119
 * @method static void allAlpha($values, $message = '')
120
 * @method static void allDigits($values, $message = '')
121
 * @method static void allAlnum($values, $message = '')
122
 * @method static void allLower($values, $message = '')
123
 * @method static void allUpper($values, $message = '')
124
 * @method static void allLength($values, $length, $message = '')
125
 * @method static void allMinLength($values, $min, $message = '')
126
 * @method static void allMaxLength($values, $max, $message = '')
127
 * @method static void allLengthBetween($values, $min, $max, $message = '')
128
 * @method static void allFileExists($values, $message = '')
129
 * @method static void allFile($values, $message = '')
130
 * @method static void allDirectory($values, $message = '')
131
 * @method static void allReadable($values, $message = '')
132
 * @method static void allWritable($values, $message = '')
133
 * @method static void allClassExists($values, $message = '')
134
 * @method static void allSubclassOf($values, $class, $message = '')
135
 * @method static void allImplementsInterface($values, $interface, $message = '')
136
 * @method static void allPropertyExists($values, $property, $message = '')
137
 * @method static void allPropertyNotExists($values, $property, $message = '')
138
 * @method static void allMethodExists($values, $method, $message = '')
139
 * @method static void allMethodNotExists($values, $method, $message = '')
140
 * @method static void allKeyExists($values, $key, $message = '')
141
 * @method static void allKeyNotExists($values, $key, $message = '')
142
 * @method static void allCount($values, $key, $message = '')
143
 * @method static void allUuid($values, $message = '')
144
 *
145
 * @since  1.0
146
 *
147
 * @author Bernhard Schussek <[email protected]>
148
 */
149
class Assert
150
{
151 90
    public static function string($value, $message = '')
152
    {
153 90
        if (!is_string($value)) {
154 14
            static::reportInvalidArgument(sprintf(
155 14
                $message ?: 'Expected a string. Got: %s',
156 14
                static::typeToString($value)
157
            ));
158
        }
159 76
    }
160
161 12
    public static function stringNotEmpty($value, $message = '')
162
    {
163 12
        static::string($value, $message);
164 8
        static::notEmpty($value, $message);
165 4
    }
166
167 17 View Code Duplication
    public static function integer($value, $message = '')
168
    {
169 17
        if (!is_int($value)) {
170 13
            static::reportInvalidArgument(sprintf(
171 13
                $message ?: 'Expected an integer. Got: %s',
172 13
                static::typeToString($value)
173
            ));
174
        }
175 4
    }
176
177 16 View Code Duplication
    public static function integerish($value, $message = '')
178
    {
179 16
        if (!is_numeric($value) || $value != (int) $value) {
180 4
            static::reportInvalidArgument(sprintf(
181 4
                $message ?: 'Expected an integerish value. Got: %s',
182 4
                static::typeToString($value)
183
            ));
184
        }
185 12
    }
186
187 16 View Code Duplication
    public static function float($value, $message = '')
188
    {
189 16
        if (!is_float($value)) {
190 8
            static::reportInvalidArgument(sprintf(
191 8
                $message ?: 'Expected a float. Got: %s',
192 8
                static::typeToString($value)
193
            ));
194
        }
195 8
    }
196
197 20 View Code Duplication
    public static function numeric($value, $message = '')
198
    {
199 20
        if (!is_numeric($value)) {
200 4
            static::reportInvalidArgument(sprintf(
201 4
                $message ?: 'Expected a numeric. Got: %s',
202 4
                static::typeToString($value)
203
            ));
204
        }
205 16
    }
206
207 16 View Code Duplication
    public static function boolean($value, $message = '')
208
    {
209 16
        if (!is_bool($value)) {
210 8
            static::reportInvalidArgument(sprintf(
211 8
                $message ?: 'Expected a boolean. Got: %s',
212 8
                static::typeToString($value)
213
            ));
214
        }
215 8
    }
216
217 23 View Code Duplication
    public static function scalar($value, $message = '')
218
    {
219 23
        if (!is_scalar($value)) {
220 11
            static::reportInvalidArgument(sprintf(
221 11
                $message ?: 'Expected a scalar. Got: %s',
222 11
                static::typeToString($value)
223
            ));
224
        }
225 12
    }
226
227 23 View Code Duplication
    public static function object($value, $message = '')
228
    {
229 23
        if (!is_object($value)) {
230 15
            static::reportInvalidArgument(sprintf(
231 15
                $message ?: 'Expected an object. Got: %s',
232 15
                static::typeToString($value)
233
            ));
234
        }
235 8
    }
236
237 16
    public static function resource($value, $type = null, $message = '')
238
    {
239 16
        if (!is_resource($value)) {
240 4
            static::reportInvalidArgument(sprintf(
241 4
                $message ?: 'Expected a resource. Got: %s',
242 4
                static::typeToString($value)
243
            ));
244
        }
245
246 12
        if ($type && $type !== get_resource_type($value)) {
247 4
            static::reportInvalidArgument(sprintf(
248 4
                $message ?: 'Expected a resource of type %2$s. Got: %s',
249 4
                static::typeToString($value),
250
                $type
251
            ));
252
        }
253 8
    }
254
255 20 View Code Duplication
    public static function isCallable($value, $message = '')
256
    {
257 20
        if (!is_callable($value)) {
258 8
            static::reportInvalidArgument(sprintf(
259 8
                $message ?: 'Expected a callable. Got: %s',
260 8
                static::typeToString($value)
261
            ));
262
        }
263 12
    }
264
265 20 View Code Duplication
    public static function isArray($value, $message = '')
266
    {
267 20
        if (!is_array($value)) {
268 12
            static::reportInvalidArgument(sprintf(
269 12
                $message ?: 'Expected an array. Got: %s',
270 12
                static::typeToString($value)
271
            ));
272
        }
273 8
    }
274
275 506
    public static function isTraversable($value, $message = '')
276
    {
277 506
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

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