Completed
Pull Request — master (#174)
by Thomas
02:07
created

Assertion::__callStatic()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 7
nop 2
dl 0
loc 36
rs 6.7272
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 allE164($value, $message = null, $propertyPath = null) Assert that the given string is a valid E164 Phone Number for all values.
35
 * @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.
36
 * @method static void allEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string ends with a sequence of chars for all values.
37
 * @method static void allEq($value, $value2, $message = null, $propertyPath = null) Assert that two values are equal (using == ) for all values.
38
 * @method static void allFalse($value, $message = null, $propertyPath = null) Assert that the value is boolean False for all values.
39
 * @method static void allFile($value, $message = null, $propertyPath = null) Assert that a file exists for all values.
40
 * @method static void allFloat($value, $message = null, $propertyPath = null) Assert that value is a php float for all values.
41
 * @method static void allGreaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is greater or equal than given limit for all values.
42
 * @method static void allGreaterThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is greater than given limit for all values.
43
 * @method static void allImplementsInterface($class, $interfaceName, $message = null, $propertyPath = null) Assert that the class implements the interface for all values.
44
 * @method static void allInArray($value, $choices, $message = null, $propertyPath = null) Alias of {@see choice()} for all values.
45
 * @method static void allInteger($value, $message = null, $propertyPath = null) Assert that value is a php integer for all values.
46
 * @method static void allIntegerish($value, $message = null, $propertyPath = null) Assert that value is a php integer'ish for all values.
47
 * @method static void allInterfaceExists($value, $message = null, $propertyPath = null) Assert that the interface exists for all values.
48
 * @method static void allIsArray($value, $message = null, $propertyPath = null) Assert that value is an array for all values.
49
 * @method static void allIsArrayAccessible($value, $message = null, $propertyPath = null) Assert that value is an array or an array-accessible object for all values.
50
 * @method static void allIsCallable($value, $message = null, $propertyPath = null) Determines that the provided value is callable for all values.
51
 * @method static void allIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is instance of given class-name for all values.
52
 * @method static void allIsJsonString($value, $message = null, $propertyPath = null) Assert that the given string is a valid json string for all values.
53
 * @method static void allIsObject($value, $message = null, $propertyPath = null) Determines that the provided value is an object for all values.
54
 * @method static void allIsTraversable($value, $message = null, $propertyPath = null) Assert that value is an array or a traversable object for all values.
55
 * @method static void allKeyExists($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array for all values.
56
 * @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.
57
 * @method static void allKeyNotExists($value, $key, $message = null, $propertyPath = null) Assert that key does not exist in an array for all values.
58
 * @method static void allLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string has a given length for all values.
59
 * @method static void allLessOrEqualThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is less or than given limit for all values.
60
 * @method static void allLessThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is less than given limit for all values.
61
 * @method static void allMax($value, $maxValue, $message = null, $propertyPath = null) Assert that a number is smaller as a given limit for all values.
62
 * @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.
63
 * @method static void allMethodExists($value, $object, $message = null, $propertyPath = null) Determines that the named method is defined in the provided object for all values.
64
 * @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.
65
 * @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.
66
 * @method static void allNoContent($value, $message = null, $propertyPath = null) Assert that value is empty for all values.
67
 * @method static void allNotBlank($value, $message = null, $propertyPath = null) Assert that value is not blank for all values.
68
 * @method static void allNotEmpty($value, $message = null, $propertyPath = null) Assert that value is not empty for all values.
69
 * @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.
70
 * @method static void allNotEq($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ) for all values.
71
 * @method static void allNotInArray($value, $choices, $message = null, $propertyPath = null) Assert that value is not in array of choices for all values.
72
 * @method static void allNotIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name for all values.
73
 * @method static void allNotNull($value, $message = null, $propertyPath = null) Assert that value is not null for all values.
74
 * @method static void allNotSame($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ) 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 nullOrE164($value, $message = null, $propertyPath = null) Assert that the given string is a valid E164 Phone Number or that the value is null.
101
 * @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.
102
 * @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.
103
 * @method static void nullOrEq($value, $value2, $message = null, $propertyPath = null) Assert that two values are equal (using == ) or that the value is null.
104
 * @method static void nullOrFalse($value, $message = null, $propertyPath = null) Assert that the value is boolean False or that the value is null.
105
 * @method static void nullOrFile($value, $message = null, $propertyPath = null) Assert that a file exists or that the value is null.
106
 * @method static void nullOrFloat($value, $message = null, $propertyPath = null) Assert that value is a php float or that the value is null.
107
 * @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.
108
 * @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.
109
 * @method static void nullOrImplementsInterface($class, $interfaceName, $message = null, $propertyPath = null) Assert that the class implements the interface or that the value is null.
110
 * @method static void nullOrInArray($value, $choices, $message = null, $propertyPath = null) Alias of {@see choice()} or that the value is null.
111
 * @method static void nullOrInteger($value, $message = null, $propertyPath = null) Assert that value is a php integer or that the value is null.
112
 * @method static void nullOrIntegerish($value, $message = null, $propertyPath = null) Assert that value is a php integer'ish or that the value is null.
113
 * @method static void nullOrInterfaceExists($value, $message = null, $propertyPath = null) Assert that the interface exists or that the value is null.
114
 * @method static void nullOrIsArray($value, $message = null, $propertyPath = null) Assert that value is an array or that the value is null.
115
 * @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.
116
 * @method static void nullOrIsCallable($value, $message = null, $propertyPath = null) Determines that the provided value is callable or that the value is null.
117
 * @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.
118
 * @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.
119
 * @method static void nullOrIsObject($value, $message = null, $propertyPath = null) Determines that the provided value is an object or that the value is null.
120
 * @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.
121
 * @method static void nullOrKeyExists($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array or that the value is null.
122
 * @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.
123
 * @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.
124
 * @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.
125
 * @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.
126
 * @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.
127
 * @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.
128
 * @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.
129
 * @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.
130
 * @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.
131
 * @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.
132
 * @method static void nullOrNoContent($value, $message = null, $propertyPath = null) Assert that value is empty or that the value is null.
133
 * @method static void nullOrNotBlank($value, $message = null, $propertyPath = null) Assert that value is not blank or that the value is null.
134
 * @method static void nullOrNotEmpty($value, $message = null, $propertyPath = null) Assert that value is not empty or that the value is null.
135
 * @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.
136
 * @method static void nullOrNotEq($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ) or that the value is null.
137
 * @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.
138
 * @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.
139
 * @method static void nullOrNotNull($value, $message = null, $propertyPath = null) Assert that value is not null or that the value is null.
140
 * @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.
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 INVALID_STRING            = 16;
166
    const INVALID_REGEX             = 17;
167
    const INVALID_MIN_LENGTH        = 18;
168
    const INVALID_MAX_LENGTH        = 19;
169
    const INVALID_STRING_START      = 20;
170
    const INVALID_STRING_CONTAINS   = 21;
171
    const INVALID_CHOICE            = 22;
172
    const INVALID_NUMERIC           = 23;
173
    const INVALID_ARRAY             = 24;
174
    const INVALID_KEY_EXISTS        = 26;
175
    const INVALID_NOT_BLANK         = 27;
176
    const INVALID_INSTANCE_OF       = 28;
177
    const INVALID_SUBCLASS_OF       = 29;
178
    const INVALID_RANGE             = 30;
179
    const INVALID_ALNUM             = 31;
180
    const INVALID_TRUE              = 32;
181
    const INVALID_EQ                = 33;
182
    const INVALID_SAME              = 34;
183
    const INVALID_MIN               = 35;
184
    const INVALID_MAX               = 36;
185
    const INVALID_LENGTH            = 37;
186
    const INVALID_FALSE             = 38;
187
    const INVALID_STRING_END        = 39;
188
    const INVALID_UUID              = 40;
189
    const INVALID_COUNT             = 41;
190
    const INVALID_NOT_EQ            = 42;
191
    const INVALID_NOT_SAME          = 43;
192
    const INVALID_TRAVERSABLE       = 44;
193
    const INVALID_ARRAY_ACCESSIBLE  = 45;
194
    const INVALID_KEY_ISSET         = 46;
195
    const INVALID_VALUE_IN_ARRAY    = 47;
196
    const INVALID_E164              = 48;
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 not 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 notNull($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 null, but non null value was expected.',
533
                static::stringify($value)
534
            );
535
536
            throw static::createException($value, $message, static::VALUE_NULL, $propertyPath);
537
        }
538
    }
539
540
    /**
541
     * Assert that value is a string
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 string($value, $message = null, $propertyPath = null)
550
    {
551
        if ( ! is_string($value)) {
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" expected to be string, type %s given.',
554
                static::stringify($value),
555
                gettype($value)
556
            );
557
558
            throw static::createException($value, $message, static::INVALID_STRING, $propertyPath);
559
        }
560
    }
561
562
    /**
563
     * Assert that value matches a regex
564
     *
565
     * @param mixed $value
566
     * @param string $pattern
567
     * @param string|null $message
568
     * @param string|null $propertyPath
569
     * @return void
570
     * @throws \Assert\AssertionFailedException
571
     */
572
    public static function regex($value, $pattern, $message = null, $propertyPath = null)
573
    {
574
        static::string($value, $message, $propertyPath);
575
576
        if ( ! preg_match($pattern, $value)) {
577
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
578
                $message ?: 'Value "%s" does not match expression.',
579
                static::stringify($value)
580
            );
581
582
            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...
583
        }
584
    }
