Completed
Push — master ( 8a9ad2...75b1ab )
by Richard
02:40
created

Assertion::notBlank()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 11
rs 8.2222
cc 7
eloc 6
nc 2
nop 3
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
     * Helper method that handles building the assertion failure exceptions.
214
     * They are returned from this method so that the stack trace still shows
215
     * the assertions method.
216
     */
217
    protected static function createException($value, $message, $code, $propertyPath, array $constraints = array())
218
    {
219
        $exceptionClass = static::$exceptionClass;
220
        return new $exceptionClass($message, $code, $propertyPath, $value, $constraints);
221
    }
222
223
    /**
224
     * Assert that two values are equal (using == ).
225
     *
226
     * @param mixed $value
227
     * @param mixed $value2
228
     * @param string|null $message
229
     * @param string|null $propertyPath
230
     * @return void
231
     * @throws \Assert\AssertionFailedException
232
     */
233
    public static function eq($value, $value2, $message = null, $propertyPath = null)
234
    {
235
        if ($value != $value2) {
236
            $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...
237
                $message ?: 'Value "%s" does not equal expected value "%s".',
238
                self::stringify($value),
239
                self::stringify($value2)
240
            );
241
242
            throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, array('expected' => $value2));
243
        }
244
    }
245
246
    /**
247
     * Assert that two values are the same (using ===).
248
     *
249
     * @param mixed $value
250
     * @param mixed $value2
251
     * @param string|null $message
252
     * @param string|null $propertyPath
253
     * @return void
254
     * @throws \Assert\AssertionFailedException
255
     */
256
    public static function same($value, $value2, $message = null, $propertyPath = null)
257
    {
258
        if ($value !== $value2) {
259
            $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...
260
                $message ?: 'Value "%s" is not the same as expected value "%s".',
261
                self::stringify($value),
262
                self::stringify($value2)
263
            );
264
265
            throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, array('expected' => $value2));
266
        }
267
    }
268
269
    /**
270
     * Assert that two values are not equal (using == ).
271
     *
272
     * @param mixed $value1
273
     * @param mixed $value2
274
     * @param string|null $message
275
     * @param string|null $propertyPath
276
     * @return void
277
     * @throws \Assert\AssertionFailedException
278
     */
279
    public static function notEq($value1, $value2, $message = null, $propertyPath = null)
280
    {
281
        if ($value1 == $value2) {
282
            $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...
283
                $message ?: 'Value "%s" is equal to expected value "%s".',
284
                self::stringify($value1),
285
                self::stringify($value2)
286
            );
287
            throw static::createException($value1, $message,static::INVALID_NOT_EQ, $propertyPath, array('expected' => $value2));
288
        }
289
    }
290
291
    /**
292
     * Assert that two values are not the same (using === ).
293
     *
294
     * @param mixed $value1
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 notSame($value1, $value2, $message = null, $propertyPath = null)
302
    {
303
        if ($value1 === $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 the same as expected value "%s".',
306
                self::stringify($value1),
307
                self::stringify($value2)
308
            );
309
            throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, array('expected' => $value2));
310
        }
311
    }
312
313
    /**
314
     * Assert that value is a php integer.
315
     *
316
     * @param mixed $value
317
     * @param string|null $message
318
     * @param string|null $propertyPath
319
     * @return void
320
     * @throws \Assert\AssertionFailedException
321
     */
322
    public static function integer($value, $message = null, $propertyPath = null)
323
    {
324
        if ( ! is_int($value)) {
325
            $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...
326
                $message ?: 'Value "%s" is not an integer.',
327
                self::stringify($value)
328
            );
329
330
            throw static::createException($value, $message, static::INVALID_INTEGER, $propertyPath);
331
        }
332
    }
333
334
    /**
335
     * Assert that value is a php float.
336
     *
337
     * @param mixed $value
338
     * @param string|null $message
339
     * @param string|null $propertyPath
340
     * @return void
341
     * @throws \Assert\AssertionFailedException
342
     */
343
    public static function float($value, $message = null, $propertyPath = null)
344
    {
345
        if ( ! is_float($value)) {
346
            $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...
347
                $message ?: 'Value "%s" is not a float.',
348
                self::stringify($value)
349
            );
350
351
            throw static::createException($value, $message, static::INVALID_FLOAT, $propertyPath);
352
        }
353
    }
354
355
    /**
356
     * Validates if an integer or integerish is a digit.
357
     *
358
     * @param mixed $value
359
     * @param string|null $message
360
     * @param string|null $propertyPath
361
     * @return void
362
     * @throws \Assert\AssertionFailedException
363
     */
364
    public static function digit($value, $message = null, $propertyPath = null)
365
    {
366
        if ( ! ctype_digit((string)$value)) {
367
            $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...
368
                $message ?: 'Value "%s" is not a digit.',
369
                self::stringify($value)
370
            );
371
372
            throw static::createException($value, $message, static::INVALID_DIGIT, $propertyPath);
373
        }
374
    }
375
376
    /**
377
     * Assert that value is a php integer'ish.
378
     * @param mixed $value
379
     * @param string|null $message
380
     * @param string|null $propertyPath
381
     * @return void
382
     * @throws \Assert\AssertionFailedException
383
     */
384
    public static function integerish($value, $message = null, $propertyPath = null)
385
    {
386
        if (is_object($value) || strval(intval($value)) != $value || is_bool($value) || is_null($value)) {
387
            $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...
388
                $message ?: 'Value "%s" is not an integer or a number castable to integer.',
389
                self::stringify($value)
390
            );
391
392
            throw static::createException($value, $message, static::INVALID_INTEGERISH, $propertyPath);
393
        }
394
    }
395
396
    /**
397
     * Assert that value is php boolean
398
     *
399
     * @param mixed $value
400
     * @param string|null $message
401
     * @param string|null $propertyPath
402
     * @return void
403
     * @throws \Assert\AssertionFailedException
404
     */
