Completed
Push — extending_assert ( 3ffee9 )
by Richard
02:11
created

Assertion::createException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 5
1
<?php
2
/**
3
 * Assert
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * If you did not receive a copy of the license and are unable to
10
 * obtain it through the world-wide-web, please send an email
11
 * to [email protected] so I can send you a copy immediately.
12
 */
13
14
namespace Assert;
15
16
use BadMethodCallException;
17
18
/**
19
 * Assert library
20
 *
21
 * @author Benjamin Eberlei <[email protected]>
22
 *
23
 * METHODSTART
24
 * @method static void nullOrEq($value, $value2, $message = null, $propertyPath = null)
25
 * @method static void nullOrSame($value, $value2, $message = null, $propertyPath = null)
26
 * @method static void nullOrNotEq($value1, $value2, $message = null, $propertyPath = null)
27
 * @method static void nullOrNotSame($value1, $value2, $message = null, $propertyPath = null)
28
 * @method static void nullOrInteger($value, $message = null, $propertyPath = null)
29
 * @method static void nullOrFloat($value, $message = null, $propertyPath = null)
30
 * @method static void nullOrDigit($value, $message = null, $propertyPath = null)
31
 * @method static void nullOrIntegerish($value, $message = null, $propertyPath = null)
32
 * @method static void nullOrBoolean($value, $message = null, $propertyPath = null)
33
 * @method static void nullOrScalar($value, $message = null, $propertyPath = null)
34
 * @method static void nullOrNotEmpty($value, $message = null, $propertyPath = null)
35
 * @method static void nullOrNoContent($value, $message = null, $propertyPath = null)
36
 * @method static void nullOrNotNull($value, $message = null, $propertyPath = null)
37
 * @method static void nullOrString($value, $message = null, $propertyPath = null)
38
 * @method static void nullOrRegex($value, $pattern, $message = null, $propertyPath = null)
39
 * @method static void nullOrLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8")
40
 * @method static void nullOrMinLength($value, $minLength, $message = null, $propertyPath = null, $encoding = "utf8")
41
 * @method static void nullOrMaxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
42
 * @method static void nullOrBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
43
 * @method static void nullOrStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
44
 * @method static void nullOrEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
45
 * @method static void nullOrContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
46
 * @method static void nullOrChoice($value, $choices, $message = null, $propertyPath = null)
47
 * @method static void nullOrInArray($value, $choices, $message = null, $propertyPath = null)
48
 * @method static void nullOrNumeric($value, $message = null, $propertyPath = null)
49
 * @method static void nullOrIsArray($value, $message = null, $propertyPath = null)
50
 * @method static void nullOrIsTraversable($value, $message = null, $propertyPath = null)
51
 * @method static void nullOrIsArrayAccessible($value, $message = null, $propertyPath = null)
52
 * @method static void nullOrKeyExists($value, $key, $message = null, $propertyPath = null)
53
 * @method static void nullOrKeyIsset($value, $key, $message = null, $propertyPath = null)
54
 * @method static void nullOrNotEmptyKey($value, $key, $message = null, $propertyPath = null)
55
 * @method static void nullOrNotBlank($value, $message = null, $propertyPath = null)
56
 * @method static void nullOrIsInstanceOf($value, $className, $message = null, $propertyPath = null)
57
 * @method static void nullOrNotIsInstanceOf($value, $className, $message = null, $propertyPath = null)
58
 * @method static void nullOrSubclassOf($value, $className, $message = null, $propertyPath = null)
59
 * @method static void nullOrRange($value, $minValue, $maxValue, $message = null, $propertyPath = null)
60
 * @method static void nullOrMin($value, $minValue, $message = null, $propertyPath = null)
61
 * @method static void nullOrMax($value, $maxValue, $message = null, $propertyPath = null)
62
 * @method static void nullOrFile($value, $message = null, $propertyPath = null)
63
 * @method static void nullOrDirectory($value, $message = null, $propertyPath = null)
64
 * @method static void nullOrReadable($value, $message = null, $propertyPath = null)
65
 * @method static void nullOrWriteable($value, $message = null, $propertyPath = null)
66
 * @method static void nullOrEmail($value, $message = null, $propertyPath = null)
67
 * @method static void nullOrUrl($value, $message = null, $propertyPath = null)
68
 * @method static void nullOrAlnum($value, $message = null, $propertyPath = null)
69
 * @method static void nullOrTrue($value, $message = null, $propertyPath = null)
70
 * @method static void nullOrFalse($value, $message = null, $propertyPath = null)
71
 * @method static void nullOrClassExists($value, $message = null, $propertyPath = null)
72
 * @method static void nullOrImplementsInterface($class, $interfaceName, $message = null, $propertyPath = null)
73
 * @method static void nullOrIsJsonString($value, $message = null, $propertyPath = null)
74
 * @method static void nullOrUuid($value, $message = null, $propertyPath = null)
75
 * @method static void nullOrCount($countable, $count, $message = null, $propertyPath = null)
76
 * @method static void nullOrChoicesNotEmpty($values, $choices, $message = null, $propertyPath = null)
77
 * @method static void nullOrMethodExists($value, $object, $message = null, $propertyPath = null)
78
 * @method static void nullOrIsObject($value, $message = null, $propertyPath = null)
79
 * @method static void nullOrLessThan($value, $limit, $message = null, $propertyPath = null)
80
 * @method static void nullOrLessOrEqualThan($value, $limit, $message = null, $propertyPath = null)
81
 * @method static void nullOrGreaterThan($value, $limit, $message = null, $propertyPath = null)
82
 * @method static void nullOrGreaterOrEqualThan($value, $limit, $message = null, $propertyPath = null)
83
 * @method static void nullOrDate($value, $format, $message = null, $propertyPath = null)
84
 * @method static void allEq($value, $value2, $message = null, $propertyPath = null)
85
 * @method static void allSame($value, $value2, $message = null, $propertyPath = null)
86
 * @method static void allNotEq($value1, $value2, $message = null, $propertyPath = null)
87
 * @method static void allNotSame($value1, $value2, $message = null, $propertyPath = null)
88
 * @method static void allInteger($value, $message = null, $propertyPath = null)
89
 * @method static void allFloat($value, $message = null, $propertyPath = null)
90
 * @method static void allDigit($value, $message = null, $propertyPath = null)
91
 * @method static void allIntegerish($value, $message = null, $propertyPath = null)
92
 * @method static void allBoolean($value, $message = null, $propertyPath = null)
93
 * @method static void allScalar($value, $message = null, $propertyPath = null)
94
 * @method static void allNotEmpty($value, $message = null, $propertyPath = null)
95
 * @method static void allNoContent($value, $message = null, $propertyPath = null)
96
 * @method static void allNotNull($value, $message = null, $propertyPath = null)
97
 * @method static void allString($value, $message = null, $propertyPath = null)
98
 * @method static void allRegex($value, $pattern, $message = null, $propertyPath = null)
99
 * @method static void allLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8")
100
 * @method static void allMinLength($value, $minLength, $message = null, $propertyPath = null, $encoding = "utf8")
101
 * @method static void allMaxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
102
 * @method static void allBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
103
 * @method static void allStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
104
 * @method static void allEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
105
 * @method static void allContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
106
 * @method static void allChoice($value, $choices, $message = null, $propertyPath = null)
107
 * @method static void allInArray($value, $choices, $message = null, $propertyPath = null)
108
 * @method static void allNumeric($value, $message = null, $propertyPath = null)
109
 * @method static void allIsArray($value, $message = null, $propertyPath = null)
110
 * @method static void allIsTraversable($value, $message = null, $propertyPath = null)
111
 * @method static void allIsArrayAccessible($value, $message = null, $propertyPath = null)
112
 * @method static void allKeyExists($value, $key, $message = null, $propertyPath = null)
113
 * @method static void allKeyIsset($value, $key, $message = null, $propertyPath = null)
114
 * @method static void allNotEmptyKey($value, $key, $message = null, $propertyPath = null)
115
 * @method static void allNotBlank($value, $message = null, $propertyPath = null)
116
 * @method static void allIsInstanceOf($value, $className, $message = null, $propertyPath = null)
117
 * @method static void allNotIsInstanceOf($value, $className, $message = null, $propertyPath = null)
118
 * @method static void allSubclassOf($value, $className, $message = null, $propertyPath = null)
119
 * @method static void allRange($value, $minValue, $maxValue, $message = null, $propertyPath = null)
120
 * @method static void allMin($value, $minValue, $message = null, $propertyPath = null)
121
 * @method static void allMax($value, $maxValue, $message = null, $propertyPath = null)
122
 * @method static void allFile($value, $message = null, $propertyPath = null)
123
 * @method static void allDirectory($value, $message = null, $propertyPath = null)
124
 * @method static void allReadable($value, $message = null, $propertyPath = null)
125
 * @method static void allWriteable($value, $message = null, $propertyPath = null)
126
 * @method static void allEmail($value, $message = null, $propertyPath = null)
127
 * @method static void allUrl($value, $message = null, $propertyPath = null)
128
 * @method static void allAlnum($value, $message = null, $propertyPath = null)
129
 * @method static void allTrue($value, $message = null, $propertyPath = null)
130
 * @method static void allFalse($value, $message = null, $propertyPath = null)
131
 * @method static void allClassExists($value, $message = null, $propertyPath = null)
132
 * @method static void allImplementsInterface($class, $interfaceName, $message = null, $propertyPath = null)
133
 * @method static void allIsJsonString($value, $message = null, $propertyPath = null)
134
 * @method static void allUuid($value, $message = null, $propertyPath = null)
135
 * @method static void allCount($countable, $count, $message = null, $propertyPath = null)
136
 * @method static void allChoicesNotEmpty($values, $choices, $message = null, $propertyPath = null)
137
 * @method static void allMethodExists($value, $object, $message = null, $propertyPath = null)
138
 * @method static void allIsObject($value, $message = null, $propertyPath = null)
139
 * @method static void allLessThan($value, $limit, $message = null, $propertyPath = null)
140
 * @method static void allLessOrEqualThan($value, $limit, $message = null, $propertyPath = null)
141
 * @method static void allGreaterThan($value, $limit, $message = null, $propertyPath = null)
142
 * @method static void allGreaterOrEqualThan($value, $limit, $message = null, $propertyPath = null)
143
 * @method static void allDate($value, $format, $message = null, $propertyPath = null)
144
 * METHODEND
145
 */