585
586
    /**
587
     * Assert that string has a given length.
588
     *
589
     * @param mixed $value
590
     * @param int $length
591
     * @param string|null $message
592
     * @param string|null $propertyPath
593
     * @param string $encoding
594
     * @return void
595
     * @throws \Assert\AssertionFailedException
596
     */
597
    public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
598
    {
599
        static::string($value, $message, $propertyPath);
600
601
        if (mb_strlen($value, $encoding) !== $length) {
602
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
603
                $message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
604
                static::stringify($value),
605
                $length,
606
                mb_strlen($value, $encoding)
607
            );
608
609
            $constraints = array('length' => $length, 'encoding' => $encoding);
610
            throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
611
        }
612
    }
613
614
    /**
615
     * Assert that a string is at least $minLength chars long.
616
     *
617
     * @param mixed $value
618
     * @param int $minLength
619
     * @param string|null $message
620
     * @param string|null $propertyPath
621
     * @param string $encoding
622
     * @return void
623
     * @throws \Assert\AssertionFailedException
624
     */
625
    public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8')
626
    {
627
        static::string($value, $message, $propertyPath);
628
629
        if (mb_strlen($value, $encoding) < $minLength) {
630
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
631
                $message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
632
                static::stringify($value),
633
                $minLength,
634
                mb_strlen($value, $encoding)
635
            );
636
637
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
638
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
639
        }
640
    }
641
642
    /**
643
     * Assert that string value is not longer than $maxLength chars.
644
     *
645
     * @param mixed $value
646
     * @param integer $maxLength
647
     * @param string|null $message
648
     * @param string|null $propertyPath
649
     * @param string $encoding
650
     * @return void
651
     * @throws \Assert\AssertionFailedException
652
     */
653
    public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
654
    {
655
        static::string($value, $message, $propertyPath);
656
657
        if (mb_strlen($value, $encoding) > $maxLength) {
658
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
659
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
660
                static::stringify($value),
661
                $maxLength,
662
                mb_strlen($value, $encoding)
663
            );
664
665
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
666
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
667
        }
668
    }
669
670
    /**
671
     * Assert that string length is between min,max lengths.
672
     *
673
     * @param mixed $value
674
     * @param integer $minLength
675
     * @param integer $maxLength
676
     * @param string|null $message
677
     * @param string|null $propertyPath
678
     * @param string $encoding
679
     * @return void
680
     * @throws \Assert\AssertionFailedException
681
     */
