Completed
Push — extending_assert ( 3ffee9...7e0c63 )
by Richard
04:22
created

Assertion::setAssertionClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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