Completed
Pull Request — master (#151)
by
unknown
02:29
created

Assertion::endsWith()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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