682
    public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
683
    {
684
        static::string($value, $message, $propertyPath);
685
686
        if (mb_strlen($value, $encoding) < $minLength) {
687
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
688
                $message ?: 'Value "%s" is too short, it should have at least %d characters, but only has %d characters.',
689
                static::stringify($value),
690
                $minLength,
691
                mb_strlen($value, $encoding)
692
            );
693
694
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
695
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
696
        }
697
698
        if (mb_strlen($value, $encoding) > $maxLength) {
699
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
700
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
701
                static::stringify($value),
702
                $maxLength,
703
                mb_strlen($value, $encoding)
704
            );
705
706
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
707
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
708
        }
709
    }
710
711
    /**
712
     * Assert that string starts with a sequence of chars.
713
     *
714
     * @param mixed $string
715
     * @param string $needle
716
     * @param string|null $message
717
     * @param string|null $propertyPath
718
     * @param string $encoding
719
     * @return void
720
     * @throws \Assert\AssertionFailedException
721
     */
722
    public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
723
    {
724
        static::string($string, $message, $propertyPath);
725
726
        if (mb_strpos($string, $needle, null, $encoding) !== 0) {
727
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
728
                $message ?: 'Value "%s" does not start with "%s".',
729
                static::stringify($string),
730
                static::stringify($needle)
731
            );
732
733
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
734
            throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints);
735
        }
736
    }
737
738
    /**
739
     * Assert that string ends with a sequence of chars.
740
     *
741
     * @param mixed $string
742
     * @param string $needle
743
     * @param string|null $message
744
     * @param string|null $propertyPath
745
     * @param string $encoding
746
     * @return void
747
     * @throws \Assert\AssertionFailedException
748
     */
749
    public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
750
    {
751
        static::string($string, $message, $propertyPath);
752
753
        $stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding);
754
755
        if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) {
756
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
757
                $message ?: 'Value "%s" does not end with "%s".',
758
                static::stringify($string),
759
                static::stringify($needle)
760
            );
761
762
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
763
            throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints);
764
        }
765
    }
766
767
    /**
768
     * Assert that string contains a sequence of chars.
769
     *
770
     * @param mixed $string
771
     * @param string $needle
772
     * @param string|null $message
773
     * @param string|null $propertyPath
774
     * @param string $encoding
775
     * @return void
776
     * @throws \Assert\AssertionFailedException
777
     */
778
    public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
779
    {
780
        static::string($string, $message, $propertyPath);
781
782
        if (mb_strpos($string, $needle, null, $encoding) === false) {
783
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
784
                $message ?: 'Value "%s" does not contain "%s".',
785
                static::stringify($string),
786
                static::stringify($needle)
787
            );
788
789
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
790
            throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints);
791
        }
792
    }
793
794
    /**
795
     * Assert that value is in array of choices.
796
     *
797
     * @param mixed $value
798
     * @param array $choices
799
     * @param string|null $message
800
     * @param string|null $propertyPath
801
     * @return void
802
     * @throws \Assert\AssertionFailedException
803
     */
804
    public static function choice($value, array $choices, $message = null, $propertyPath = null)
805
    {
806
        if ( ! in_array($value, $choices, true)) {
807
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
808
                $message ?: 'Value "%s" is not an element of the valid values: %s',
809
                static::stringify($value),
810
                implode(", ", array_map('Assert\Assertion::stringify', $choices))
811
            );
812
813
            throw static::createException($value, $message, static::INVALID_CHOICE, $propertyPath, array('choices' => $choices));
814
        }
815
    }
816
817
    /**
818
     * Alias of {@see choice()}
819
     *
820
     * @throws \Assert\AssertionFailedException
821
     */
822
    public static function inArray($value, array $choices, $message = null, $propertyPath = null)
823
    {
824
        static::choice($value, $choices, $message, $propertyPath);
825
    }
826
827
    /**
828
     * Assert that value is numeric.
829
     *
830
     * @param mixed $value
831
     * @param string|null $message
832
     * @param string|null $propertyPath
833
     * @return void
834
     * @throws \Assert\AssertionFailedException
835
     */
836
    public static function numeric($value, $message = null, $propertyPath = null)
837
    {
838
        if ( ! is_numeric($value)) {
839
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
840
                $message ?: 'Value "%s" is not numeric.',
841
                static::stringify($value)
842
            );
843
844
            throw static::createException($value, $message, static::INVALID_NUMERIC, $propertyPath);
845
        }
846
    }
847
848
    /**
849
     * Assert that value is an array.
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 isArray($value, $message = null, $propertyPath = null)
858
    {
859
        if ( ! is_array($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 an array.',
862
                static::stringify($value)
863
            );
864
865
            throw static::createException($value, $message, static::INVALID_ARRAY, $propertyPath);
866
        }
867
    }
868
869
    /**
870
     * Assert that value is an array or a traversable object.
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 isTraversable($value, $message = null, $propertyPath = null)
879
    {
880
        if ( ! is_array($value) && ! $value instanceof \Traversable) {
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 and does not implement Traversable.',
883
                static::stringify($value)
884
            );
885
886
            throw static::createException($value, $message, static::INVALID_TRAVERSABLE, $propertyPath);
887
        }
888
    }
889
890
    /**
891
     * Assert that value is an array or an array-accessible 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 isArrayAccessible($value, $message = null, $propertyPath = null)
900
    {
901
        if ( ! is_array($value) && ! $value instanceof \ArrayAccess) {
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 ArrayAccess.',
904
                static::stringify($value)
905
            );
906
907
            throw static::createException($value, $message, static::INVALID_ARRAY_ACCESSIBLE, $propertyPath);
908
        }
909
    }
910
911
    /**
912
     * Assert that key exists in an array
913
     *
914
     * @param mixed $value
915
     * @param string|integer $key
916
     * @param string|null $message
917
     * @param string|null $propertyPath
918
     * @return void
919
     * @throws \Assert\AssertionFailedException
920
     */
