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

Assertion::keyNotExists()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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