146
class Assertion
147
{
148
    const INVALID_FLOAT             = 9;
149
    const INVALID_INTEGER           = 10;
150
    const INVALID_DIGIT             = 11;
151
    const INVALID_INTEGERISH        = 12;
152
    const INVALID_BOOLEAN           = 13;
153
    const VALUE_EMPTY               = 14;
154
    const VALUE_NULL                = 15;
155
    const INVALID_STRING            = 16;
156
    const INVALID_REGEX             = 17;
157
    const INVALID_MIN_LENGTH        = 18;
158
    const INVALID_MAX_LENGTH        = 19;
159
    const INVALID_STRING_START      = 20;
160
    const INVALID_STRING_CONTAINS   = 21;
161
    const INVALID_CHOICE            = 22;
162
    const INVALID_NUMERIC           = 23;
163
    const INVALID_ARRAY             = 24;
164
    const INVALID_KEY_EXISTS        = 26;
165
    const INVALID_NOT_BLANK         = 27;
166
    const INVALID_INSTANCE_OF       = 28;
167
    const INVALID_SUBCLASS_OF       = 29;
168
    const INVALID_RANGE             = 30;
169
    const INVALID_ALNUM             = 31;
170
    const INVALID_TRUE              = 32;
171
    const INVALID_EQ                = 33;
172
    const INVALID_SAME              = 34;
173
    const INVALID_MIN               = 35;
174
    const INVALID_MAX               = 36;
175
    const INVALID_LENGTH            = 37;
176
    const INVALID_FALSE             = 38;
177
    const INVALID_STRING_END        = 39;
178
    const INVALID_UUID              = 40;
179
    const INVALID_COUNT             = 41;
180
    const INVALID_NOT_EQ            = 42;
181
    const INVALID_NOT_SAME          = 43;
182
    const INVALID_TRAVERSABLE       = 44;
183
    const INVALID_ARRAY_ACCESSIBLE  = 45;
184
    const INVALID_KEY_ISSET         = 46;
185
    const INVALID_DIRECTORY         = 101;
186
    const INVALID_FILE              = 102;
187
    const INVALID_READABLE          = 103;
188
    const INVALID_WRITEABLE         = 104;
189
    const INVALID_CLASS             = 105;
190
    const INVALID_EMAIL             = 201;
191
    const INTERFACE_NOT_IMPLEMENTED = 202;
192
    const INVALID_URL               = 203;
193
    const INVALID_NOT_INSTANCE_OF   = 204;
194
    const VALUE_NOT_EMPTY           = 205;
195
    const INVALID_JSON_STRING       = 206;
196
    const INVALID_OBJECT            = 207;
197
    const INVALID_METHOD            = 208;
198
    const INVALID_SCALAR            = 209;
199
    const INVALID_LESS              = 210;
200
    const INVALID_LESS_OR_EQUAL     = 211;
201
    const INVALID_GREATER           = 212;
202
    const INVALID_GREATER_OR_EQUAL  = 212;
203
    const INVALID_DATE              = 213;
204
205
    /**
206
     * Exception to throw when an assertion failed.
207
     *
208
     * @var string
209
     */
210
    static protected $exceptionClass = 'Assert\InvalidArgumentException';
211
212
    /**
213
     * Assertion class name.
214
     *
215
     * @var string
216
     */
217
    static protected $assertionClass = 'Assert\Assertion';
218
219
    /**
220
     * AssertionChain class name.
221
     *
222
     * @var string
223
     */
224
    static protected $chainClass = 'Assert\AssertionChain';
225
226
    /**
227
     * Lazy Assertion class name.
228
     *
229
     * @var string
230
     */
231
    static protected $lazyClass = 'Assert\LazyAssertion';
232
233
    /**
234
     * @return string
235
     */
236
    public static function getAssertionClass()
237
    {
238
        return static::$assertionClass;
239
    }
240
241
    /**
242
     * @return string
243
     */
244
    public static function getChainClass()
245
    {
246
        return static::$chainClass;
247
    }
248
249
    /**
250
     * @return string
251
     */
252
    public static function getLazyClass()
253
    {
254
        return static::$lazyClass;
255
    }
256
257
    /**
258
     * Helper method that handles building the assertion failure exceptions.
259
     * They are returned from this method so that the stack trace still shows
260
     * the assertions method.
261
     */
262
    protected static function createException($value, $message, $code, $propertyPath, array $constraints = array())
263
    {
264
        $exceptionClass = static::$exceptionClass;
265
        return new $exceptionClass($message, $code, $propertyPath, $value, $constraints);
266
    }
267
268
    /**
269
     * Assert that two values are equal (using == ).
270
     *
271
     * @param mixed $value
272
     * @param mixed $value2
273
     * @param string|null $message
274
     * @param string|null $propertyPath
275
     * @return void
276
     * @throws \Assert\AssertionFailedException
277
     */
278
    public static function eq($value, $value2, $message = null, $propertyPath = null)
279
    {
280
        if ($value != $value2) {
281
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
282
                $message ?: 'Value "%s" does not equal expected value "%s".',
283
                self::stringify($value),
284
                self::stringify($value2)
285
            );
286
287
            throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, array('expected' => $value2));
288
        }