921
    public static function keyExists($value, $key, $message = null, $propertyPath = null)
922
    {
923
        static::isArray($value, $message, $propertyPath);
924
925
        if ( ! array_key_exists($key, $value)) {
926
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
927
                $message ?: 'Array does not contain an element with key "%s"',
928
                static::stringify($key)
929
            );
930
931
            throw static::createException($value, $message, static::INVALID_KEY_EXISTS, $propertyPath, array('key' => $key));
932
        }
933
    }
934
935
    /**
936
     * Assert that key does not exist in an array
937
     *
938
     * @param mixed $value
939
     * @param string|integer $key
940
     * @param string|null $message
941
     * @param string|null $propertyPath
942
     * @return void
943
     * @throws \Assert\AssertionFailedException
944
     */
945
    public static function keyNotExists($value, $key, $message = null, $propertyPath = null)
946
    {
947
        static::isArray($value, $message, $propertyPath);
948
949
        if (array_key_exists($key, $value)) {
950
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
951
                $message ?: 'Array contains an element with key "%s"',
952
                self::stringify($key)
953
            );
954
955
            throw static::createException($value, $message, static::INVALID_KEY_NOT_EXISTS, $propertyPath, array('key' => $key));
956
        }
957
    }
958
959
    /**
960
     * Assert that key exists in an array/array-accessible object using isset()
961
     *
962
     * @param mixed $value
963
     * @param string|integer $key
964
     * @param string|null $message
965
     * @param string|null $propertyPath
966
     * @return void
967
     * @throws \Assert\AssertionFailedException
968
     */
969
    public static function keyIsset($value, $key, $message = null, $propertyPath = null)
970
    {
971
        static::isArrayAccessible($value, $message, $propertyPath);
972
973
        if ( ! isset($value[$key])) {
974
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
975
                $message ?: 'The element with key "%s" was not found',
976
                static::stringify($key)
977
            );
978
979
            throw static::createException($value, $message, static::INVALID_KEY_ISSET, $propertyPath, array('key' => $key));
980
        }
981
    }
982
983
    /**
984
     * Assert that key exists in an array/array-accessible object and it's value is not empty.
985
     *
986
     * @param mixed $value
987
     * @param string|integer $key
988
     * @param string|null $message
989
     * @param string|null $propertyPath
990
     * @return void
991
     * @throws \Assert\AssertionFailedException
992
     */
993
    public static function notEmptyKey($value, $key, $message = null, $propertyPath = null)
994
    {
995
        static::keyIsset($value, $key, $message, $propertyPath);
996
        static::notEmpty($value[$key], $message, $propertyPath);
997
    }
998
999
    /**
1000
     * Assert that value is not blank
1001
     *
1002
     * @param mixed $value
1003
     * @param string|null $message
1004
     * @param string|null $propertyPath
1005
     * @return void
1006
     * @throws \Assert\AssertionFailedException
1007
     */
1008
    public static function notBlank($value, $message = null, $propertyPath = null)
1009
    {
1010
        if (false === $value || (empty($value) && '0' != $value) || (is_string($value) && '' === trim($value))) {
1011
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1012
                $message ?: 'Value "%s" is blank, but was expected to contain a value.',
1013
                static::stringify($value)
1014
            );
1015
1016
            throw static::createException($value, $message, static::INVALID_NOT_BLANK, $propertyPath);
1017
        }
1018
    }
1019
1020
    /**
1021
     * Assert that value is instance of given class-name.
1022
     *
1023
     * @param mixed $value
1024
     * @param string $className
1025
     * @param string|null $message
1026
     * @param string|null $propertyPath
1027
     * @return void
1028
     * @throws \Assert\AssertionFailedException
1029
     */
1030
    public static function isInstanceOf($value, $className, $message = null, $propertyPath = null)
1031
    {
1032
        if ( ! ($value instanceof $className)) {
1033
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1034
                $message ?: 'Class "%s" was expected to be instanceof of "%s" but is not.',
1035
                static::stringify($value),
1036
                $className
1037
            );
1038
1039
            throw static::createException($value, $message, static::INVALID_INSTANCE_OF, $propertyPath, array('class' => $className));
1040
        }
1041
    }
1042
1043
    /**
1044
     * Assert that value is not instance of given class-name.
1045
     *
1046
     * @param mixed $value
1047
     * @param string $className
1048
     * @param string|null $message
1049
     * @param string|null $propertyPath
1050
     * @return void
1051
     * @throws \Assert\AssertionFailedException
1052
     */
1053
    public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null)
1054
    {
1055
        if ($value instanceof $className) {
1056
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1057
                $message ?: 'Class "%s" was not expected to be instanceof of "%s".',
1058
                static::stringify($value),
1059
                $className
1060
            );
1061
1062
            throw static::createException($value, $message, static::INVALID_NOT_INSTANCE_OF, $propertyPath, array('class' => $className));
1063
        }
1064
    }
1065
1066
    /**
1067
     * Assert that value is subclass of given class-name.
1068
     *
1069
     * @param mixed $value
1070
     * @param string $className
1071
     * @param string|null $message
1072
     * @param string|null $propertyPath
1073
     * @return void
1074
     * @throws \Assert\AssertionFailedException
1075
     */
1076
    public static function subclassOf($value, $className, $message = null, $propertyPath = null)
1077
    {
1078
        if ( ! is_subclass_of($value, $className)) {
1079
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1080
                $message ?: 'Class "%s" was expected to be subclass of "%s".',
1081
                static::stringify($value),
1082
                $className
1083
            );
1084
1085
            throw static::createException($value, $message, static::INVALID_SUBCLASS_OF, $propertyPath, array('class' => $className));
1086
        }
1087
    }
1088
1089
    /**
1090
     * Assert that value is in range of numbers.
1091
     *
1092
     * @param mixed $value
1093
     * @param integer $minValue
1094
     * @param integer $maxValue
1095
     * @param string|null $message
1096
     * @param string|null $propertyPath
1097
     * @return void
1098
     * @throws \Assert\AssertionFailedException
1099
     */
