Failed Conditions
Push — master ( 14b391...031727 )
by Gert de
02:52
created

Assert::email()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1076 20
            static::reportInvalidArgument(
1077 20
                $message ?: 'Expected list - non-associative array.'
1078
            );
1079
        }
1080 4
    }
1081
1082 16
    public static function isMap($array, $message = '')
1083
    {
1084
        if (
1085 16
            !is_array($array) ||
1086 16
            !$array ||
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1087
            array_keys($array) !== array_filter(array_keys($array), function ($key) {
1088 12
                return is_string($key);
1089 16
            })
1090
        ) {
1091 12
            static::reportInvalidArgument(
1092 12
                $message ?: 'Expected map - associative array with string keys.'
1093
            );
1094
        }
1095 4
    }
1096
1097 56
    public static function uuid($value, $message = '')
1098
    {
1099 56
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
1100
1101
        // The nil UUID is special form of UUID that is specified to have all
1102
        // 128 bits set to zero.
1103 56
        if ('00000000-0000-0000-0000-000000000000' === $value) {
1104 4
            return;
1105
        }
1106
1107 52
        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)) {
1108 20
            static::reportInvalidArgument(sprintf(
1109 20
                $message ?: 'Value %s is not a valid UUID.',
1110 20
                static::valueToString($value)
1111
            ));
1112
        }
1113 32
    }
1114
1115 24
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
1116
    {
1117 24
        static::string($class);
1118
1119 24
        $actual = 'none';
1120
1121
        try {
1122 24
            $expression();
1123 24
        } catch (Exception $e) {
1124 20
            $actual = get_class($e);
1125 20
            if ($e instanceof $class) {
1126 20
                return;
1127
            }
1128 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...
1129 4
            $actual = get_class($e);
1130 4
            if ($e instanceof $class) {
1131 4
                return;
1132
            }
1133
        }
1134
1135 8
        static::reportInvalidArgument($message ?: sprintf(
1136 8
            'Expected to throw "%s", got "%s"',
1137
            $class,
1138
            $actual
1139
        ));
1140
    }
1141
1142 1383
    public static function __callStatic($name, $arguments)
1143
    {
1144 1383
        if ('nullOr' === substr($name, 0, 6)) {
1145 514
            if (null !== $arguments[0]) {
1146 421
                $method = lcfirst(substr($name, 6));
1147 421
                call_user_func_array(array('static', $method), $arguments);
1148
            }
1149
1150 296
            return;
1151
        }
1152
1153 869
        if ('all' === substr($name, 0, 3)) {
1154 868
            static::isIterable($arguments[0]);
1155
1156 868
            $method = lcfirst(substr($name, 3));
1157 868
            $args = $arguments;
1158
1159 868
            foreach ($arguments[0] as $entry) {
1160 868
                $args[0] = $entry;
1161
1162 868
                call_user_func_array(array('static', $method), $args);
1163
            }
1164
1165 412
            return;
1166
        }
1167
1168 1
        throw new BadMethodCallException('No such method: '.$name);
1169
    }
1170
1171 694
    protected static function valueToString($value)
1172
    {
1173 694
        if (null === $value) {
1174 20
            return 'null';
1175
        }
1176
1177 676
        if (true === $value) {
1178 15
            return 'true';
1179
        }
1180
1181 666
        if (false === $value) {
1182 25
            return 'false';
1183
        }
1184
1185 641
        if (is_array($value)) {
1186 13
            return 'array';
1187
        }
1188
1189 628
        if (is_object($value)) {
1190 2
            if (method_exists($value, '__toString')) {
1191 1
                return get_class($value).': '.self::valueToString($value->__toString());
1192
            }
1193
1194 1
            return get_class($value);
1195
        }
1196
1197 627
        if (is_resource($value)) {
1198 1
            return 'resource';
1199
        }
1200
1201 627
        if (is_string($value)) {
1202 525
            return '"'.$value.'"';
1203
        }
1204
1205 110
        return (string) $value;
1206
    }
1207
1208 169
    protected static function typeToString($value)
1209
    {
1210 169
        return is_object($value) ? get_class($value) : gettype($value);
1211
    }
1212
1213 168
    protected static function strlen($value)
1214
    {
1215 168
        if (!function_exists('mb_detect_encoding')) {
1216
            return strlen($value);
1217
        }
1218
1219 168
        if (false === $encoding = mb_detect_encoding($value)) {
1220
            return strlen($value);
1221
        }
1222
1223 168
        return mb_strlen($value, $encoding);
1224
    }
1225
1226 918
    protected static function reportInvalidArgument($message)
1227
    {
1228 918
        throw new InvalidArgumentException($message);
1229
    }
1230
1231
    private function __construct()
1232
    {
1233
    }
1234
}
1235