Failed Conditions
Pull Request — master (#31)
by Dmitriy
01:54
created

Assert::minCount()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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