1100
    public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null)
1101
    {
1102
        static::numeric($value, $message, $propertyPath);
1103
1104
        if ($value < $minValue || $value > $maxValue) {
1105
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1106
                $message ?: 'Number "%s" was expected to be at least "%d" and at most "%d".',
1107
                static::stringify($value),
1108
                static::stringify($minValue),
1109
                static::stringify($maxValue)
1110
            );
1111
1112
            throw static::createException($value, $message, static::INVALID_RANGE, $propertyPath, array('min' => $minValue, 'max' => $maxValue));
1113
        }
1114
    }
1115
1116
    /**
1117
     * Assert that a value is at least as big as a given limit
1118
     *
1119
     * @param mixed $value
1120
     * @param mixed $minValue
1121
     * @param string|null $message
1122
     * @param string|null $propertyPath
1123
     * @return void
1124
     * @throws \Assert\AssertionFailedException
1125
     */
1126
    public static function min($value, $minValue, $message = null, $propertyPath = null)
1127
    {
1128
        static::numeric($value, $message, $propertyPath);
1129
1130
        if ($value < $minValue) {
1131
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1132
                $message ?: 'Number "%s" was expected to be at least "%s".',
1133
                static::stringify($value),
1134
                static::stringify($minValue)
1135
            );
1136
1137
            throw static::createException($value, $message, static::INVALID_MIN, $propertyPath, array('min' => $minValue));
1138
        }
1139
    }
1140
1141
    /**
1142
     * Assert that a number is smaller as a given limit
1143
     *
1144
     * @param mixed $value
1145
     * @param mixed $maxValue
1146
     * @param string|null $message
1147
     * @param string|null $propertyPath
1148
     * @return void
1149
     * @throws \Assert\AssertionFailedException
1150
     */
1151
    public static function max($value, $maxValue, $message = null, $propertyPath = null)
1152
    {
1153
        static::numeric($value, $message, $propertyPath);
1154
1155
        if ($value > $maxValue) {
1156
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1157
                $message ?: 'Number "%s" was expected to be at most "%s".',
1158
                static::stringify($value),
1159
                static::stringify($maxValue)
1160
            );
1161
1162
            throw static::createException($value, $message, static::INVALID_MAX, $propertyPath, array('max' => $maxValue));
1163
        }
1164
    }
1165
1166
    /**
1167
     * Assert that a file exists
1168
     *
1169
     * @param string $value
1170
     * @param string|null $message
1171
     * @param string|null $propertyPath
1172
     * @return void
1173
     * @throws \Assert\AssertionFailedException
1174
     */
1175
    public static function file($value, $message = null, $propertyPath = null)
1176
    {
1177
        static::string($value, $message, $propertyPath);
1178
        static::notEmpty($value, $message, $propertyPath);
1179
1180
        if ( ! is_file($value)) {
1181
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1182
                $message ?: 'File "%s" was expected to exist.',
1183
                static::stringify($value)
1184
            );
1185
1186
            throw static::createException($value, $message, static::INVALID_FILE, $propertyPath);
1187
        }
1188
    }
1189
1190
    /**
1191
     * Assert that a directory exists
1192
     *
1193
     * @param string $value
1194
     * @param string|null $message
1195
     * @param string|null $propertyPath
1196
     * @return void
1197
     * @throws \Assert\AssertionFailedException
1198
     */
1199
    public static function directory($value, $message = null, $propertyPath = null)
1200
    {
1201
        static::string($value, $message, $propertyPath);
1202
1203
        if ( ! is_dir($value)) {
1204
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1205
                $message ?: 'Path "%s" was expected to be a directory.',
1206
                static::stringify($value)
1207
            );
1208
1209
            throw static::createException($value, $message, static::INVALID_DIRECTORY, $propertyPath);
1210
        }
1211
    }
1212
1213
    /**
1214
     * Assert that the value is something readable
1215
     *
1216
     * @param string $value
1217
     * @param string|null $message
1218
     * @param string|null $propertyPath
1219
     * @return void
1220
     * @throws \Assert\AssertionFailedException
1221
     */
1222
    public static function readable($value, $message = null, $propertyPath = null)
1223
    {
1224
        static::string($value, $message, $propertyPath);
1225
1226
        if ( ! is_readable($value)) {
1227
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1228
                $message ?: 'Path "%s" was expected to be readable.',
1229
                static::stringify($value)
1230
            );
1231
1232
            throw static::createException($value, $message, static::INVALID_READABLE, $propertyPath);
1233
        }
1234
    }
1235
1236
    /**
1237
     * Assert that the value is something writeable
1238
     *
1239
     * @param string $value
1240
     * @param string|null $message
1241
     * @param string|null $propertyPath
1242
     * @return void
1243
     * @throws \Assert\AssertionFailedException
1244
     */
1245
    public static function writeable($value, $message = null, $propertyPath = null)
1246
    {
1247
        static::string($value, $message, $propertyPath);
1248
1249
        if ( ! is_writeable($value)) {
1250
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1251
                $message ?: 'Path "%s" was expected to be writeable.',
1252
                static::stringify($value)
1253
            );
1254
1255
            throw static::createException($value, $message, static::INVALID_WRITEABLE, $propertyPath);
1256
        }
1257
    }
1258
1259
    /**
1260
     * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL).
1261
     *
1262
     * @param mixed $value
1263
     * @param string|null $message
1264
     * @param string|null $propertyPath
1265
     * @return void
1266
     * @throws \Assert\AssertionFailedException
1267
     */
1268
    public static function email($value, $message = null, $propertyPath = null)