405
    public static function boolean($value, $message = null, $propertyPath = null)
406
    {
407
        if ( ! is_bool($value)) {
408
            $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...
409
                $message ?: 'Value "%s" is not a boolean.',
410
                self::stringify($value)
411
            );
412
413
            throw static::createException($value, $message, static::INVALID_BOOLEAN, $propertyPath);
414
        }
415
    }
416
417
    /**
418
     * Assert that value is a PHP scalar
419
     *
420
     * @param mixed $value
421
     * @param string|null $message
422
     * @param string|null $propertyPath
423
     * @return void
424
     * @throws \Assert\AssertionFailedException
425
     */
426
    public static function scalar($value, $message = null, $propertyPath = null)
427
    {
428
        if (!is_scalar($value)) {
429
            $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...
430
                $message ?: 'Value "%s" is not a scalar.',
431
                self::stringify($value)
432
            );
433
434
            throw static::createException($value, $message, static::INVALID_SCALAR, $propertyPath);
435
        }
436
    }
437
438
    /**
439
     * Assert that value is not empty
440
     *
441
     * @param mixed $value
442
     * @param string|null $message
443
     * @param string|null $propertyPath
444
     * @return void
445
     * @throws \Assert\AssertionFailedException
446
     */
447
    public static function notEmpty($value, $message = null, $propertyPath = null)
448
    {
449
        if (empty($value)) {
450
            $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...
451
                $message ?: 'Value "%s" is empty, but non empty value was expected.',
452
                self::stringify($value)
453
            );
454
455
            throw static::createException($value, $message, static::VALUE_EMPTY, $propertyPath);
456
        }
457
    }
458
459
    /**
460
     * Assert that value is empty
461
     *
462
     * @param mixed $value
463
     * @param string|null $message
464
     * @param string|null $propertyPath
465
     * @return void
466
     * @throws \Assert\AssertionFailedException
467
     */
468
    public static function noContent($value, $message = null, $propertyPath = null)
469
    {
470
        if (!empty($value)) {
471
            $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...
472
                $message ?: 'Value "%s" is not empty, but empty value was expected.',
473
                self::stringify($value)
474
            );
475
476
            throw static::createException($value, $message, static::VALUE_NOT_EMPTY, $propertyPath);
477
        }
478
    }
479
480
    /**
481
     * Assert that value is not null
482
     *
483
     * @param mixed $value
484
     * @param string|null $message
485
     * @param string|null $propertyPath
486
     * @return void
487
     * @throws \Assert\AssertionFailedException
488
     */
489
    public static function notNull($value, $message = null, $propertyPath = null)
490
    {
491
        if ($value === null) {
492
            $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...
493
                $message ?: 'Value "%s" is null, but non null value was expected.',
494
                self::stringify($value)
495
            );
496
497
            throw static::createException($value, $message, static::VALUE_NULL, $propertyPath);
498
        }
499
    }
500
501
    /**
502
     * Assert that value is a string
503
     *
504
     * @param mixed $value
505
     * @param string|null $message
506
     * @param string|null $propertyPath
507
     * @return void
508
     * @throws \Assert\AssertionFailedException
509
     */
510
    public static function string($value, $message = null, $propertyPath = null)
511
    {
512
        if ( ! is_string($value)) {
513
            $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...
514
                $message ?: 'Value "%s" expected to be string, type %s given.',
515
                self::stringify($value),
516
                gettype($value)
517
            );
518
519
            throw static::createException($value, $message, static::INVALID_STRING, $propertyPath);
520
        }
521
    }
522
523
    /**
524
     * Assert that value matches a regex
525
     *
526
     * @param mixed $value
527
     * @param string $pattern
528
     * @param string|null $message
529
     * @param string|null $propertyPath
530
     * @return void
531
     * @throws \Assert\AssertionFailedException
532
     */
533
    public static function regex($value, $pattern, $message = null, $propertyPath = null)
534
    {
535
        static::string($value, $message, $propertyPath);
536
537
        if ( ! preg_match($pattern, $value)) {
538
            $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...
539
                $message ?: 'Value "%s" does not match expression.',
540
                self::stringify($value)
541
            );
542
543
            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...
544
        }
545
    }
546
547
    /**
548
     * Assert that string has a given length.
549
     *
550
     * @param mixed $value
551
     * @param int $length
552
     * @param string|null $message
553
     * @param string|null $propertyPath
554
     * @param string $encoding
555
     * @return void
556
     * @throws \Assert\AssertionFailedException
557
     */
558
    public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
559
    {
560
        static::string($value, $message, $propertyPath);
561
562
        if (mb_strlen($value, $encoding) !== $length) {
563
            $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...
564
                $message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
565
                self::stringify($value),
566
                $length,
567
                mb_strlen($value, $encoding)
568
            );
569
570
            $constraints = array('length' => $length, 'encoding' => $encoding);
571
            throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
572
        }
573
    }
574
575
    /**
576
     * Assert that a string is at least $minLength chars long.
577
     *
578
     * @param mixed $value
579
     * @param int $minLength
580
     * @param string|null $message
581
     * @param string|null $propertyPath
582
     * @param string $encoding
583
     * @return void
584
     * @throws \Assert\AssertionFailedException
585
     */
586
    public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8')
587
    {
588
        static::string($value, $message, $propertyPath);
589
590
        if (mb_strlen($value, $encoding) < $minLength) {
591
            $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...
592
                $message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
593
                self::stringify($value),
594
                $minLength,
595
                mb_strlen($value, $encoding)
596
            );
597
598
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
599
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
600
        }
601
    }
602
603
    /**
604
     * Assert that string value is not longer than $maxLength chars.
605
     *
606
     * @param mixed $value
607
     * @param integer $maxLength
608
     * @param string|null $message
609
     * @param string|null $propertyPath
610
     * @param string $encoding
611
     * @return void
612
     * @throws \Assert\AssertionFailedException
613
     */
