Failed Conditions
Push — master ( 11ceec...da82da )
by Tobias
01:33
created

Assert::minCount()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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