1269
    {
1270
        static::string($value, $message, $propertyPath);
1271
1272
        if ( ! filter_var($value, FILTER_VALIDATE_EMAIL)) {
1273
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1274
                $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1275
                static::stringify($value)
1276
            );
1277
1278
            throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1279
        } else {
1280
            $host = substr($value, strpos($value, '@') + 1);
1281
1282
            // Likely not a FQDN, bug in PHP FILTER_VALIDATE_EMAIL prior to PHP 5.3.3
1283
            if (version_compare(PHP_VERSION, '5.3.3', '<') && strpos($host, '.') === false) {
1284
                $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1285
                    $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1286
                    static::stringify($value)
1287
                );
1288
1289
                throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1290
            }
1291
        }
1292
    }
1293
1294
    /**
1295
     * Assert that value is an URL.
1296
     *
1297
     * This code snipped was taken from the Symfony project and modified to the special demands of this method.
1298
     *
1299
     * @param mixed $value
1300
     * @param string|null $message
1301
     * @param string|null $propertyPath
1302
     * @return void
1303
     * @throws \Assert\AssertionFailedException
1304
     *
1305
     *
1306
     * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php
1307
     * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php
1308
     */
1309
    public static function url($value, $message = null, $propertyPath = null)
1310
    {
1311
        static::string($value, $message, $propertyPath);
1312
1313
        $protocols = array('http', 'https');
1314
1315
        $pattern = '~^
1316
            (%s)://                                 # protocol
1317
            (
1318
                ([\pL\pN\pS-]+\.)+[\pL]+                   # a domain name
1319
                    |                                     #  or
1320
                \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}      # a IP address
1321
                    |                                     #  or
1322
                \[
1323
                    (?:(?:(?:(?:(?:(?:(?:[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})))?::))))
1324
                \]  # a IPv6 address
1325
            )
1326
            (:[0-9]+)?                              # a port (optional)
1327
            (/?|/\S+)                               # a /, nothing or a / with something
1328
        $~ixu';
1329
1330
        $pattern = sprintf($pattern, implode('|', $protocols));
1331
1332
        if (!preg_match($pattern, $value)) {
1333
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1334
                $message ?: 'Value "%s" was expected to be a valid URL starting with http or https',
1335
                static::stringify($value)
1336
            );
1337
1338
            throw static::createException($value, $message, static::INVALID_URL, $propertyPath);
1339
        }
1340
1341
    }
1342
1343
    /**
1344
     * Assert that value is alphanumeric.
1345
     *
1346
     * @param mixed $value
1347
     * @param string|null $message
1348
     * @param string|null $propertyPath
1349
     * @return void
1350
     * @throws \Assert\AssertionFailedException
1351
     */
1352
    public static function alnum($value, $message = null, $propertyPath = null)
1353
    {
1354
        try {
1355
            static::regex($value, '(^([a-zA-Z]{1}[a-zA-Z0-9]*)$)', $message, $propertyPath);
1356
        } catch(AssertionFailedException $e) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after CATCH keyword; 0 found
Loading history...
1357
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1358
                $message ?: 'Value "%s" is not alphanumeric, starting with letters and containing only letters and numbers.',
1359
                static::stringify($value)
1360
            );
1361
1362
            throw static::createException($value, $message, static::INVALID_ALNUM, $propertyPath);
1363
        }
1364
    }
1365
1366
    /**
1367
     * Assert that the value is boolean True.
1368
     *
1369
     * @param mixed $value
1370
     * @param string|null $message
1371
     * @param string|null $propertyPath
1372
     * @return void
1373
     * @throws \Assert\AssertionFailedException
1374
     */
1375
    public static function true($value, $message = null, $propertyPath = null)
1376
    {
1377
        if ($value !== true) {
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 TRUE.',
1380
                static::stringify($value)
1381
            );
1382
1383
            throw static::createException($value, $message, static::INVALID_TRUE, $propertyPath);
1384
        }
1385
    }
1386
1387
    /**
1388
     * Assert that the value is boolean False.
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 false($value, $message = null, $propertyPath = null)
1397
    {
1398
        if ($value !== false) {
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 FALSE.',
1401
                static::stringify($value)
1402
            );
1403
1404
            throw static::createException($value, $message, static::INVALID_FALSE, $propertyPath);
1405
        }
1406
    }
1407
1408
    /**
1409
     * Assert that the class exists.
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 classExists($value, $message = null, $propertyPath = null)
1418
    {
1419
        if ( ! class_exists($value)) {
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 ?: 'Class "%s" does not exist.',
1422
                static::stringify($value)
1423
            );
1424
1425
            throw static::createException($value, $message, static::INVALID_CLASS, $propertyPath);
1426
        }
1427
    }
1428
1429
    /**
1430
     * Assert that the interface 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 interfaceExists($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 ?: 'Interface "%s" does not exist.',
1443
                static::stringify($value)
1444
            );
1445
1446
            throw static::createException($value, $message, static::INVALID_INTERFACE, $propertyPath);
1447
        }
1448
    }
1449
1450
    /**
1451
     * Assert that the class implements the interface
1452
     *
1453
     * @param mixed $class
1454
     * @param string $interfaceName
1455
     * @param string|null $message
1456
     * @param string|null $propertyPath
1457
     * @return void
1458
     * @throws \Assert\AssertionFailedException
1459
     */
1460
    public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null)
1461
    {
1462
        $reflection = new \ReflectionClass($class);
1463
        if ( ! $reflection->implementsInterface($interfaceName)) {
1464
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1465
                $message ?: 'Class "%s" does not implement interface "%s".',
1466
                static::stringify($class),
1467
                static::stringify($interfaceName)
1468
            );
1469
1470
            throw static::createException($class, $message, static::INTERFACE_NOT_IMPLEMENTED, $propertyPath, array('interface' => $interfaceName));
1471
        }
1472
    }
1473
1474
    /**
1475
     * Assert that the given string is a valid json string.
1476
     *
1477
     * NOTICE:
1478
     * Since this does a json_decode to determine its validity
1479
     * you probably should consider, when using the variable
1480
     * content afterwards, just to decode and check for yourself instead
1481
     * of using this assertion.
1482
     *
1483
     * @param mixed $value
1484
     * @param string|null $message
1485
     * @param string|null $propertyPath
1486
     * @return void
1487
     * @throws \Assert\AssertionFailedException
1488
     */