614
    public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
615
    {
616
        static::string($value, $message, $propertyPath);
617
618
        if (mb_strlen($value, $encoding) > $maxLength) {
619
            $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...
620
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
621
                self::stringify($value),
622
                $maxLength,
623
                mb_strlen($value, $encoding)
624
            );
625
626
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
627
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
628
        }
629
    }
630
631
    /**
632
     * Assert that string length is between min,max lengths.
633
     *
634
     * @param mixed $value
635
     * @param integer $minLength
636
     * @param integer $maxLength
637
     * @param string|null $message
638
     * @param string|null $propertyPath
639
     * @param string $encoding
640
     * @return void
641
     * @throws \Assert\AssertionFailedException
642
     */
643
    public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
644
    {
645
        static::string($value, $message, $propertyPath);
646
647
        if (mb_strlen($value, $encoding) < $minLength) {
648
            $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...
649
                $message ?: 'Value "%s" is too short, it should have at least %d characters, but only has %d characters.',
650
                self::stringify($value),
651
                $minLength,
652
                mb_strlen($value, $encoding)
653
            );
654
655
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
656
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
657
        }
658
659
        if (mb_strlen($value, $encoding) > $maxLength) {
660
            $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...
661
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
662
                self::stringify($value),
663
                $maxLength,
664
                mb_strlen($value, $encoding)
665
            );
666
667
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
668
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
669
        }
670
    }
671
672
    /**
673
     * Assert that string starts with a sequence of chars.
674
     *
675
     * @param mixed $string
676
     * @param string $needle
677
     * @param string|null $message
678
     * @param string|null $propertyPath
679
     * @param string $encoding
680
     * @return void
681
     * @throws \Assert\AssertionFailedException
682
     */
683
    public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
684
    {
685
        static::string($string, $message, $propertyPath);
686
687
        if (mb_strpos($string, $needle, null, $encoding) !== 0) {
688
            $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...
689
                $message ?: 'Value "%s" does not start with "%s".',
690
                self::stringify($string),
691
                self::stringify($needle)
692
            );
693
694
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
695
            throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints);
696
        }
697
    }
698
699
    /**
700
     * Assert that string ends with a sequence of chars.
701
     *
702
     * @param mixed $string
703
     * @param string $needle
704
     * @param string|null $message
705
     * @param string|null $propertyPath
706
     * @param string $encoding
707
     * @return void
708
     * @throws \Assert\AssertionFailedException
709
     */
710
    public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
711
    {
712
        static::string($string, $message, $propertyPath);
713
714
        $stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding);
715
716
        if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) {
717
            $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...
718
                $message ?: 'Value "%s" does not end with "%s".',
719
                self::stringify($string),
720
                self::stringify($needle)
721
            );
722
723
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
724
            throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints);
725
        }
726
    }
727
728
    /**
729
     * Assert that string contains a sequence of chars.
730
     *
731
     * @param mixed $string
732
     * @param string $needle
733
     * @param string|null $message
734
     * @param string|null $propertyPath
735
     * @param string $encoding
736
     * @return void
737
     * @throws \Assert\AssertionFailedException
738
     */
739
    public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
740
    {
741
        static::string($string, $message, $propertyPath);
742
743
        if (mb_strpos($string, $needle, null, $encoding) === false) {
744
            $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...
745
                $message ?: 'Value "%s" does not contain "%s".',
746
                self::stringify($string),
747
                self::stringify($needle)
748
            );
749
750
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
751
            throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints);
752
        }
753
    }
754
755
    /**
756
     * Assert that value is in array of choices.
757
     *
758
     * @param mixed $value
759
     * @param array $choices
760
     * @param string|null $message
761
     * @param string|null $propertyPath
762
     * @return void
763
     * @throws \Assert\AssertionFailedException
764
     */
765
    public static function choice($value, array $choices, $message = null, $propertyPath = null)
766
    {
767
        if ( ! in_array($value, $choices, true)) {
768
            $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...
769
                $message ?: 'Value "%s" is not an element of the valid values: %s',
770
                self::stringify($value),
771
                implode(", ", array_map('Assert\Assertion::stringify', $choices))
772
            );
773
774
            throw static::createException($value, $message, static::INVALID_CHOICE, $propertyPath, array('choices' => $choices));
775
        }
776
    }
777
778
    /**
779
     * Alias of {@see choice()}
780
     *
781
     * @throws \Assert\AssertionFailedException
782
     */
783
    public static function inArray($value, array $choices, $message = null, $propertyPath = null)
784
    {
785
        static::choice($value, $choices, $message, $propertyPath);
786
    }
787
788
    /**
789
     * Assert that value is numeric.
790
     *
791
     * @param mixed $value
792
     * @param string|null $message
793
     * @param string|null $propertyPath
794
     * @return void
795
     * @throws \Assert\AssertionFailedException
796
     */
797
    public static function numeric($value, $message = null, $propertyPath = null)
798
    {
799
        if ( ! is_numeric($value)) {
800
            $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...
801
                $message ?: 'Value "%s" is not numeric.',
802
                self::stringify($value)
803
            );
804
805
            throw static::createException($value, $message, static::INVALID_NUMERIC, $propertyPath);
806
        }
807
    }
808
809
    /**
810
     * Assert that value is an array.
811
     *
812
     * @param mixed $value
813
     * @param string|null $message
814
     * @param string|null $propertyPath
815
     * @return void
816
     * @throws \Assert\AssertionFailedException
817
     */
818
    public static function isArray($value, $message = null, $propertyPath = null)
819
    {
820
        if ( ! is_array($value)) {
821
            $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...
822
                $message ?: 'Value "%s" is not an array.',
823
                self::stringify($value)
824
            );
825
826
            throw static::createException($value, $message, static::INVALID_ARRAY, $propertyPath);
827
        }
