Failed Conditions
Pull Request — master (#64)
by
unknown
18:15
created

Assert::valueToString()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

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