Completed
Pull Request — master (#165)
by Diego
02:05
created

Assertion::null()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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