828
    }
829
830
    /**
831
     * Assert that value is an array or a traversable object.
832
     *
833
     * @param mixed $value
834
     * @param string|null $message
835
     * @param string|null $propertyPath
836
     * @return void
837
     * @throws \Assert\AssertionFailedException
838
     */
839
    public static function isTraversable($value, $message = null, $propertyPath = null)
840
    {
841
        if ( ! is_array($value) && ! $value instanceof \Traversable) {
842
            $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...
843
                $message ?: 'Value "%s" is not an array and does not implement Traversable.',
844
                self::stringify($value)
845
            );
846
847
            throw static::createException($value, $message, static::INVALID_TRAVERSABLE, $propertyPath);
848
        }
849
    }
850
851
    /**
852
     * Assert that value is an array or an array-accessible object.
853
     *
854
     * @param mixed $value
855
     * @param string|null $message
856
     * @param string|null $propertyPath
857
     * @return void
858
     * @throws \Assert\AssertionFailedException
859
     */
860
    public static function isArrayAccessible($value, $message = null, $propertyPath = null)
861
    {
862
        if ( ! is_array($value) && ! $value instanceof \ArrayAccess) {
863
            $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...
864
                $message ?: 'Value "%s" is not an array and does not implement ArrayAccess.',
865
                self::stringify($value)
866
            );
867
868
            throw static::createException($value, $message, static::INVALID_ARRAY_ACCESSIBLE, $propertyPath);
869
        }
870
    }
871
872
    /**
873
     * Assert that key exists in an array
874
     *
875
     * @param mixed $value
876
     * @param string|integer $key
877
     * @param string|null $message
878
     * @param string|null $propertyPath
879
     * @return void
880
     * @throws \Assert\AssertionFailedException
881
     */
882
    public static function keyExists($value, $key, $message = null, $propertyPath = null)
883
    {
884
        static::isArray($value, $message, $propertyPath);
885
886
        if ( ! array_key_exists($key, $value)) {
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 ?: 'Array does not contain an element with key "%s"',
889
                self::stringify($key)
890
            );
891
892
            throw static::createException($value, $message, static::INVALID_KEY_EXISTS, $propertyPath, array('key' => $key));
893
        }
894
    }
895
896
    /**
897
     * Assert that key exists in an array/array-accessible object using isset()
898
     *
899
     * @param mixed $value
900
     * @param string|integer $key
901
     * @param string|null $message
902
     * @param string|null $propertyPath
903
     * @return void
904
     * @throws \Assert\AssertionFailedException
905
     */
906
    public static function keyIsset($value, $key, $message = null, $propertyPath = null)
907
    {
908
        static::isArrayAccessible($value, $message, $propertyPath);
909
910
        if ( ! isset($value[$key])) {
911
            $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...
912
                $message ?: 'The element with key "%s" was not found',
913
                self::stringify($key)
914
            );
915
916
            throw static::createException($value, $message, static::INVALID_KEY_ISSET, $propertyPath, array('key' => $key));
917
        }
918
    }
919
920
    /**
921
     * Assert that key exists in an array/array-accessible object and it's value is not empty.
922
     *
923
     * @param mixed $value
924
     * @param string|integer $key
925
     * @param string|null $message
926
     * @param string|null $propertyPath
927
     * @return void
928
     * @throws \Assert\AssertionFailedException
929
     */
930
    public static function notEmptyKey($value, $key, $message = null, $propertyPath = null)
931
    {
932
        static::keyIsset($value, $key, $message, $propertyPath);
933
        static::notEmpty($value[$key], $message, $propertyPath);
934
    }
935
936
    /**
937
     * Assert that value is not blank
938
     *
939
     * @param mixed $value
940
     * @param string|null $message
941
     * @param string|null $propertyPath
942
     * @return void
943
     * @throws \Assert\AssertionFailedException
944
     */
945
    public static function notBlank($value, $message = null, $propertyPath = null)
946
    {
947
        if (false === $value || (empty($value) && '0' != $value) || (is_string($value) && '' === trim($value))) {
948
            $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...
949
                $message ?: 'Value "%s" is blank, but was expected to contain a value.',
950
                self::stringify($value)
951
            );
952
953
            throw static::createException($value, $message, static::INVALID_NOT_BLANK, $propertyPath);
954
        }
955
    }
956
957
    /**
958
     * Assert that value is instance of given class-name.
959
     *
960
     * @param mixed $value
961
     * @param string $className
962
     * @param string|null $message
963
     * @param string|null $propertyPath
964
     * @return void
965
     * @throws \Assert\AssertionFailedException
966
     */
967
    public static function isInstanceOf($value, $className, $message = null, $propertyPath = null)
968
    {
969
        if ( ! ($value instanceof $className)) {
970
            $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...
971
                $message ?: 'Class "%s" was expected to be instanceof of "%s" but is not.',
972
                self::stringify($value),
973
                $className
974
            );
975
976
            throw static::createException($value, $message, static::INVALID_INSTANCE_OF, $propertyPath, array('class' => $className));
977
        }
978
    }
979
980
    /**
981
     * Assert that value is not instance of given class-name.
982
     *
983
     * @param mixed $value
984
     * @param string $className
985
     * @param string|null $message
986
     * @param string|null $propertyPath
987
     * @return void
988
     * @throws \Assert\AssertionFailedException
989
     */
990
    public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null)
991
    {
992
        if ($value instanceof $className) {
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 ?: 'Class "%s" was not expected to be instanceof of "%s".',
995
                self::stringify($value),
996
                $className
997
            );
998
999
            throw static::createException($value, $message, static::INVALID_NOT_INSTANCE_OF, $propertyPath, array('class' => $className));
1000
        }
1001
    }
1002
1003
    /**
1004
     * Assert that value is subclass of given class-name.
1005
     *
1006
     * @param mixed $value
1007
     * @param string $className
1008
     * @param string|null $message
1009
     * @param string|null $propertyPath
1010
     * @return void
1011
     * @throws \Assert\AssertionFailedException
1012
     */