1489
    public static function isJsonString($value, $message = null, $propertyPath = null)
1490
    {
1491
        if (null === json_decode($value) && JSON_ERROR_NONE !== json_last_error()) {
1492
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1493
                $message ?: 'Value "%s" is not a valid JSON string.',
1494
                static::stringify($value)
1495
            );
1496
1497
            throw static::createException($value, $message, static::INVALID_JSON_STRING, $propertyPath);
1498
        }
1499
    }
1500
1501
    /**
1502
     * Assert that the given string is a valid UUID
1503
     *
1504
     * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed.
1505
     *
1506
     * @param string $value
1507
     * @param string|null $message
1508
     * @param string|null $propertyPath
1509
     * @return void
1510
     * @throws \Assert\AssertionFailedException
1511
     */
1512
    public static function uuid($value, $message = null, $propertyPath = null)
1513
    {
1514
        $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...
1515
1516
        if ($value === '00000000-0000-0000-0000-000000000000') {
1517
            return;
1518
        }
1519
1520
        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)) {
1521
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1522
                $message ?: 'Value "%s" is not a valid UUID.',
1523
                static::stringify($value)
1524
            );
1525
1526
            throw static::createException($value, $message, static::INVALID_UUID, $propertyPath);
1527
        }
1528
    }
1529
1530
    /**
1531
     * Assert that the given string is a valid E164 Phone Number
1532
     *
1533
     * @link https://en.wikipedia.org/wiki/E.164
1534
     *
1535
     * @param string $value
1536
     * @param string|null $message
1537
     * @param string|null $propertyPath
1538
     * @return void
1539
     * @throws \Assert\AssertionFailedException
1540
     */
1541
    public static function e164($value, $message = null, $propertyPath = null)
1542
    {
1543
        if (!preg_match('/^\+?[1-9]\d{1,14}$/', $value)) {
1544
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1545
                $message ?: 'Value "%s" is not a valid E164.',
1546
                static::stringify($value)
1547
            );
1548
1549
            throw static::createException($value, $message, static::INVALID_E164, $propertyPath);
1550
        }
1551
    }
1552
1553
    /**
1554
     * Assert that the count of countable is equal to count.
1555
     *
1556
     * @param array|\Countable $countable
1557
     * @param int              $count
1558
     * @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...
1559
     * @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...
1560
     * @return void
1561
     * @throws \Assert\AssertionFailedException
1562
     */
1563
    public static function count($countable, $count, $message = null, $propertyPath = null)
1564
    {
1565
        if ($count !== count($countable)) {
1566
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1567
                $message ?: 'List does not contain exactly "%d" elements.',
1568
                static::stringify($count)
1569
            );
1570
1571
            throw static::createException($countable, $message, static::INVALID_COUNT, $propertyPath, array('count' => $count));
1572
        }
1573
    }
1574
1575
    /**
1576
     * static call handler to implement:
1577
     *  - "null or assertion" delegation
1578
     *  - "all" delegation
1579
     */
1580
    public static function __callStatic($method, $args)
1581
    {
1582
        if (strpos($method, "nullOr") === 0) {
1583
            if ( ! array_key_exists(0, $args)) {
1584
                throw new BadMethodCallException("Missing the first argument.");
1585
            }
1586
1587
            if ($args[0] === null) {
1588
                return;
1589
            }
1590
1591
            $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...
1592
1593
            return call_user_func_array(array(get_called_class(), $method), $args);
1594
        }
1595
1596
        if (strpos($method, "all") === 0) {
1597
            if ( ! array_key_exists(0, $args)) {
1598
                throw new BadMethodCallException("Missing the first argument.");
1599
            }
1600
1601
            static::isTraversable($args[0]);
1602
1603
            $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...
1604
            $values      = array_shift($args);
1605
            $calledClass = get_called_class();
1606
1607
            foreach ($values as $value) {
1608
                call_user_func_array(array($calledClass, $method), array_merge(array($value), $args));
1609
            }
1610
1611
            return;
1612
        }
1613
1614
        throw new BadMethodCallException("No assertion Assertion#" . $method . " exists.");
1615
    }
1616
1617
    /**
1618
     * Determines if the values array has every choice as key and that this choice has content.
1619
     *
1620
     * @param array $values
1621
     * @param array $choices
1622
     * @param null  $message
1623
     * @param null  $propertyPath
1624
     */
1625
    public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null)
1626
    {
1627
        self::notEmpty($values, $message, $propertyPath);
1628
1629
        foreach ($choices as $choice) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1630
1631
            self::notEmptyKey($values, $choice, $message, $propertyPath);
1632
        }
1633
    }
1634
1635
    /**
1636
     * Determines that the named method is defined in the provided object.
1637
     *
1638
     * @param string $value
1639
     * @param mixed  $object
1640
     * @param null   $message
1641
     * @param null   $propertyPath
1642
     *
1643
     * @throws
1644
     */
1645
    public static function methodExists($value, $object, $message = null, $propertyPath = null)
1646
    {
1647
        self::isObject($object, $message, $propertyPath);
1648
1649
        if (!method_exists($object, $value)) {
1650
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1651
                $message ?: 'Expected "%s" does not exist in provided object.',
1652
                static::stringify($value)
1653
            );
1654
1655
            throw static::createException($value, $message, static::INVALID_METHOD, $propertyPath);
1656
        }
1657
    }
1658
1659
    /**
1660
     * Determines that the provided value is an object.
1661
     *
1662
     * @param mixed $value
1663
     * @param null  $message
1664
     * @param null  $propertyPath
1665
     */
1666
    public static function isObject($value, $message = null, $propertyPath = null)
1667
    {
1668
        if (!is_object($value)) {
1669
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1670
                $message ?: 'Provided "%s" is not a valid object.',
1671
                static::stringify($value)
1672
            );
1673
1674
            throw static::createException($value, $message, static::INVALID_OBJECT, $propertyPath);
1675
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1676
        }