289
    }
290
291
    /**
292
     * Assert that two values are the same (using ===).
293
     *
294
     * @param mixed $value
295
     * @param mixed $value2
296
     * @param string|null $message
297
     * @param string|null $propertyPath
298
     * @return void
299
     * @throws \Assert\AssertionFailedException
300
     */
301
    public static function same($value, $value2, $message = null, $propertyPath = null)
302
    {
303
        if ($value !== $value2) {
304
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
305
                $message ?: 'Value "%s" is not the same as expected value "%s".',
306
                self::stringify($value),
307
                self::stringify($value2)
308
            );
309
310
            throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, array('expected' => $value2));
311
        }
312
    }
313
314
    /**
315
     * Assert that two values are not equal (using == ).
316
     *
317
     * @param mixed $value1
318
     * @param mixed $value2
319
     * @param string|null $message
320
     * @param string|null $propertyPath
321
     * @return void
322
     * @throws \Assert\AssertionFailedException
323
     */
324
    public static function notEq($value1, $value2, $message = null, $propertyPath = null)
325
    {
326
        if ($value1 == $value2) {
327
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
328
                $message ?: 'Value "%s" is equal to expected value "%s".',
329
                self::stringify($value1),
330
                self::stringify($value2)
331
            );
332
            throw static::createException($value1, $message,static::INVALID_NOT_EQ, $propertyPath, array('expected' => $value2));
333
        }
334
    }
335
336
    /**
337
     * Assert that two values are not the same (using === ).
338
     *
339
     * @param mixed $value1
340
     * @param mixed $value2
341
     * @param string|null $message
342
     * @param string|null $propertyPath
343
     * @return void
344
     * @throws \Assert\AssertionFailedException
345
     */
346
    public static function notSame($value1, $value2, $message = null, $propertyPath = null)
347
    {
348
        if ($value1 === $value2) {
349
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
350
                $message ?: 'Value "%s" is the same as expected value "%s".',
351
                self::stringify($value1),
352
                self::stringify($value2)
353
            );
354
            throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, array('expected' => $value2));
355
        }
356
    }
357
358
    /**
359
     * Assert that value is a php integer.
360
     *
361
     * @param mixed $value
362
     * @param string|null $message
363
     * @param string|null $propertyPath
364
     * @return void
365
     * @throws \Assert\AssertionFailedException
366
     */
367
    public static function integer($value, $message = null, $propertyPath = null)
368
    {
369
        if ( ! is_int($value)) {
370
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
371
                $message ?: 'Value "%s" is not an integer.',
372
                self::stringify($value)
373
            );
374
375
            throw static::createException($value, $message, static::INVALID_INTEGER, $propertyPath);
376
        }
377
    }
378
379
    /**
380
     * Assert that value is a php float.
381
     *
382
     * @param mixed $value
383
     * @param string|null $message
384
     * @param string|null $propertyPath
385
     * @return void
386
     * @throws \Assert\AssertionFailedException
387
     */
388
    public static function float($value, $message = null, $propertyPath = null)
389
    {
390
        if ( ! is_float($value)) {
391
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
392
                $message ?: 'Value "%s" is not a float.',
393
                self::stringify($value)
394
            );
395
396
            throw static::createException($value, $message, static::INVALID_FLOAT, $propertyPath);
397
        }
398
    }
399
400
    /**
401
     * Validates if an integer or integerish is a digit.
402
     *
403
     * @param mixed $value
404
     * @param string|null $message
405
     * @param string|null $propertyPath
406
     * @return void
407
     * @throws \Assert\AssertionFailedException
408
     */
409
    public static function digit($value, $message = null, $propertyPath = null)
410
    {
411
        if ( ! ctype_digit((string)$value)) {
412
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
413
                $message ?: 'Value "%s" is not a digit.',
414
                self::stringify($value)
415
            );
416
417
            throw static::createException($value, $message, static::INVALID_DIGIT, $propertyPath);
418
        }
419
    }
420
421
    /**
422
     * Assert that value is a php integer'ish.
423
     * @param mixed $value
424
     * @param string|null $message
425
     * @param string|null $propertyPath
426
     * @return void
427
     * @throws \Assert\AssertionFailedException
428
     */
429
    public static function integerish($value, $message = null, $propertyPath = null)
430
    {
431
        if (is_object($value) || strval(intval($value)) != $value || is_bool($value) || is_null($value)) {
432
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
433
                $message ?: 'Value "%s" is not an integer or a number castable to integer.',
434
                self::stringify($value)
435
            );
436
437
            throw static::createException($value, $message, static::INVALID_INTEGERISH, $propertyPath);
438
        }
439
    }
440
441
    /**
442
     * Assert that value is php boolean
443
     *
444
     * @param mixed $value
445
     * @param string|null $message
446
     * @param string|null $propertyPath
447
     * @return void
448
     * @throws \Assert\AssertionFailedException
449
     */
450
    public static function boolean($value, $message = null, $propertyPath = null)
451
    {
452
        if ( ! is_bool($value)) {
453
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
454
                $message ?: 'Value "%s" is not a boolean.',
455
                self::stringify($value)
456
            );
457
458
            throw static::createException($value, $message, static::INVALID_BOOLEAN, $propertyPath);
459
        }
460
    }
461
462
    /**
463
     * Assert that value is a PHP scalar
464
     *
465
     * @param mixed $value
466
     * @param string|null $message
467
     * @param string|null $propertyPath
468
     * @return void
469
     * @throws \Assert\AssertionFailedException
470
     */
471
    public static function scalar($value, $message = null, $propertyPath = null)
472
    {
473
        if (!is_scalar($value)) {
474
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
475
                $message ?: 'Value "%s" is not a scalar.',
476
                self::stringify($value)
477
            );
478
479
            throw static::createException($value, $message, static::INVALID_SCALAR, $propertyPath);
480
        }
481
    }
482
483
    /**
484
     * Assert that value is not empty
485
     *
486
     * @param mixed $value
487
     * @param string|null $message
488
     * @param string|null $propertyPath
489
     * @return void
490
     * @throws \Assert\AssertionFailedException
491
     */
492
    public static function notEmpty($value, $message = null, $propertyPath = null)
493
    {
494
        if (empty($value)) {
495
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
496
                $message ?: 'Value "%s" is empty, but non empty value was expected.',
497
                self::stringify($value)
498
            );
499
500
            throw static::createException($value, $message, static::VALUE_EMPTY, $propertyPath);
501
        }
502
    }
503
504
    /**
505
     * Assert that value is empty
506
     *
507
     * @param mixed $value
508
     * @param string|null $message
509
     * @param string|null $propertyPath
510
     * @return void
511
     * @throws \Assert\AssertionFailedException
512
     */
513
    public static function noContent($value, $message = null, $propertyPath = null)
514
    {
515
        if (!empty($value)) {
516
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
517
                $message ?: 'Value "%s" is not empty, but empty value was expected.',
518
                self::stringify($value)
519
            );
520
521
            throw static::createException($value, $message, static::VALUE_NOT_EMPTY, $propertyPath);
522
        }
523
    }
524
525
    /**
526
     * Assert that value is not null
527
     *
528
     * @param mixed $value
529
     * @param string|null $message
530
     * @param string|null $propertyPath
531
     * @return void
532
     * @throws \Assert\AssertionFailedException
533
     */
534
    public static function notNull($value, $message = null, $propertyPath = null)
