Completed
Pull Request — master (#152)
by Dominique
06:29
created

Assertion::isCallable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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