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

Assertion::isJsonString()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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