535
    {
536
        if ($value === null) {
537
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
538
                $message ?: 'Value "%s" is null, but non null value was expected.',
539
                self::stringify($value)
540
            );
541
542
            throw static::createException($value, $message, static::VALUE_NULL, $propertyPath);
543
        }
544
    }
545
546
    /**
547
     * Assert that value is a string
548
     *
549
     * @param mixed $value
550
     * @param string|null $message
551
     * @param string|null $propertyPath
552
     * @return void
553
     * @throws \Assert\AssertionFailedException
554
     */
555
    public static function string($value, $message = null, $propertyPath = null)
556
    {
557
        if ( ! is_string($value)) {
558
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
559
                $message ?: 'Value "%s" expected to be string, type %s given.',
560
                self::stringify($value),
561
                gettype($value)
562
            );
563
564
            throw static::createException($value, $message, static::INVALID_STRING, $propertyPath);
565
        }
566
    }
567
568
    /**
569
     * Assert that value matches a regex
570
     *
571
     * @param mixed $value
572
     * @param string $pattern
573
     * @param string|null $message
574
     * @param string|null $propertyPath
575
     * @return void
576
     * @throws \Assert\AssertionFailedException
577
     */
578
    public static function regex($value, $pattern, $message = null, $propertyPath = null)
579
    {
580
        static::string($value, $message, $propertyPath);
581
582
        if ( ! preg_match($pattern, $value)) {
583
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
584
                $message ?: 'Value "%s" does not match expression.',
585
                self::stringify($value)
586
            );
587
588
            throw static::createException($value, $message, static::INVALID_REGEX , $propertyPath, array('pattern' => $pattern));
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
589
        }
590
    }
591
592
    /**
593
     * Assert that string has a given length.
594
     *
595
     * @param mixed $value
596
     * @param int $length
597
     * @param string|null $message
598
     * @param string|null $propertyPath
599
     * @param string $encoding
600
     * @return void
601
     * @throws \Assert\AssertionFailedException
602
     */
603
    public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
604
    {
605
        static::string($value, $message, $propertyPath);
606
607
        if (mb_strlen($value, $encoding) !== $length) {
608
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
609
                $message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
610
                self::stringify($value),
611
                $length,
612
                mb_strlen($value, $encoding)
613
            );
614
615
            $constraints = array('length' => $length, 'encoding' => $encoding);
616
            throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
617
        }
618
    }
619
620
    /**
621
     * Assert that a string is at least $minLength chars long.
622
     *
623
     * @param mixed $value
624
     * @param int $minLength
625
     * @param string|null $message
626
     * @param string|null $propertyPath
627
     * @param string $encoding
628
     * @return void
629
     * @throws \Assert\AssertionFailedException
630
     */
631
    public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8')
632
    {
633
        static::string($value, $message, $propertyPath);
634
635
        if (mb_strlen($value, $encoding) < $minLength) {
636
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
637
                $message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
638
                self::stringify($value),
639
                $minLength,
640
                mb_strlen($value, $encoding)
641
            );
642
643
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
644
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
645
        }
646
    }
647
648
    /**
649
     * Assert that string value is not longer than $maxLength chars.
650
     *
651
     * @param mixed $value
652
     * @param integer $maxLength
653
     * @param string|null $message
654
     * @param string|null $propertyPath
655
     * @param string $encoding
656
     * @return void
657
     * @throws \Assert\AssertionFailedException
658
     */
659
    public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
660
    {
661
        static::string($value, $message, $propertyPath);
662
663
        if (mb_strlen($value, $encoding) > $maxLength) {
664
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
665
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
666
                self::stringify($value),
667
                $maxLength,
668
                mb_strlen($value, $encoding)
669
            );
670
671
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
672
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
673
        }
674
    }
675
676
    /**
677
     * Assert that string length is between min,max lengths.
678
     *
679
     * @param mixed $value
680
     * @param integer $minLength
681
     * @param integer $maxLength
682
     * @param string|null $message
683
     * @param string|null $propertyPath
684
     * @param string $encoding
685
     * @return void
686
     * @throws \Assert\AssertionFailedException
687
     */
688
    public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
689
    {
690
        static::string($value, $message, $propertyPath);
691
692
        if (mb_strlen($value, $encoding) < $minLength) {
693
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
694
                $message ?: 'Value "%s" is too short, it should have at least %d characters, but only has %d characters.',
695
                self::stringify($value),
696
                $minLength,
697
                mb_strlen($value, $encoding)
698
            );
699
700
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
701
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
702
        }
703
704
        if (mb_strlen($value, $encoding) > $maxLength) {
705
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
706
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
707
                self::stringify($value),
708
                $maxLength,
709
                mb_strlen($value, $encoding)
710
            );
711
712
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
713
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
714
        }
715
    }
716
717
    /**
718
     * Assert that string starts with a sequence of chars.
719
     *
720
     * @param mixed $string
721
     * @param string $needle
722
     * @param string|null $message
723
     * @param string|null $propertyPath
724
     * @param string $encoding
725
     * @return void
726
     * @throws \Assert\AssertionFailedException
727
     */
728
    public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
729
    {
730
        static::string($string, $message, $propertyPath);
731
732
        if (mb_strpos($string, $needle, null, $encoding) !== 0) {
733
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
734
                $message ?: 'Value "%s" does not start with "%s".',
735
                self::stringify($string),
736
                self::stringify($needle)
737
            );
738
739
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
740
            throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints);
741
        }
742
    }
743
744
    /**
745
     * Assert that string ends with a sequence of chars.
746
     *
747
     * @param mixed $string
748
     * @param string $needle
749
     * @param string|null $message
750
     * @param string|null $propertyPath
751
     * @param string $encoding
752
     * @return void
753
     * @throws \Assert\AssertionFailedException
754
     */
755
    public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
756
    {
757
        static::string($string, $message, $propertyPath);
758
759
        $stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding);
760
761
        if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) {
762
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
763
                $message ?: 'Value "%s" does not end with "%s".',
764
                self::stringify($string),
765
                self::stringify($needle)
766
            );
767
768
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
769
            throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints);
770
        }
771
    }
772
773
    /**
774
     * Assert that string contains a sequence of chars.
775
     *
776
     * @param mixed $string
777
     * @param string $needle
778
     * @param string|null $message
779
     * @param string|null $propertyPath
780
     * @param string $encoding
781
     * @return void
782
     * @throws \Assert\AssertionFailedException
783
     */
784
    public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
785
    {
786
        static::string($string, $message, $propertyPath);
787
788
        if (mb_strpos($string, $needle, null, $encoding) === false) {
789
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
790
                $message ?: 'Value "%s" does not contain "%s".',
791
                self::stringify($string),
792
                self::stringify($needle)
793
            );
794
795
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
796
            throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints);
797
        }
798
    }
799
800
    /**
801
     * Assert that value is in array of choices.
802
     *
803
     * @param mixed $value
804
     * @param array $choices
805
     * @param string|null $message
806
     * @param string|null $propertyPath
807
     * @return void
808
     * @throws \Assert\AssertionFailedException
809
     */
810
    public static function choice($value, array $choices, $message = null, $propertyPath = null)
811
    {
812
        if ( ! in_array($value, $choices, true)) {
813
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
814
                $message ?: 'Value "%s" is not an element of the valid values: %s',
815
                self::stringify($value),
816
                implode(", ", array_map('Assert\Assertion::stringify', $choices))
817
            );
818
819
            throw static::createException($value, $message, static::INVALID_CHOICE, $propertyPath, array('choices' => $choices));
820
        }
821
    }