1013
    public static function subclassOf($value, $className, $message = null, $propertyPath = null)
1014
    {
1015
        if ( ! is_subclass_of($value, $className)) {
1016
            $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...
1017
                $message ?: 'Class "%s" was expected to be subclass of "%s".',
1018
                self::stringify($value),
1019
                $className
1020
            );
1021
1022
            throw static::createException($value, $message, static::INVALID_SUBCLASS_OF, $propertyPath, array('class' => $className));
1023
        }
1024
    }
1025
1026
    /**
1027
     * Assert that value is in range of numbers.
1028
     *
1029
     * @param mixed $value
1030
     * @param integer $minValue
1031
     * @param integer $maxValue
1032
     * @param string|null $message
1033
     * @param string|null $propertyPath
1034
     * @return void
1035
     * @throws \Assert\AssertionFailedException
1036
     */
1037
    public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null)
1038
    {
1039
        static::numeric($value, $message, $propertyPath);
1040
1041
        if ($value < $minValue || $value > $maxValue) {
1042
            $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...
1043
                $message ?: 'Number "%s" was expected to be at least "%d" and at most "%d".',
1044
                self::stringify($value),
1045
                self::stringify($minValue),
1046
                self::stringify($maxValue)
1047
            );
1048
1049
            throw static::createException($value, $message, static::INVALID_RANGE, $propertyPath, array('min' => $minValue, 'max' => $maxValue));
1050
        }
1051
    }
1052
1053
    /**
1054
     * Assert that a value is at least as big as a given limit
1055
     *
1056
     * @param mixed $value
1057
     * @param mixed $minValue
1058
     * @param string|null $message
1059
     * @param string|null $propertyPath
1060
     * @return void
1061
     * @throws \Assert\AssertionFailedException
1062
     */
1063
    public static function min($value, $minValue, $message = null, $propertyPath = null)
1064
    {
1065
        static::numeric($value, $message, $propertyPath);
1066
1067
        if ($value < $minValue) {
1068
            $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...
1069
                $message ?: 'Number "%s" was expected to be at least "%d".',
1070
                self::stringify($value),
1071
                self::stringify($minValue)
1072
            );
1073
1074
            throw static::createException($value, $message, static::INVALID_MIN, $propertyPath, array('min' => $minValue));
1075
        }
1076
    }
1077
1078
    /**
1079
     * Assert that a number is smaller as a given limit
1080
     *
1081
     * @param mixed $value
1082
     * @param mixed $maxValue
1083
     * @param string|null $message
1084
     * @param string|null $propertyPath
1085
     * @return void
1086
     * @throws \Assert\AssertionFailedException
1087
     */
1088
    public static function max($value, $maxValue, $message = null, $propertyPath = null)
1089
    {
1090
        static::numeric($value, $message, $propertyPath);
1091
1092
        if ($value > $maxValue) {
1093
            $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...
1094
                $message ?: 'Number "%s" was expected to be at most "%d".',
1095
                self::stringify($value),
1096
                self::stringify($maxValue)
1097
            );
1098
1099
            throw static::createException($value, $message, static::INVALID_MAX, $propertyPath, array('max' => $maxValue));
1100
        }
1101
    }
1102
1103
    /**
1104
     * Assert that a file exists
1105
     *
1106
     * @param string $value
1107
     * @param string|null $message
1108
     * @param string|null $propertyPath
1109
     * @return void
1110
     * @throws \Assert\AssertionFailedException
1111
     */
1112
    public static function file($value, $message = null, $propertyPath = null)
1113
    {
1114
        static::string($value, $message, $propertyPath);
1115
        static::notEmpty($value, $message, $propertyPath);
1116
1117
        if ( ! is_file($value)) {
1118
            $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...
1119
                $message ?: 'File "%s" was expected to exist.',
1120
                self::stringify($value)
1121
            );
1122
1123
            throw static::createException($value, $message, static::INVALID_FILE, $propertyPath);
1124
        }
1125
    }
1126
1127
    /**
1128
     * Assert that a directory exists
1129
     *
1130
     * @param string $value
1131
     * @param string|null $message
1132
     * @param string|null $propertyPath
1133
     * @return void
1134
     * @throws \Assert\AssertionFailedException
1135
     */
1136
    public static function directory($value, $message = null, $propertyPath = null)
1137
    {
1138
        static::string($value, $message, $propertyPath);
1139
1140
        if ( ! is_dir($value)) {
1141
            $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...
1142
                $message ?: 'Path "%s" was expected to be a directory.',
1143
                self::stringify($value)
1144
            );
1145
1146
            throw static::createException($value, $message, static::INVALID_DIRECTORY, $propertyPath);
1147
        }
1148
    }
1149
1150
    /**
1151
     * Assert that the value is something readable
1152
     *
1153
     * @param string $value
1154
     * @param string|null $message
1155
     * @param string|null $propertyPath
1156
     * @return void
1157
     * @throws \Assert\AssertionFailedException
1158
     */
1159
    public static function readable($value, $message = null, $propertyPath = null)
1160
    {
1161
        static::string($value, $message, $propertyPath);
1162
1163
        if ( ! is_readable($value)) {
1164
            $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...
1165
                $message ?: 'Path "%s" was expected to be readable.',
1166
                self::stringify($value)
1167
            );
1168
1169
            throw static::createException($value, $message, static::INVALID_READABLE, $propertyPath);
1170
        }
1171
    }
1172
1173
    /**
1174
     * Assert that the value is something writeable
1175
     *
1176
     * @param string $value
1177
     * @param string|null $message
1178
     * @param string|null $propertyPath
1179
     * @return void
1180
     * @throws \Assert\AssertionFailedException
1181
     */
1182
    public static function writeable($value, $message = null, $propertyPath = null)
