Completed
Pull Request — master (#169)
by Woody
03:13 queued 48s
created

Assertion::custom()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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