822
823
    /**
824
     * Alias of {@see choice()}
825
     *
826
     * @throws \Assert\AssertionFailedException
827
     */
828
    public static function inArray($value, array $choices, $message = null, $propertyPath = null)
829
    {
830
        static::choice($value, $choices, $message, $propertyPath);
831
    }
832
833
    /**
834
     * Assert that value is numeric.
835
     *
836
     * @param mixed $value
837
     * @param string|null $message
838
     * @param string|null $propertyPath
839
     * @return void
840
     * @throws \Assert\AssertionFailedException
841
     */
842
    public static function numeric($value, $message = null, $propertyPath = null)
843
    {
844
        if ( ! is_numeric($value)) {
845
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
846
                $message ?: 'Value "%s" is not numeric.',
847
                self::stringify($value)
848
            );
849
850
            throw static::createException($value, $message, static::INVALID_NUMERIC, $propertyPath);
851
        }
852
    }
853
854
    /**
855
     * Assert that value is an array.
856
     *
857
     * @param mixed $value
858
     * @param string|null $message
859
     * @param string|null $propertyPath
860
     * @return void
861
     * @throws \Assert\AssertionFailedException
862
     */
863
    public static function isArray($value, $message = null, $propertyPath = null)
864
    {
865
        if ( ! is_array($value)) {
866
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
867
                $message ?: 'Value "%s" is not an array.',
868
                self::stringify($value)
869
            );
870
871
            throw static::createException($value, $message, static::INVALID_ARRAY, $propertyPath);
872
        }
873
    }
874
875
    /**
876
     * Assert that value is an array or a traversable object.
877
     *
878
     * @param mixed $value
879
     * @param string|null $message
880
     * @param string|null $propertyPath
881
     * @return void
882
     * @throws \Assert\AssertionFailedException
883
     */
884
    public static function isTraversable($value, $message = null, $propertyPath = null)
885
    {
886
        if ( ! is_array($value) && ! $value instanceof \Traversable) {
887
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
888
                $message ?: 'Value "%s" is not an array and does not implement Traversable.',
889
                self::stringify($value)
890
            );
891
892
            throw static::createException($value, $message, static::INVALID_TRAVERSABLE, $propertyPath);
893
        }
894
    }
895
896
    /**
897
     * Assert that value is an array or an array-accessible object.
898
     *
899
     * @param mixed $value
900
     * @param string|null $message
901
     * @param string|null $propertyPath
902
     * @return void
903
     * @throws \Assert\AssertionFailedException
904
     */
905
    public static function isArrayAccessible($value, $message = null, $propertyPath = null)
906
    {
907
        if ( ! is_array($value) && ! $value instanceof \ArrayAccess) {
908
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
909
                $message ?: 'Value "%s" is not an array and does not implement ArrayAccess.',
910
                self::stringify($value)
911
            );
912
913
            throw static::createException($value, $message, static::INVALID_ARRAY_ACCESSIBLE, $propertyPath);
914
        }
915
    }
916
917
    /**
918
     * Assert that key exists in an array
919
     *
920
     * @param mixed $value
921
     * @param string|integer $key
922
     * @param string|null $message
923
     * @param string|null $propertyPath
924
     * @return void
925
     * @throws \Assert\AssertionFailedException
926
     */
927
    public static function keyExists($value, $key, $message = null, $propertyPath = null)
928
    {
929
        static::isArray($value, $message, $propertyPath);
930
931
        if ( ! array_key_exists($key, $value)) {
932
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
933
                $message ?: 'Array does not contain an element with key "%s"',
934
                self::stringify($key)
935
            );
936
937
            throw static::createException($value, $message, static::INVALID_KEY_EXISTS, $propertyPath, array('key' => $key));
938
        }
939
    }
940
941
    /**
942
     * Assert that key exists in an array/array-accessible object using isset()
943
     *
944
     * @param mixed $value
945
     * @param string|integer $key
946
     * @param string|null $message
947
     * @param string|null $propertyPath
948
     * @return void
949
     * @throws \Assert\AssertionFailedException
950
     */
951
    public static function keyIsset($value, $key, $message = null, $propertyPath = null)
952
    {
953
        static::isArrayAccessible($value, $message, $propertyPath);
954
955
        if ( ! isset($value[$key])) {
956
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
957
                $message ?: 'The element with key "%s" was not found',
958
                self::stringify($key)
959
            );
960
961
            throw static::createException($value, $message, static::INVALID_KEY_ISSET, $propertyPath, array('key' => $key));
962
        }
963
    }
964
965
    /**
966
     * Assert that key exists in an array/array-accessible object and it's value is not empty.
967
     *
968
     * @param mixed $value
969
     * @param string|integer $key
970
     * @param string|null $message
971
     * @param string|null $propertyPath
972
     * @return void
973
     * @throws \Assert\AssertionFailedException
974
     */
975
    public static function notEmptyKey($value, $key, $message = null, $propertyPath = null)
976
    {
977
        static::keyIsset($value, $key, $message, $propertyPath);
978
        static::notEmpty($value[$key], $message, $propertyPath);
979
    }
980
981
    /**
982
     * Assert that value is not blank
983
     *
984
     * @param mixed $value
985
     * @param string|null $message
986
     * @param string|null $propertyPath
987
     * @return void
988
     * @throws \Assert\AssertionFailedException
989
     */
990
    public static function notBlank($value, $message = null, $propertyPath = null)
991
    {
992
        if (false === $value || (empty($value) && '0' != $value) || (is_string($value) && '' === trim($value))) {
993
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
994
                $message ?: 'Value "%s" is blank, but was expected to contain a value.',
995
                self::stringify($value)
996
            );
997
998
            throw static::createException($value, $message, static::INVALID_NOT_BLANK, $propertyPath);
999
        }
1000
    }
1001
1002
    /**
1003
     * Assert that value is instance of given class-name.
1004
     *
1005
     * @param mixed $value
1006
     * @param string $className
1007
     * @param string|null $message
1008
     * @param string|null $propertyPath
1009
     * @return void
1010
     * @throws \Assert\AssertionFailedException
1011
     */
1012
    public static function isInstanceOf($value, $className, $message = null, $propertyPath = null)
1013
    {
1014
        if ( ! ($value instanceof $className)) {
1015
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1016
                $message ?: 'Class "%s" was expected to be instanceof of "%s" but is not.',
1017
                self::stringify($value),
1018
                $className
1019
            );
1020
1021
            throw static::createException($value, $message, static::INVALID_INSTANCE_OF, $propertyPath, array('class' => $className));
1022
        }
1023
    }
1024
1025
    /**
1026
     * Assert that value is not instance of given class-name.
1027
     *
1028
     * @param mixed $value
1029
     * @param string $className
1030
     * @param string|null $message
1031
     * @param string|null $propertyPath
1032
     * @return void
1033
     * @throws \Assert\AssertionFailedException
1034
     */
1035
    public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null)
1036
    {
1037
        if ($value instanceof $className) {
1038
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1039
                $message ?: 'Class "%s" was not expected to be instanceof of "%s".',
1040
                self::stringify($value),
1041
                $className
1042
            );
1043
1044
            throw static::createException($value, $message, static::INVALID_NOT_INSTANCE_OF, $propertyPath, array('class' => $className));
1045
        }
1046
    }
1047
1048
    /**
1049
     * Assert that value is subclass of given class-name.
1050
     *
1051
     * @param mixed $value
1052
     * @param string $className
1053
     * @param string|null $message
1054
     * @param string|null $propertyPath
1055
     * @return void
1056
     * @throws \Assert\AssertionFailedException
1057
     */
1058
    public static function subclassOf($value, $className, $message = null, $propertyPath = null)