1183
    {
1184
        static::string($value, $message, $propertyPath);
1185
1186
        if ( ! is_writeable($value)) {
1187
            $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...
1188
                $message ?: 'Path "%s" was expected to be writeable.',
1189
                self::stringify($value)
1190
            );
1191
1192
            throw static::createException($value, $message, static::INVALID_WRITEABLE, $propertyPath);
1193
        }
1194
    }
1195
1196
    /**
1197
     * Assert that value is an email adress (using
1198
     * input_filter/FILTER_VALIDATE_EMAIL).
1199
     *
1200
     * @param mixed $value
1201
     * @param string|null $message
1202
     * @param string|null $propertyPath
1203
     * @return void
1204
     * @throws \Assert\AssertionFailedException
1205
     */
1206
    public static function email($value, $message = null, $propertyPath = null)
1207
    {
1208
        static::string($value, $message, $propertyPath);
1209
1210
        if ( ! filter_var($value, FILTER_VALIDATE_EMAIL)) {
1211
            $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...
1212
                $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1213
                self::stringify($value)
1214
            );
1215
1216
            throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1217
        } else {
1218
            $host = substr($value, strpos($value, '@') + 1);
1219
1220
            // Likely not a FQDN, bug in PHP FILTER_VALIDATE_EMAIL prior to PHP 5.3.3
1221
            if (version_compare(PHP_VERSION, '5.3.3', '<') && strpos($host, '.') === false) {
1222
                $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...
1223
                    $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1224
                    self::stringify($value)
1225
                );
1226
1227
                throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1228
            }
1229
        }
1230
    }
1231
1232
    /**
1233
     * Assert that value is an URL.
1234
     *
1235
     * This code snipped was taken from the Symfony project and modified to the special demands of this method.
1236
     *
1237
     * @param mixed $value
1238
     * @param string|null $message
1239
     * @param string|null $propertyPath
1240
     * @return void
1241
     * @throws \Assert\AssertionFailedException
1242
     *
1243
     *
1244
     * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php
1245
     * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php
1246
     */
1247
    public static function url($value, $message = null, $propertyPath = null)
1248
    {
1249
        static::string($value, $message, $propertyPath);
1250
1251
        $protocols = array('http', 'https');
1252
1253
        $pattern = '~^
1254
            (%s)://                                 # protocol
1255
            (
1256
                ([\pL\pN\pS-]+\.)+[\pL]+                   # a domain name
1257
                    |                                     #  or
1258
                \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}      # a IP address
1259
                    |                                     #  or
1260
                \[
1261
                    (?:(?:(?:(?:(?:(?:(?:[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})))?::))))
1262
                \]  # a IPv6 address
1263
            )
1264
            (:[0-9]+)?                              # a port (optional)
1265
            (/?|/\S+)                               # a /, nothing or a / with something
1266
        $~ixu';
1267
1268
        $pattern = sprintf($pattern, implode('|', $protocols));
1269
1270
        if (!preg_match($pattern, $value)) {
1271
            $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...
1272
                $message ?: 'Value "%s" was expected to be a valid URL starting with http or https',
1273
                self::stringify($value)
1274
            );
1275
1276
            throw static::createException($value, $message, static::INVALID_URL, $propertyPath);
1277
        }
1278
1279
    }
1280
1281
    /**
1282
     * Assert that value is alphanumeric.
1283
     *
1284
     * @param mixed $value
1285
     * @param string|null $message
1286
     * @param string|null $propertyPath
1287
     * @return void
1288
     * @throws \Assert\AssertionFailedException
1289
     */
1290
    public static function alnum($value, $message = null, $propertyPath = null)
1291
    {
1292
        try {
1293
            static::regex($value, '(^([a-zA-Z]{1}[a-zA-Z0-9]*)$)', $message, $propertyPath);
1294
        } catch(AssertionFailedException $e) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after CATCH keyword; 0 found
Loading history...
1295
            $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...
1296
                $message ?: 'Value "%s" is not alphanumeric, starting with letters and containing only letters and numbers.',
1297
                self::stringify($value)
1298
            );
1299
1300
            throw static::createException($value, $message, static::INVALID_ALNUM, $propertyPath);
1301
        }
1302
    }
1303
1304
    /**
1305
     * Assert that the value is boolean True.
1306
     *
1307
     * @param mixed $value
1308
     * @param string|null $message
1309
     * @param string|null $propertyPath
1310
     * @return void
1311
     * @throws \Assert\AssertionFailedException
1312
     */
1313
    public static function true($value, $message = null, $propertyPath = null)
1314
    {
1315
        if ($value !== true) {
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" is not TRUE.',
1318
                self::stringify($value)
1319
            );
1320
1321
            throw static::createException($value, $message, static::INVALID_TRUE, $propertyPath);
1322
        }
1323
    }
1324
1325
    /**
1326
     * Assert that the value is boolean False.
1327
     *
1328
     * @param mixed $value
1329
     * @param string|null $message
1330
     * @param string|null $propertyPath
1331
     * @return void
1332
     * @throws \Assert\AssertionFailedException
1333
     */
1334
    public static function false($value, $message = null, $propertyPath = null)
1335
    {
1336
        if ($value !== false) {
1337
            $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...
1338
                $message ?: 'Value "%s" is not FALSE.',
1339
                self::stringify($value)
1340
            );
1341
1342
            throw static::createException($value, $message, static::INVALID_FALSE, $propertyPath);
1343
        }
1344
    }
1345
1346
    /**
1347
     * Assert that the class exists.
1348
     *
1349
     * @param mixed $value
1350
     * @param string|null $message
1351
     * @param string|null $propertyPath
1352
     * @return void
1353
     * @throws \Assert\AssertionFailedException
1354
     */
1355
    public static function classExists($value, $message = null, $propertyPath = null)
