Failed Conditions
Pull Request — master (#14)
by
unknown
07:34
created

Assert::methodNotExists()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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