1059
    {
1060
        if ( ! is_subclass_of($value, $className)) {
1061
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1062
                $message ?: 'Class "%s" was expected to be subclass of "%s".',
1063
                self::stringify($value),
1064
                $className
1065
            );
1066
1067
            throw static::createException($value, $message, static::INVALID_SUBCLASS_OF, $propertyPath, array('class' => $className));
1068
        }
1069
    }
1070
1071
    /**
1072
     * Assert that value is in range of numbers.
1073
     *
1074
     * @param mixed $value
1075
     * @param integer $minValue
1076
     * @param integer $maxValue
1077
     * @param string|null $message
1078
     * @param string|null $propertyPath
1079
     * @return void
1080
     * @throws \Assert\AssertionFailedException
1081
     */
1082
    public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null)
1083
    {
1084
        static::numeric($value, $message, $propertyPath);
1085
1086
        if ($value < $minValue || $value > $maxValue) {
1087
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1088
                $message ?: 'Number "%s" was expected to be at least "%d" and at most "%d".',
1089
                self::stringify($value),
1090
                self::stringify($minValue),
1091
                self::stringify($maxValue)
1092
            );
1093
1094
            throw static::createException($value, $message, static::INVALID_RANGE, $propertyPath, array('min' => $minValue, 'max' => $maxValue));
1095
        }
1096
    }
1097
1098
    /**
1099
     * Assert that a value is at least as big as a given limit
1100
     *
1101
     * @param mixed $value
1102
     * @param mixed $minValue
1103
     * @param string|null $message
1104
     * @param string|null $propertyPath
1105
     * @return void
1106
     * @throws \Assert\AssertionFailedException
1107
     */
1108
    public static function min($value, $minValue, $message = null, $propertyPath = null)
1109
    {
1110
        static::numeric($value, $message, $propertyPath);
1111
1112
        if ($value < $minValue) {
1113
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1114
                $message ?: 'Number "%s" was expected to be at least "%d".',
1115
                self::stringify($value),
1116
                self::stringify($minValue)
1117
            );
1118
1119
            throw static::createException($value, $message, static::INVALID_MIN, $propertyPath, array('min' => $minValue));
1120
        }
1121
    }
1122
1123
    /**
1124
     * Assert that a number is smaller as a given limit
1125
     *
1126
     * @param mixed $value
1127
     * @param mixed $maxValue
1128
     * @param string|null $message
1129
     * @param string|null $propertyPath
1130
     * @return void
1131
     * @throws \Assert\AssertionFailedException
1132
     */
1133
    public static function max($value, $maxValue, $message = null, $propertyPath = null)
1134
    {
1135
        static::numeric($value, $message, $propertyPath);
1136
1137
        if ($value > $maxValue) {
1138
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1139
                $message ?: 'Number "%s" was expected to be at most "%d".',
1140
                self::stringify($value),
1141
                self::stringify($maxValue)
1142
            );
1143
1144
            throw static::createException($value, $message, static::INVALID_MAX, $propertyPath, array('max' => $maxValue));
1145
        }
1146
    }
1147
1148
    /**
1149
     * Assert that a file exists
1150
     *
1151
     * @param string $value
1152
     * @param string|null $message
1153
     * @param string|null $propertyPath
1154
     * @return void
1155
     * @throws \Assert\AssertionFailedException
1156
     */
1157
    public static function file($value, $message = null, $propertyPath = null)
1158
    {
1159
        static::string($value, $message, $propertyPath);
1160
        static::notEmpty($value, $message, $propertyPath);
1161
1162
        if ( ! is_file($value)) {
1163
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1164
                $message ?: 'File "%s" was expected to exist.',
1165
                self::stringify($value)
1166
            );
1167
1168
            throw static::createException($value, $message, static::INVALID_FILE, $propertyPath);
1169
        }
1170
    }
1171
1172
    /**
1173
     * Assert that a directory exists
1174
     *
1175
     * @param string $value
1176
     * @param string|null $message
1177
     * @param string|null $propertyPath
1178
     * @return void
1179
     * @throws \Assert\AssertionFailedException
1180
     */
1181
    public static function directory($value, $message = null, $propertyPath = null)
1182
    {
1183
        static::string($value, $message, $propertyPath);
1184
1185
        if ( ! is_dir($value)) {
1186
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1187
                $message ?: 'Path "%s" was expected to be a directory.',
1188
                self::stringify($value)
1189
            );
1190
1191
            throw static::createException($value, $message, static::INVALID_DIRECTORY, $propertyPath);
1192
        }
1193
    }
1194
1195
    /**
1196
     * Assert that the value is something readable
1197
     *
1198
     * @param string $value
1199
     * @param string|null $message
1200
     * @param string|null $propertyPath
1201
     * @return void
1202
     * @throws \Assert\AssertionFailedException
1203
     */
1204
    public static function readable($value, $message = null, $propertyPath = null)
1205
    {
1206
        static::string($value, $message, $propertyPath);
1207
1208
        if ( ! is_readable($value)) {
1209
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1210
                $message ?: 'Path "%s" was expected to be readable.',
1211
                self::stringify($value)
1212
            );
1213
1214
            throw static::createException($value, $message, static::INVALID_READABLE, $propertyPath);
1215
        }
1216
    }
1217
1218
    /**
1219
     * Assert that the value is something writeable
1220
     *
1221
     * @param string $value
1222
     * @param string|null $message
1223
     * @param string|null $propertyPath
1224
     * @return void
1225
     * @throws \Assert\AssertionFailedException
1226
     */
1227
    public static function writeable($value, $message = null, $propertyPath = null)
1228
    {
1229
        static::string($value, $message, $propertyPath);
1230
1231
        if ( ! is_writeable($value)) {
1232
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1233
                $message ?: 'Path "%s" was expected to be writeable.',
1234
                self::stringify($value)
1235
            );
1236
1237
            throw static::createException($value, $message, static::INVALID_WRITEABLE, $propertyPath);
1238
        }
1239
    }
1240
1241
    /**
1242
     * Assert that value is an email adress (using
1243
     * input_filter/FILTER_VALIDATE_EMAIL).
1244
     *
1245
     * @param mixed $value
1246
     * @param string|null $message
1247
     * @param string|null $propertyPath
1248
     * @return void
1249
     * @throws \Assert\AssertionFailedException
1250
     */
1251
    public static function email($value, $message = null, $propertyPath = null)
1252
    {
1253
        static::string($value, $message, $propertyPath);
1254
1255
        if ( ! filter_var($value, FILTER_VALIDATE_EMAIL)) {
1256
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1257
                $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1258
                self::stringify($value)
1259
            );
1260
1261
            throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1262
        } else {
1263
            $host = substr($value, strpos($value, '@') + 1);
1264
1265
            // Likely not a FQDN, bug in PHP FILTER_VALIDATE_EMAIL prior to PHP 5.3.3
1266
            if (version_compare(PHP_VERSION, '5.3.3', '<') && strpos($host, '.') === false) {
1267
                $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1268
                    $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1269
                    self::stringify($value)
1270
                );
1271
1272
                throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1273
            }
1274
        }
1275
    }
1276
1277
    /**
1278
     * Assert that value is an URL.
1279
     *
1280
     * This code snipped was taken from the Symfony project and modified to the special demands of this method.
1281
     *
1282
     * @param mixed $value
1283
     * @param string|null $message
1284
     * @param string|null $propertyPath
1285
     * @return void
1286
     * @throws \Assert\AssertionFailedException
1287
     *
1288
     *
1289
     * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php
1290
     * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php
1291
     */
1292
    public static function url($value, $message = null, $propertyPath = null)