1677
    }
1678
1679
    /**
1680
     * Determines if the value is less than given limit.
1681
     *
1682
     * @param mixed $value
1683
     * @param mixed $limit
1684
     * @param null  $message
1685
     * @param null  $propertyPath
1686
     */
1687
    public static function lessThan($value, $limit, $message = null, $propertyPath = null)
1688
    {
1689
        if ($value >= $limit) {
1690
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1691
                $message ?: 'Provided "%s" is not less than "%s".',
1692
                static::stringify($value),
1693
                static::stringify($limit)
1694
            );
1695
1696
            throw static::createException($value, $message, static::INVALID_LESS, $propertyPath);
1697
        }
1698
    }
1699
1700
    /**
1701
     * Determines if the value is less or than given limit.
1702
     *
1703
     * @param mixed $value
1704
     * @param mixed $limit
1705
     * @param null  $message
1706
     * @param null  $propertyPath
1707
     */
1708
    public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1709
    {
1710
        if ($value > $limit) {
1711
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1712
                $message ?: 'Provided "%s" is not less or equal than "%s".',
1713
                static::stringify($value),
1714
                static::stringify($limit)
1715
            );
1716
1717
            throw static::createException($value, $message, static::INVALID_LESS_OR_EQUAL, $propertyPath);
1718
        }
1719
    }
1720
1721
    /**
1722
     * Determines if the value is greater than given limit.
1723
     *
1724
     * @param mixed $value
1725
     * @param mixed $limit
1726
     * @param null  $message
1727
     * @param null  $propertyPath
1728
     */
1729
    public static function greaterThan($value, $limit, $message = null, $propertyPath = null)
1730
    {
1731
        if ($value <= $limit) {
1732
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1733
                $message ?: 'Provided "%s" is not greater than "%s".',
1734
                static::stringify($value),
1735
                static::stringify($limit)
1736
            );
1737
1738
            throw static::createException($value, $message, static::INVALID_GREATER, $propertyPath);
1739
        }
1740
    }
1741
1742
    /**
1743
     * Determines if the value is greater or equal than given limit.
1744
     *
1745
     * @param mixed $value
1746
     * @param mixed $limit
1747
     * @param null  $message
1748
     * @param null  $propertyPath
1749
     */
1750
    public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1751
    {
1752
        if ($value < $limit) {
1753
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1754
                $message ?: 'Provided "%s" is not greater or equal than "%s".',
1755
                static::stringify($value),
1756
                static::stringify($limit)
1757
            );
1758
1759
            throw static::createException($value, $message, static::INVALID_GREATER_OR_EQUAL, $propertyPath);
1760
        }
1761
    }
1762
1763
    /**
1764
     * Assert that date is valid and corresponds to the given format.
1765
     *
1766
     * @param string      $value
1767
     * @param string      $format supports all of the options date(), except for the following:
1768
     *                            N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r.
1769
     * @param string|null $message
1770
     * @param string|null $propertyPath
1771
     *
1772
     * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters
1773
     */
1774
     public static function date($value, $format, $message = null, $propertyPath = null)
1775
     {
1776
         static::string($value, $message, $propertyPath);
1777
         static::string($format, $message, $propertyPath);
1778
1779
         $dateTime = \DateTime::createFromFormat($format, $value);
1780
1781
         if (false === $dateTime || $value !== $dateTime->format($format)) {
1782
             $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1783
                 $message ?: 'Date "%s" is invalid or does not match format "%s".',
1784
                 static::stringify($value),
1785
                 static::stringify($format)
1786
             );
1787
1788
             throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format));
1789
         }
1790
     }
1791
1792
    /**
1793
     * Determines that the provided value is callable.
1794
     *
1795
     * @param mixed $value
1796
     * @param null $message
1797
     * @param null $propertyPath
1798
     */
1799
    public static function isCallable($value, $message = null, $propertyPath = null)
1800
    {
1801
        if (!is_callable($value)) {
1802
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1803
                $message ?: 'Provided "%s" is not a callable.',
1804
                static::stringify($value)
1805
            );
1806
1807
            throw static::createException($value, $message, static::INVALID_CALLABLE, $propertyPath);
1808
        }
1809
    }
1810
1811
    /**
1812
     * Assert that the provided value is valid according to a callback.
1813
     *
1814
     * If the callback returns `false` the assertion will fail.
1815
     *
1816
     * @param mixed $value
1817
     * @param callable $callback
1818
     * @param string|null $message
1819
     * @param string|null $propertyPath
1820
     */
1821
    public static function satisfy($value, $callback, $message = null, $propertyPath = null)
1822
    {
1823
        static::isCallable($callback);
1824
1825
        if (call_user_func($callback, $value) === false) {
1826
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1827
                $message ?: 'Provided "%s" is invalid according to custom rule.',
1828
                static::stringify($value)
1829
            );
1830
1831
            throw static::createException($value, $message, static::INVALID_SATISFY, $propertyPath);
1832
        }
1833
    }
1834
1835
    /**
1836
     * Make a string version of a value.
1837
     *
1838
     * @param mixed $value
1839
     * @return string
1840
     */
1841
    protected static function stringify($value)
1842
    {
1843
        if (is_bool($value)) {
1844
            return $value ? '<TRUE>' : '<FALSE>';
1845
        }
1846
1847
        if (is_scalar($value)) {
1848
            $val = (string)$value;
1849
1850
            if (strlen($val) > 100) {
1851
                $val = substr($val, 0, 97) . '...';
1852
            }
1853
1854
            return $val;
1855
        }
1856
1857
        if (is_array($value)) {
1858
            return '<ARRAY>';
1859
        }
1860
1861
        if (is_object($value)) {
1862
            return get_class($value);
1863
        }
1864
1865
        if (is_resource($value)) {
1866
            return '<RESOURCE>';
1867
        }
1868
1869
        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...
1870
            return '<NULL>';
1871
        }
1872
1873
        return 'unknown';
1874
    }
1875
}
1876
1877