1356
    {
1357
        if ( ! class_exists($value)) {
1358
            $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...
1359
                $message ?: 'Class "%s" does not exist.',
1360
                self::stringify($value)
1361
            );
1362
1363
            throw static::createException($value, $message, static::INVALID_CLASS, $propertyPath);
1364
        }
1365
    }
1366
1367
    /**
1368
     * Assert that the class implements the interface
1369
     *
1370
     * @param mixed $class
1371
     * @param string $interfaceName
1372
     * @param string|null $message
1373
     * @param string|null $propertyPath
1374
     * @return void
1375
     * @throws \Assert\AssertionFailedException
1376
     */
1377
    public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null)
1378
    {
1379
        $reflection = new \ReflectionClass($class);
1380
        if ( ! $reflection->implementsInterface($interfaceName)) {
1381
            $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...
1382
                $message ?: 'Class "%s" does not implement interface "%s".',
1383
                self::stringify($class),
1384
                self::stringify($interfaceName)
1385
            );
1386
1387
            throw static::createException($class, $message, static::INTERFACE_NOT_IMPLEMENTED, $propertyPath, array('interface' => $interfaceName));
1388
        }
1389
    }
1390
1391
    /**
1392
     * Assert that the given string is a valid json string.
1393
     *
1394
     * NOTICE:
1395
     * Since this does a json_decode to determine its validity
1396
     * you probably should consider, when using the variable
1397
     * content afterwards, just to decode and check for yourself instead
1398
     * of using this assertion.
1399
     *
1400
     * @param mixed $value
1401
     * @param string|null $message
1402
     * @param string|null $propertyPath
1403
     * @return void
1404
     * @throws \Assert\AssertionFailedException
1405
     */
1406
    public static function isJsonString($value, $message = null, $propertyPath = null)
1407
    {
1408
        if (null === json_decode($value) && JSON_ERROR_NONE !== json_last_error()) {
1409
            $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...
1410
                $message ?: 'Value "%s" is not a valid JSON string.',
1411
                self::stringify($value)
1412
            );
1413
1414
            throw static::createException($value, $message, static::INVALID_JSON_STRING, $propertyPath);
1415
        }
1416
    }
1417
1418
    /**
1419
     * Assert that the given string is a valid UUID
1420
     *
1421
     * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed.
1422
     *
1423
     * @param string $value
1424
     * @param string|null $message
1425
     * @param string|null $propertyPath
1426
     * @return void
1427
     * @throws \Assert\AssertionFailedException
1428
     */
1429
    public static function uuid($value, $message = null, $propertyPath = null)
1430
    {
1431
        $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...
1432
1433
        if ($value === '00000000-0000-0000-0000-000000000000') {
1434
            return;
1435
        }
1436
1437
        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)) {
1438
            $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...
1439
                $message ?: 'Value "%s" is not a valid UUID.',
1440
                self::stringify($value)
1441
            );
1442
1443
            throw static::createException($value, $message, static::INVALID_UUID, $propertyPath);
1444
        }
1445
    }
1446
1447
    /**
1448
     * Assert that the count of countable is equal to count.
1449
     *
1450
     * @param array|\Countable $countable
1451
     * @param int              $count
1452
     * @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...
1453
     * @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...
1454
     * @return void
1455
     * @throws \Assert\AssertionFailedException
1456
     */
1457
    public static function count($countable, $count, $message = null, $propertyPath = null)
1458
    {
1459
        if ($count !== count($countable)) {
1460
            $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...
1461
                $message ?: 'List does not contain exactly "%d" elements.',
1462
                self::stringify($count)
1463
            );
1464
1465
            throw static::createException($countable, $message, static::INVALID_COUNT, $propertyPath, array('count' => $count));
1466
        }
1467
    }
1468
1469
    /**
1470
     * static call handler to implement:
1471
     *  - "null or assertion" delegation
1472
     *  - "all" delegation
1473
     */
1474
    public static function __callStatic($method, $args)
1475
    {
1476
        if (strpos($method, "nullOr") === 0) {
1477
            if ( ! array_key_exists(0, $args)) {
1478
                throw new BadMethodCallException("Missing the first argument.");
1479
            }
1480
1481
            if ($args[0] === null) {
1482
                return;
1483
            }
1484
1485
            $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...
1486
1487
            return call_user_func_array(array(get_called_class(), $method), $args);
1488
        }
1489
1490
        if (strpos($method, "all") === 0) {
1491
            if ( ! array_key_exists(0, $args)) {
1492
                throw new BadMethodCallException("Missing the first argument.");
1493
            }
1494
1495
            static::isTraversable($args[0]);
1496
1497
            $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...
1498
            $values      = array_shift($args);
1499
            $calledClass = get_called_class();
1500
1501
            foreach ($values as $value) {
1502
                call_user_func_array(array($calledClass, $method), array_merge(array($value), $args));
1503
            }
1504
1505
            return;
1506
        }
1507
1508
        throw new BadMethodCallException("No assertion Assertion#" . $method . " exists.");
1509
    }
1510
1511
    /**
1512
     * Determines if the values array has every choice as key and that this choice has content.
1513
     *
1514
     * @param array $values
1515
     * @param array $choices
1516
     * @param null  $message
1517
     * @param null  $propertyPath
1518
     */
1519
    public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null)
1520
    {
1521
        self::notEmpty($values, $message, $propertyPath);
1522
1523
        foreach ($choices as $choice) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1524
1525
            self::notEmptyKey($values, $choice, $message, $propertyPath);
1526
        }
1527
    }
1528
1529
    /**
1530
     * Determines that the named method is defined in the provided object.
1531
     *
1532
     * @param string $value
1533
     * @param mixed  $object
1534
     * @param null   $message
1535
     * @param null   $propertyPath
1536
     *
1537
     * @throws
1538
     */
1539
    public static function methodExists($value, $object, $message = null, $propertyPath = null)