1293
    {
1294
        static::string($value, $message, $propertyPath);
1295
1296
        $protocols = array('http', 'https');
1297
1298
        $pattern = '~^
1299
            (%s)://                                 # protocol
1300
            (
1301
                ([\pL\pN\pS-]+\.)+[\pL]+                   # a domain name
1302
                    |                                     #  or
1303
                \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}      # a IP address
1304
                    |                                     #  or
1305
                \[
1306
                    (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,2}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,3}(?:(?:[0-9a-f]{1,4})))?::(?:(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
1307
                \]  # a IPv6 address
1308
            )
1309
            (:[0-9]+)?                              # a port (optional)
1310
            (/?|/\S+)                               # a /, nothing or a / with something
1311
        $~ixu';
1312
1313
        $pattern = sprintf($pattern, implode('|', $protocols));
1314
1315
        if (!preg_match($pattern, $value)) {
1316
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1317
                $message ?: 'Value "%s" was expected to be a valid URL starting with http or https',
1318
                self::stringify($value)
1319
            );
1320
1321
            throw static::createException($value, $message, static::INVALID_URL, $propertyPath);
1322
        }
1323
1324
    }
1325
1326
    /**
1327
     * Assert that value is alphanumeric.
1328
     *
1329
     * @param mixed $value
1330
     * @param string|null $message
1331
     * @param string|null $propertyPath
1332
     * @return void
1333
     * @throws \Assert\AssertionFailedException
1334
     */
1335
    public static function alnum($value, $message = null, $propertyPath = null)
1336
    {
1337
        try {
1338
            static::regex($value, '(^([a-zA-Z]{1}[a-zA-Z0-9]*)$)', $message, $propertyPath);
1339
        } catch(AssertionFailedException $e) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after CATCH keyword; 0 found
Loading history...
1340
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1341
                $message ?: 'Value "%s" is not alphanumeric, starting with letters and containing only letters and numbers.',
1342
                self::stringify($value)
1343
            );
1344
1345
            throw static::createException($value, $message, static::INVALID_ALNUM, $propertyPath);
1346
        }
1347
    }
1348
1349
    /**
1350
     * Assert that the value is boolean True.
1351
     *
1352
     * @param mixed $value
1353
     * @param string|null $message
1354
     * @param string|null $propertyPath
1355
     * @return void
1356
     * @throws \Assert\AssertionFailedException
1357
     */
1358
    public static function true($value, $message = null, $propertyPath = null)
1359
    {
1360
        if ($value !== true) {
1361
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1362
                $message ?: 'Value "%s" is not TRUE.',
1363
                self::stringify($value)
1364
            );
1365
1366
            throw static::createException($value, $message, static::INVALID_TRUE, $propertyPath);
1367
        }
1368
    }
1369
1370
    /**
1371
     * Assert that the value is boolean False.
1372
     *
1373
     * @param mixed $value
1374
     * @param string|null $message
1375
     * @param string|null $propertyPath
1376
     * @return void
1377
     * @throws \Assert\AssertionFailedException
1378
     */
1379
    public static function false($value, $message = null, $propertyPath = null)
1380
    {
1381
        if ($value !== false) {
1382
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1383
                $message ?: 'Value "%s" is not FALSE.',
1384
                self::stringify($value)
1385
            );
1386
1387
            throw static::createException($value, $message, static::INVALID_FALSE, $propertyPath);
1388
        }
1389
    }
1390
1391
    /**
1392
     * Assert that the class exists.
1393
     *
1394
     * @param mixed $value
1395
     * @param string|null $message
1396
     * @param string|null $propertyPath
1397
     * @return void
1398
     * @throws \Assert\AssertionFailedException
1399
     */
1400
    public static function classExists($value, $message = null, $propertyPath = null)
1401
    {
1402
        if ( ! class_exists($value)) {
1403
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1404
                $message ?: 'Class "%s" does not exist.',
1405
                self::stringify($value)
1406
            );
1407
1408
            throw static::createException($value, $message, static::INVALID_CLASS, $propertyPath);
1409
        }
1410
    }
1411
1412
    /**
1413
     * Assert that the class implements the interface
1414
     *
1415
     * @param mixed $class
1416
     * @param string $interfaceName
1417
     * @param string|null $message
1418
     * @param string|null $propertyPath
1419
     * @return void
1420
     * @throws \Assert\AssertionFailedException
1421
     */
1422
    public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null)
1423
    {
1424
        $reflection = new \ReflectionClass($class);
1425
        if ( ! $reflection->implementsInterface($interfaceName)) {
1426
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1427
                $message ?: 'Class "%s" does not implement interface "%s".',
1428
                self::stringify($class),
1429
                self::stringify($interfaceName)
1430
            );
1431
1432
            throw static::createException($class, $message, static::INTERFACE_NOT_IMPLEMENTED, $propertyPath, array('interface' => $interfaceName));
1433
        }
1434
    }
1435
1436
    /**
1437
     * Assert that the given string is a valid json string.
1438
     *
1439
     * NOTICE:
1440
     * Since this does a json_decode to determine its validity
1441
     * you probably should consider, when using the variable
1442
     * content afterwards, just to decode and check for yourself instead
1443
     * of using this assertion.
1444
     *
1445
     * @param mixed $value
1446
     * @param string|null $message
1447
     * @param string|null $propertyPath
1448
     * @return void
1449
     * @throws \Assert\AssertionFailedException
1450
     */
1451
    public static function isJsonString($value, $message = null, $propertyPath = null)
1452
    {
1453
        if (null === json_decode($value) && JSON_ERROR_NONE !== json_last_error()) {
1454
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1455
                $message ?: 'Value "%s" is not a valid JSON string.',
1456
                self::stringify($value)
1457
            );
1458
1459
            throw static::createException($value, $message, static::INVALID_JSON_STRING, $propertyPath);
1460
        }
1461
    }
1462
1463
    /**
1464
     * Assert that the given string is a valid UUID
1465
     *
1466
     * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed.
1467
     *
1468
     * @param string $value
1469
     * @param string|null $message
1470
     * @param string|null $propertyPath
1471
     * @return void
1472
     * @throws \Assert\AssertionFailedException
1473
     */
1474
    public static function uuid($value, $message = null, $propertyPath = null)
1475
    {
1476
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $value. This often makes code more readable.
Loading history...
1477
1478
        if ($value === '00000000-0000-0000-0000-000000000000') {
1479
            return;
1480
        }
1481
1482
        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)) {
1483
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1484
                $message ?: 'Value "%s" is not a valid UUID.',
1485
                self::stringify($value)
1486
            );
1487
1488
            throw static::createException($value, $message, static::INVALID_UUID, $propertyPath);
1489
        }
1490
    }
1491
1492
    /**
1493
     * Assert that the count of countable is equal to count.
1494
     *
1495
     * @param array|\Countable $countable
1496
     * @param int              $count
1497
     * @param string           $message
0 ignored issues
show
Documentation introduced by
Should the type for parameter $message not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
1498
     * @param string           $propertyPath
0 ignored issues
show
Documentation introduced by
Should the type for parameter $propertyPath not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
1499
     * @return void
1500
     * @throws \Assert\AssertionFailedException
1501
     */
1502
    public static function count($countable, $count, $message = null, $propertyPath = null)
1503
    {
1504
        if ($count !== count($countable)) {
1505
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1506
                $message ?: 'List does not contain exactly "%d" elements.',
1507
                self::stringify($count)
1508
            );
1509
1510
            throw static::createException($countable, $message, static::INVALID_COUNT, $propertyPath, array('count' => $count));
1511
        }
1512
    }
1513
1514
    /**
1515
     * static call handler to implement:
1516
     *  - "null or assertion" delegation
1517
     *  - "all" delegation
1518
     */
