Failed Conditions
Pull Request — master (#58)
by Baptiste
01:30
created

Assert::notRegex()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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