Completed
Push — master ( 441bc9...07f413 )
by Richard
10s
created

Assertion::keyIsset()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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