1519
    public static function __callStatic($method, $args)
1520
    {
1521
        if (strpos($method, "nullOr") === 0) {
1522
            if ( ! array_key_exists(0, $args)) {
1523
                throw new BadMethodCallException("Missing the first argument.");
1524
            }
1525
1526
            if ($args[0] === null) {
1527
                return;
1528
            }
1529
1530
            $method = substr($method, 6);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $method. This often makes code more readable.
Loading history...
1531
1532
            return call_user_func_array(array(get_called_class(), $method), $args);
1533
        }
1534
1535
        if (strpos($method, "all") === 0) {
1536
            if ( ! array_key_exists(0, $args)) {
1537
                throw new BadMethodCallException("Missing the first argument.");
1538
            }
1539
1540
            static::isTraversable($args[0]);
1541
1542
            $method      = substr($method, 3);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $method. This often makes code more readable.
Loading history...
1543
            $values      = array_shift($args);
1544
            $calledClass = get_called_class();
1545
1546
            foreach ($values as $value) {
1547
                call_user_func_array(array($calledClass, $method), array_merge(array($value), $args));
1548
            }
1549
1550
            return;
1551
        }
1552
1553
        throw new BadMethodCallException("No assertion Assertion#" . $method . " exists.");
1554
    }
1555
1556
    /**
1557
     * Determines if the values array has every choice as key and that this choice has content.
1558
     *
1559
     * @param array $values
1560
     * @param array $choices
1561
     * @param null  $message
1562
     * @param null  $propertyPath
1563
     */
1564
    public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null)
1565
    {
1566
        self::notEmpty($values, $message, $propertyPath);
1567
1568
        foreach ($choices as $choice) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1569
1570
            self::notEmptyKey($values, $choice, $message, $propertyPath);
1571
        }
1572
    }
1573
1574
    /**
1575
     * Determines that the named method is defined in the provided object.
1576
     *
1577
     * @param string $value
1578
     * @param mixed  $object
1579
     * @param null   $message
1580
     * @param null   $propertyPath
1581
     *
1582
     * @throws
1583
     */
1584
    public static function methodExists($value, $object, $message = null, $propertyPath = null)
1585
    {
1586
        self::isObject($object, $message, $propertyPath);
1587
1588
        if (!method_exists($object, $value)) {
1589
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1590
                $message ?: 'Expected "%s" does not exist in provided object.',
1591
                self::stringify($value)
1592
            );
1593
1594
            throw static::createException($value, $message, static::INVALID_METHOD, $propertyPath);
1595
        }
1596
    }
1597
1598
    /**
1599
     * Determines that the provided value is an object.
1600
     *
1601
     * @param mixed $value
1602
     * @param null  $message
1603
     * @param null  $propertyPath
1604
     */
1605
    public static function isObject($value, $message = null, $propertyPath = null)
1606
    {
1607
        if (!is_object($value)) {
1608
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1609
                $message ?: 'Provided "%s" is not a valid object.',
1610
                self::stringify($value)
1611
            );
1612
1613
            throw static::createException($value, $message, static::INVALID_OBJECT, $propertyPath);
1614
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1615
        }
1616
    }
1617
1618
    /**
1619
     * Determines if the value is less than given limit.
1620
     *
1621
     * @param mixed $value
1622
     * @param mixed $limit
1623
     * @param null  $message
1624
     * @param null  $propertyPath
1625
     */
1626
    public static function lessThan($value, $limit, $message = null, $propertyPath = null)
1627
    {
1628
        if ($value >= $limit) {
1629
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1630
                $message ?: 'Provided "%s" is not less than "%s".',
1631
                self::stringify($value),
1632
                self::stringify($limit)
1633
            );
1634
1635
            throw static::createException($value, $message, static::INVALID_LESS, $propertyPath);
1636
        }
1637
    }
1638
1639
    /**
1640
     * Determines if the value is less or than given limit.
1641
     *
1642
     * @param mixed $value
1643
     * @param mixed $limit
1644
     * @param null  $message
1645
     * @param null  $propertyPath
1646
     */
1647
    public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1648
    {
1649
        if ($value > $limit) {
1650
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1651
                $message ?: 'Provided "%s" is not less or equal than "%s".',
1652
                self::stringify($value),
1653
                self::stringify($limit)
1654
            );
1655
1656
            throw static::createException($value, $message, static::INVALID_LESS_OR_EQUAL, $propertyPath);
1657
        }
1658
    }
1659
1660
    /**
1661
     * Determines if the value is greater than given limit.
1662
     *
1663
     * @param mixed $value
1664
     * @param mixed $limit
1665
     * @param null  $message
1666
     * @param null  $propertyPath
1667
     */
1668
    public static function greaterThan($value, $limit, $message = null, $propertyPath = null)
1669
    {
1670
        if ($value <= $limit) {
1671
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1672
                $message ?: 'Provided "%s" is not greater than "%s".',
1673
                self::stringify($value),
1674
                self::stringify($limit)
1675
            );
1676
1677
            throw static::createException($value, $message, static::INVALID_GREATER, $propertyPath);
1678
        }
1679
    }
1680
1681
    /**
1682
     * Determines if the value is greater or equal than given limit.
1683
     *
1684
     * @param mixed $value
1685
     * @param mixed $limit
1686
     * @param null  $message
1687
     * @param null  $propertyPath
1688
     */
1689
    public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1690
    {
1691
        if ($value < $limit) {
1692
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1693
                $message ?: 'Provided "%s" is not greater or equal than "%s".',
1694
                self::stringify($value),
1695
                self::stringify($limit)
1696
            );
1697
1698
            throw static::createException($value, $message, static::INVALID_GREATER_OR_EQUAL, $propertyPath);
1699
        }
1700
    }
1701
1702
    /**
1703
     * Assert that date is valid and corresponds to the given format.
1704
     *
1705
     * @param string      $value
1706
     * @param string      $format supports all of the options date(), except for the following:
1707
     *                            N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r.
1708
     * @param string|null $message
1709
     * @param string|null $propertyPath
1710
     *
1711
     * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters
1712
     */
1713
     public static function date($value, $format, $message = null, $propertyPath = null)
1714
     {
1715
         static::string($value, $message, $propertyPath);
1716
         static::string($format, $message, $propertyPath);
1717
1718
         $dateTime = \DateTime::createFromFormat($format, $value);
1719
1720
         if (false === $dateTime || $value !== $dateTime->format($format)) {
1721
             $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1722
                 $message ?: 'Date "%s" is invalid or does not match format "%s".',
1723
                 self::stringify($value),
1724
                 self::stringify($format)
1725
             );
1726
1727
             throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format));
1728
         }
1729
     }
1730
1731
    /**
1732
     * Make a string version of a value.
1733
     *
1734
     * @param mixed $value
1735
     * @return string
1736
     */
1737
    protected static function stringify($value)
1738
    {
1739
        if (is_bool($value)) {
1740
            return $value ? '<TRUE>' : '<FALSE>';
1741
        }
1742
1743
        if (is_scalar($value)) {
1744
            $val = (string)$value;
1745
1746
            if (strlen($val) > 100) {
1747
                $val = substr($val, 0, 97) . '...';
1748
            }
1749
1750
            return $val;
1751
        }
1752
1753
        if (is_array($value)) {
1754
            return '<ARRAY>';
1755
        }
1756
1757
        if (is_object($value)) {
1758
            return get_class($value);
1759
        }
1760
1761
        if (is_resource($value)) {
1762
            return '<RESOURCE>';
1763
        }
1764
1765
        if ($value === NULL) {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
1766
            return '<NULL>';
1767
        }
1768
1769
        return 'unknown';
1770
    }
1771
}
1772
1773