1540
    {
1541
        self::isObject($object, $message, $propertyPath);
1542
1543
        if (!method_exists($object, $value)) {
1544
            $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...
1545
                $message ?: 'Expected "%s" does not exist in provided object.',
1546
                self::stringify($value)
1547
            );
1548
1549
            throw static::createException($value, $message, static::INVALID_METHOD, $propertyPath);
1550
        }
1551
    }
1552
1553
    /**
1554
     * Determines that the provided value is an object.
1555
     *
1556
     * @param mixed $value
1557
     * @param null  $message
1558
     * @param null  $propertyPath
1559
     */
1560
    public static function isObject($value, $message = null, $propertyPath = null)
1561
    {
1562
        if (!is_object($value)) {
1563
            $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...
1564
                $message ?: 'Provided "%s" is not a valid object.',
1565
                self::stringify($value)
1566
            );
1567
1568
            throw static::createException($value, $message, static::INVALID_OBJECT, $propertyPath);
1569
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1570
        }
1571
    }
1572
1573
    /**
1574
     * Determines if the value is less than given limit.
1575
     *
1576
     * @param mixed $value
1577
     * @param mixed $limit
1578
     * @param null  $message
1579
     * @param null  $propertyPath
1580
     */
1581
    public static function lessThan($value, $limit, $message = null, $propertyPath = null)
1582
    {
1583
        if ($value >= $limit) {
1584
            $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...
1585
                $message ?: 'Provided "%s" is not less than "%s".',
1586
                self::stringify($value),
1587
                self::stringify($limit)
1588
            );
1589
1590
            throw static::createException($value, $message, static::INVALID_LESS, $propertyPath);
1591
        }
1592
    }
1593
1594
    /**
1595
     * Determines if the value is less or than given limit.
1596
     *
1597
     * @param mixed $value
1598
     * @param mixed $limit
1599
     * @param null  $message
1600
     * @param null  $propertyPath
1601
     */
1602
    public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1603
    {
1604
        if ($value > $limit) {
1605
            $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...
1606
                $message ?: 'Provided "%s" is not less or equal than "%s".',
1607
                self::stringify($value),
1608
                self::stringify($limit)
1609
            );
1610
1611
            throw static::createException($value, $message, static::INVALID_LESS_OR_EQUAL, $propertyPath);
1612
        }
1613
    }
1614
1615
    /**
1616
     * Determines if the value is greater than given limit.
1617
     *
1618
     * @param mixed $value
1619
     * @param mixed $limit
1620
     * @param null  $message
1621
     * @param null  $propertyPath
1622
     */
1623
    public static function greaterThan($value, $limit, $message = null, $propertyPath = null)
1624
    {
1625
        if ($value <= $limit) {
1626
            $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...
1627
                $message ?: 'Provided "%s" is not greater than "%s".',
1628
                self::stringify($value),
1629
                self::stringify($limit)
1630
            );
1631
1632
            throw static::createException($value, $message, static::INVALID_GREATER, $propertyPath);
1633
        }
1634
    }
1635
1636
    /**
1637
     * Determines if the value is greater or equal than given limit.
1638
     *
1639
     * @param mixed $value
1640
     * @param mixed $limit
1641
     * @param null  $message
1642
     * @param null  $propertyPath
1643
     */
1644
    public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1645
    {
1646
        if ($value < $limit) {
1647
            $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...
1648
                $message ?: 'Provided "%s" is not greater or equal than "%s".',
1649
                self::stringify($value),
1650
                self::stringify($limit)
1651
            );
1652
1653
            throw static::createException($value, $message, static::INVALID_GREATER_OR_EQUAL, $propertyPath);
1654
        }
1655
    }
1656
1657
    /**
1658
     * Assert that date is valid and corresponds to the given format.
1659
     *
1660
     * @param string      $value
1661
     * @param string      $format supports all of the options date(), except for the following:
1662
     *                            N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r.
1663
     * @param string|null $message
1664
     * @param string|null $propertyPath
1665
     *
1666
     * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters
1667
     */
1668
     public static function date($value, $format, $message = null, $propertyPath = null)
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 4 spaces, found 5
Loading history...
1669
     {
1670
         static::string($value, $message, $propertyPath);
1671
         static::string($format, $message, $propertyPath);
1672
1673
         $dateTime = \DateTime::createFromFormat($format, $value);
1674
1675
         if (false === $dateTime || $value !== $dateTime->format($format)) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 9
Loading history...
1676
             $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...
1677
                 $message ?: 'Date "%s" is invalid or does not match format "%s".',
1678
                 self::stringify($value),
1679
                 self::stringify($format)
1680
             );
1681
1682
             throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format));
1683
         }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 9
Loading history...
1684
     }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 5
Loading history...
1685
1686
    /**
1687
     * Make a string version of a value.
1688
     *
1689
     * @param mixed $value
1690
     * @return string
1691
     */
1692
    protected static function stringify($value)
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 4
Loading history...
1693
    {
1 ignored issue
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 8 spaces, but found 4).
Loading history...
1694
        if (is_bool($value)) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 8
Loading history...
1695
            return $value ? '<TRUE>' : '<FALSE>';
1 ignored issue
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 16 spaces, but found 12).
Loading history...
1696
        }
1697
1698
        if (is_scalar($value)) {
1699
            $val = (string)$value;
1700
1701
            if (strlen($val) > 100) {
1702
                $val = substr($val, 0, 97) . '...';
1703
            }
1704
1705
            return $val;
1706
        }
1707
1708
        if (is_array($value)) {
1709
            return '<ARRAY>';
1710
        }
1711
1712
        if (is_object($value)) {
1713
            return get_class($value);
1714
        }
1715
1716
        if (is_resource($value)) {
1717
            return '<RESOURCE>';
1718
        }
1719
1720
        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...
1721
            return '<NULL>';
1722
        }
1723
1724
        return 'unknown';
1725
    }
1726
}
1727
1728