Completed
Push — master ( 2f56eb...d40050 )
by Richard
9s
created

Assertion::notNull()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 3
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Assert
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this package in the file LICENSE.txt.
10
 * If you did not receive a copy of the license and are unable to
11
 * obtain it through the world-wide-web, please send an email
12
 * to [email protected] so I can send you a copy immediately.
13
 */
14
15
namespace Assert;
16
17
use BadMethodCallException;
18
19
/**
20
 * Assert library
21
 *
22
 * @author Benjamin Eberlei <[email protected]>
23
 *
24
 * @method static bool allAlnum($value, $message = null, $propertyPath = null) Assert that value is alphanumeric for all values.
25
 * @method static bool allBetween($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit for all values.
26
 * @method static bool allBetweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit for all values.
27
 * @method static bool allBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string length is between min,max lengths for all values.
28
 * @method static bool allBoolean($value, $message = null, $propertyPath = null) Assert that value is php boolean for all values.
29
 * @method static bool allChoice($value, $choices, $message = null, $propertyPath = null) Assert that value is in array of choices for all values.
30
 * @method static bool 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.
31
 * @method static bool allClassExists($value, $message = null, $propertyPath = null) Assert that the class exists for all values.
32
 * @method static bool allContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string contains a sequence of chars for all values.
33
 * @method static bool allCount($countable, $count, $message = null, $propertyPath = null) Assert that the count of countable is equal to count for all values.
34
 * @method static bool allDate($value, $format, $message = null, $propertyPath = null) Assert that date is valid and corresponds to the given format for all values.
35
 * @method static bool allDefined($constant, $message = null, $propertyPath = null) Assert that a constant is defined for all values.
36
 * @method static bool allDigit($value, $message = null, $propertyPath = null) Validates if an integer or integerish is a digit for all values.
37
 * @method static bool allDirectory($value, $message = null, $propertyPath = null) Assert that a directory exists for all values.
38
 * @method static bool allE164($value, $message = null, $propertyPath = null) Assert that the given string is a valid E164 Phone Number for all values.
39
 * @method static bool allEmail($value, $message = null, $propertyPath = null) Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL) for all values.
40
 * @method static bool allEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string ends with a sequence of chars for all values.
41
 * @method static bool allEq($value, $value2, $message = null, $propertyPath = null) Assert that two values are equal (using == ) for all values.
42
 * @method static bool allExtensionLoaded($value, $message = null, $propertyPath = null) Assert that extension is loaded for all values.
43
 * @method static bool allFalse($value, $message = null, $propertyPath = null) Assert that the value is boolean False for all values.
44
 * @method static bool allFile($value, $message = null, $propertyPath = null) Assert that a file exists for all values.
45
 * @method static bool allFloat($value, $message = null, $propertyPath = null) Assert that value is a php float for all values.
46
 * @method static bool allGreaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is greater or equal than given limit for all values.
47
 * @method static bool allGreaterThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is greater than given limit for all values.
48
 * @method static bool allImplementsInterface($class, $interfaceName, $message = null, $propertyPath = null) Assert that the class implements the interface for all values.
49
 * @method static bool allInArray($value, $choices, $message = null, $propertyPath = null) Alias of {@see choice()} for all values.
50
 * @method static bool allInteger($value, $message = null, $propertyPath = null) Assert that value is a php integer for all values.
51
 * @method static bool allIntegerish($value, $message = null, $propertyPath = null) Assert that value is a php integer'ish for all values.
52
 * @method static bool allInterfaceExists($value, $message = null, $propertyPath = null) Assert that the interface exists for all values.
53
 * @method static bool allIp($value, $flag = null, $message = null, $propertyPath = null) Assert that value is an IPv4 or IPv6 address for all values.
54
 * @method static bool allIpv4($value, $flag = null, $message = null, $propertyPath = null) Assert that value is an IPv4 address for all values.
55
 * @method static bool allIpv6($value, $flag = null, $message = null, $propertyPath = null) Assert that value is an IPv6 address for all values.
56
 * @method static bool allIsArray($value, $message = null, $propertyPath = null) Assert that value is an array for all values.
57
 * @method static bool allIsArrayAccessible($value, $message = null, $propertyPath = null) Assert that value is an array or an array-accessible object for all values.
58
 * @method static bool allIsCallable($value, $message = null, $propertyPath = null) Determines that the provided value is callable for all values.
59
 * @method static bool allIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is instance of given class-name for all values.
60
 * @method static bool allIsJsonString($value, $message = null, $propertyPath = null) Assert that the given string is a valid json string for all values.
61
 * @method static bool allIsObject($value, $message = null, $propertyPath = null) Determines that the provided value is an object for all values.
62
 * @method static bool allIsTraversable($value, $message = null, $propertyPath = null) Assert that value is an array or a traversable object for all values.
63
 * @method static bool allKeyExists($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array for all values.
64
 * @method static bool allKeyIsset($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object using isset() for all values.
65
 * @method static bool allKeyNotExists($value, $key, $message = null, $propertyPath = null) Assert that key does not exist in an array for all values.
66
 * @method static bool allLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string has a given length for all values.
67
 * @method static bool allLessOrEqualThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is less or than given limit for all values.
68
 * @method static bool allLessThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is less than given limit for all values.
69
 * @method static bool allMax($value, $maxValue, $message = null, $propertyPath = null) Assert that a number is smaller as a given limit for all values.
70
 * @method static bool allMaxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string value is not longer than $maxLength chars for all values.
71
 * @method static bool allMethodExists($value, $object, $message = null, $propertyPath = null) Determines that the named method is defined in the provided object for all values.
72
 * @method static bool allMin($value, $minValue, $message = null, $propertyPath = null) Assert that a value is at least as big as a given limit for all values.
73
 * @method static bool allMinLength($value, $minLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that a string is at least $minLength chars long for all values.
74
 * @method static bool allNoContent($value, $message = null, $propertyPath = null) Assert that value is empty for all values.
75
 * @method static bool allNotBlank($value, $message = null, $propertyPath = null) Assert that value is not blank for all values.
76
 * @method static bool allNotEmpty($value, $message = null, $propertyPath = null) Assert that value is not empty for all values.
77
 * @method static bool allNotEmptyKey($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty for all values.
78
 * @method static bool allNotEq($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ) for all values.
79
 * @method static bool allNotInArray($value, $choices, $message = null, $propertyPath = null) Assert that value is not in array of choices for all values.
80
 * @method static bool allNotIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name for all values.
81
 * @method static bool allNotNull($value, $message = null, $propertyPath = null) Assert that value is not null for all values.
82
 * @method static bool allNotSame($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ) for all values.
83
 * @method static bool allNull($value, $message = null, $propertyPath = null) Assert that value is null for all values.
84
 * @method static bool allNumeric($value, $message = null, $propertyPath = null) Assert that value is numeric for all values.
85
 * @method static bool allRange($value, $minValue, $maxValue, $message = null, $propertyPath = null) Assert that value is in range of numbers for all values.
86
 * @method static bool allReadable($value, $message = null, $propertyPath = null) Assert that the value is something readable for all values.
87
 * @method static bool allRegex($value, $pattern, $message = null, $propertyPath = null) Assert that value matches a regex for all values.
88
 * @method static bool allSame($value, $value2, $message = null, $propertyPath = null) Assert that two values are the same (using ===) for all values.
89
 * @method static bool allSatisfy($value, $callback, $message = null, $propertyPath = null) Assert that the provided value is valid according to a callback for all values.
90
 * @method static bool allScalar($value, $message = null, $propertyPath = null) Assert that value is a PHP scalar for all values.
91
 * @method static bool allStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string starts with a sequence of chars for all values.
92
 * @method static bool allString($value, $message = null, $propertyPath = null) Assert that value is a string for all values.
93
 * @method static bool allSubclassOf($value, $className, $message = null, $propertyPath = null) Assert that value is subclass of given class-name for all values.
94
 * @method static bool allTrue($value, $message = null, $propertyPath = null) Assert that the value is boolean True for all values.
95
 * @method static bool allUrl($value, $message = null, $propertyPath = null) Assert that value is an URL for all values.
96
 * @method static bool allUuid($value, $message = null, $propertyPath = null) Assert that the given string is a valid UUID for all values.
97
 * @method static bool allWriteable($value, $message = null, $propertyPath = null) Assert that the value is something writeable for all values.
98
 * @method static bool nullOrAlnum($value, $message = null, $propertyPath = null) Assert that value is alphanumeric or that the value is null.
99
 * @method static bool nullOrBetween($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit or that the value is null.
100
 * @method static bool nullOrBetweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit or that the value is null.
101
 * @method static bool 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.
102
 * @method static bool nullOrBoolean($value, $message = null, $propertyPath = null) Assert that value is php boolean or that the value is null.
103
 * @method static bool nullOrChoice($value, $choices, $message = null, $propertyPath = null) Assert that value is in array of choices or that the value is null.
104
 * @method static bool 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.
105
 * @method static bool nullOrClassExists($value, $message = null, $propertyPath = null) Assert that the class exists or that the value is null.
106
 * @method static bool nullOrContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string contains a sequence of chars or that the value is null.
107
 * @method static bool nullOrCount($countable, $count, $message = null, $propertyPath = null) Assert that the count of countable is equal to count or that the value is null.
108
 * @method static bool nullOrDate($value, $format, $message = null, $propertyPath = null) Assert that date is valid and corresponds to the given format or that the value is null.
109
 * @method static bool nullOrDefined($constant, $message = null, $propertyPath = null) Assert that a constant is defined or that the value is null.
110
 * @method static bool nullOrDigit($value, $message = null, $propertyPath = null) Validates if an integer or integerish is a digit or that the value is null.
111
 * @method static bool nullOrDirectory($value, $message = null, $propertyPath = null) Assert that a directory exists or that the value is null.
112
 * @method static bool nullOrE164($value, $message = null, $propertyPath = null) Assert that the given string is a valid E164 Phone Number or that the value is null.
113
 * @method static bool 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.
114
 * @method static bool nullOrEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string ends with a sequence of chars or that the value is null.
115
 * @method static bool nullOrEq($value, $value2, $message = null, $propertyPath = null) Assert that two values are equal (using == ) or that the value is null.
116
 * @method static bool nullOrExtensionLoaded($value, $message = null, $propertyPath = null) Assert that extension is loaded or that the value is null.
117
 * @method static bool nullOrFalse($value, $message = null, $propertyPath = null) Assert that the value is boolean False or that the value is null.
118
 * @method static bool nullOrFile($value, $message = null, $propertyPath = null) Assert that a file exists or that the value is null.
119
 * @method static bool nullOrFloat($value, $message = null, $propertyPath = null) Assert that value is a php float or that the value is null.
120
 * @method static bool nullOrGreaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is greater or equal than given limit or that the value is null.
121
 * @method static bool nullOrGreaterThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is greater than given limit or that the value is null.
122
 * @method static bool nullOrImplementsInterface($class, $interfaceName, $message = null, $propertyPath = null) Assert that the class implements the interface or that the value is null.
123
 * @method static bool nullOrInArray($value, $choices, $message = null, $propertyPath = null) Alias of {@see choice()} or that the value is null.
124
 * @method static bool nullOrInteger($value, $message = null, $propertyPath = null) Assert that value is a php integer or that the value is null.
125
 * @method static bool nullOrIntegerish($value, $message = null, $propertyPath = null) Assert that value is a php integer'ish or that the value is null.
126
 * @method static bool nullOrInterfaceExists($value, $message = null, $propertyPath = null) Assert that the interface exists or that the value is null.
127
 * @method static bool nullOrIp($value, $flag = null, $message = null, $propertyPath = null) Assert that value is an IPv4 or IPv6 address or that the value is null.
128
 * @method static bool nullOrIpv4($value, $flag = null, $message = null, $propertyPath = null) Assert that value is an IPv4 address or that the value is null.
129
 * @method static bool nullOrIpv6($value, $flag = null, $message = null, $propertyPath = null) Assert that value is an IPv6 address or that the value is null.
130
 * @method static bool nullOrIsArray($value, $message = null, $propertyPath = null) Assert that value is an array or that the value is null.
131
 * @method static bool nullOrIsArrayAccessible($value, $message = null, $propertyPath = null) Assert that value is an array or an array-accessible object or that the value is null.
132
 * @method static bool nullOrIsCallable($value, $message = null, $propertyPath = null) Determines that the provided value is callable or that the value is null.
133
 * @method static bool nullOrIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is instance of given class-name or that the value is null.
134
 * @method static bool nullOrIsJsonString($value, $message = null, $propertyPath = null) Assert that the given string is a valid json string or that the value is null.
135
 * @method static bool nullOrIsObject($value, $message = null, $propertyPath = null) Determines that the provided value is an object or that the value is null.
136
 * @method static bool nullOrIsTraversable($value, $message = null, $propertyPath = null) Assert that value is an array or a traversable object or that the value is null.
137
 * @method static bool nullOrKeyExists($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array or that the value is null.
138
 * @method static bool 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.
139
 * @method static bool nullOrKeyNotExists($value, $key, $message = null, $propertyPath = null) Assert that key does not exist in an array or that the value is null.
140
 * @method static bool nullOrLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string has a given length or that the value is null.
141
 * @method static bool nullOrLessOrEqualThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is less or than given limit or that the value is null.
142
 * @method static bool nullOrLessThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is less than given limit or that the value is null.
143
 * @method static bool nullOrMax($value, $maxValue, $message = null, $propertyPath = null) Assert that a number is smaller as a given limit or that the value is null.
144
 * @method static bool 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.
145
 * @method static bool nullOrMethodExists($value, $object, $message = null, $propertyPath = null) Determines that the named method is defined in the provided object or that the value is null.
146
 * @method static bool 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.
147
 * @method static bool 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.
148
 * @method static bool nullOrNoContent($value, $message = null, $propertyPath = null) Assert that value is empty or that the value is null.
149
 * @method static bool nullOrNotBlank($value, $message = null, $propertyPath = null) Assert that value is not blank or that the value is null.
150
 * @method static bool nullOrNotEmpty($value, $message = null, $propertyPath = null) Assert that value is not empty or that the value is null.
151
 * @method static bool nullOrNotEmptyKey($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty or that the value is null.
152
 * @method static bool nullOrNotEq($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ) or that the value is null.
153
 * @method static bool nullOrNotInArray($value, $choices, $message = null, $propertyPath = null) Assert that value is not in array of choices or that the value is null.
154
 * @method static bool nullOrNotIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name or that the value is null.
155
 * @method static bool nullOrNotNull($value, $message = null, $propertyPath = null) Assert that value is not null or that the value is null.
156
 * @method static bool nullOrNotSame($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ) or that the value is null.
157
 * @method static bool nullOrNull($value, $message = null, $propertyPath = null) Assert that value is null or that the value is null.
158
 * @method static bool nullOrNumeric($value, $message = null, $propertyPath = null) Assert that value is numeric or that the value is null.
159
 * @method static bool nullOrRange($value, $minValue, $maxValue, $message = null, $propertyPath = null) Assert that value is in range of numbers or that the value is null.
160
 * @method static bool nullOrReadable($value, $message = null, $propertyPath = null) Assert that the value is something readable or that the value is null.
161
 * @method static bool nullOrRegex($value, $pattern, $message = null, $propertyPath = null) Assert that value matches a regex or that the value is null.
162
 * @method static bool nullOrSame($value, $value2, $message = null, $propertyPath = null) Assert that two values are the same (using ===) or that the value is null.
163
 * @method static bool nullOrSatisfy($value, $callback, $message = null, $propertyPath = null) Assert that the provided value is valid according to a callback or that the value is null.
164
 * @method static bool nullOrScalar($value, $message = null, $propertyPath = null) Assert that value is a PHP scalar or that the value is null.
165
 * @method static bool nullOrStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string starts with a sequence of chars or that the value is null.
166
 * @method static bool nullOrString($value, $message = null, $propertyPath = null) Assert that value is a string or that the value is null.
167
 * @method static bool nullOrSubclassOf($value, $className, $message = null, $propertyPath = null) Assert that value is subclass of given class-name or that the value is null.
168
 * @method static bool nullOrTrue($value, $message = null, $propertyPath = null) Assert that the value is boolean True or that the value is null.
169
 * @method static bool nullOrUrl($value, $message = null, $propertyPath = null) Assert that value is an URL or that the value is null.
170
 * @method static bool nullOrUuid($value, $message = null, $propertyPath = null) Assert that the given string is a valid UUID or that the value is null.
171
 * @method static bool nullOrWriteable($value, $message = null, $propertyPath = null) Assert that the value is something writeable or that the value is null.
172
 */
173
class Assertion
174
{
175
    const INVALID_FLOAT             = 9;
176
    const INVALID_INTEGER           = 10;
177
    const INVALID_DIGIT             = 11;
178
    const INVALID_INTEGERISH        = 12;
179
    const INVALID_BOOLEAN           = 13;
180
    const VALUE_EMPTY               = 14;
181
    const VALUE_NULL                = 15;
182
    const VALUE_NOT_NULL            = 25;
183
    const INVALID_STRING            = 16;
184
    const INVALID_REGEX             = 17;
185
    const INVALID_MIN_LENGTH        = 18;
186
    const INVALID_MAX_LENGTH        = 19;
187
    const INVALID_STRING_START      = 20;
188
    const INVALID_STRING_CONTAINS   = 21;
189
    const INVALID_CHOICE            = 22;
190
    const INVALID_NUMERIC           = 23;
191
    const INVALID_ARRAY             = 24;
192
    const INVALID_KEY_EXISTS        = 26;
193
    const INVALID_NOT_BLANK         = 27;
194
    const INVALID_INSTANCE_OF       = 28;
195
    const INVALID_SUBCLASS_OF       = 29;
196
    const INVALID_RANGE             = 30;
197
    const INVALID_ALNUM             = 31;
198
    const INVALID_TRUE              = 32;
199
    const INVALID_EQ                = 33;
200
    const INVALID_SAME              = 34;
201
    const INVALID_MIN               = 35;
202
    const INVALID_MAX               = 36;
203
    const INVALID_LENGTH            = 37;
204
    const INVALID_FALSE             = 38;
205
    const INVALID_STRING_END        = 39;
206
    const INVALID_UUID              = 40;
207
    const INVALID_COUNT             = 41;
208
    const INVALID_NOT_EQ            = 42;
209
    const INVALID_NOT_SAME          = 43;
210
    const INVALID_TRAVERSABLE       = 44;
211
    const INVALID_ARRAY_ACCESSIBLE  = 45;
212
    const INVALID_KEY_ISSET         = 46;
213
    const INVALID_VALUE_IN_ARRAY    = 47;
214
    const INVALID_E164              = 48;
215
    const INVALID_DIRECTORY         = 101;
216
    const INVALID_FILE              = 102;
217
    const INVALID_READABLE          = 103;
218
    const INVALID_WRITEABLE         = 104;
219
    const INVALID_CLASS             = 105;
220
    const INVALID_INTERFACE         = 106;
221
    const INVALID_EMAIL             = 201;
222
    const INTERFACE_NOT_IMPLEMENTED = 202;
223
    const INVALID_URL               = 203;
224
    const INVALID_NOT_INSTANCE_OF   = 204;
225
    const VALUE_NOT_EMPTY           = 205;
226
    const INVALID_JSON_STRING       = 206;
227
    const INVALID_OBJECT            = 207;
228
    const INVALID_METHOD            = 208;
229
    const INVALID_SCALAR            = 209;
230
    const INVALID_LESS              = 210;
231
    const INVALID_LESS_OR_EQUAL     = 211;
232
    const INVALID_GREATER           = 212;
233
    const INVALID_GREATER_OR_EQUAL  = 213;
234
    const INVALID_DATE              = 214;
235
    const INVALID_CALLABLE          = 215;
236
    const INVALID_KEY_NOT_EXISTS    = 216;
237
    const INVALID_SATISFY           = 217;
238
    const INVALID_IP                = 218;
239
    const INVALID_BETWEEN           = 219;
240
    const INVALID_BETWEEN_EXCLUSIVE = 220;
241
    const INVALID_EXTENSION         = 222;
242
    const INVALID_CONSTANT          = 221;
243
244
    /**
245
     * Exception to throw when an assertion failed.
246
     *
247
     * @var string
248
     */
249
    protected static $exceptionClass = 'Assert\InvalidArgumentException';
250
251
    /**
252
     * Helper method that handles building the assertion failure exceptions.
253
     * They are returned from this method so that the stack trace still shows
254
     * the assertions method.
255
     *
256
     * @param mixed $value
257
     * @param string $message
258
     * @param int $code
259
     * @param string|null $propertyPath
260
     * @param array $constraints
261
     *
262
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
263
     */
264
    protected static function createException($value, $message, $code, $propertyPath, array $constraints = array())
265
    {
266
        $exceptionClass = static::$exceptionClass;
267
        return new $exceptionClass($message, $code, $propertyPath, $value, $constraints);
268
    }
269
270
    /**
271
     * Assert that two values are equal (using == ).
272
     *
273
     * @param mixed $value
274
     * @param mixed $value2
275
     * @param string|null $message
276
     * @param string|null $propertyPath
277
     * @return bool
278
     * @throws \Assert\AssertionFailedException
279
     */
280
    public static function eq($value, $value2, $message = null, $propertyPath = null)
281
    {
282
        if ($value != $value2) {
283
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
284
                $message ?: 'Value "%s" does not equal expected value "%s".',
285
                static::stringify($value),
286
                static::stringify($value2)
287
            );
288
289
            throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, array('expected' => $value2));
290
        }
291
292
        return true;
293
    }
294
295
    /**
296
     * Assert that two values are the same (using ===).
297
     *
298
     * @param mixed $value
299
     * @param mixed $value2
300
     * @param string|null $message
301
     * @param string|null $propertyPath
302
     * @return bool
303
     * @throws \Assert\AssertionFailedException
304
     */
305
    public static function same($value, $value2, $message = null, $propertyPath = null)
306
    {
307
        if ($value !== $value2) {
308
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
309
                $message ?: 'Value "%s" is not the same as expected value "%s".',
310
                static::stringify($value),
311
                static::stringify($value2)
312
            );
313
314
            throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, array('expected' => $value2));
315
        }
316
317
        return true;
318
    }
319
320
    /**
321
     * Assert that two values are not equal (using == ).
322
     *
323
     * @param mixed $value1
324
     * @param mixed $value2
325
     * @param string|null $message
326
     * @param string|null $propertyPath
327
     * @return bool
328
     * @throws \Assert\AssertionFailedException
329
     */
330
    public static function notEq($value1, $value2, $message = null, $propertyPath = null)
331
    {
332
        if ($value1 == $value2) {
333
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
334
                $message ?: 'Value "%s" is equal to expected value "%s".',
335
                static::stringify($value1),
336
                static::stringify($value2)
337
            );
338
            throw static::createException($value1, $message, static::INVALID_NOT_EQ, $propertyPath, array('expected' => $value2));
339
        }
340
341
        return true;
342
    }
343
344
    /**
345
     * Assert that two values are not the same (using === ).
346
     *
347
     * @param mixed $value1
348
     * @param mixed $value2
349
     * @param string|null $message
350
     * @param string|null $propertyPath
351
     * @return bool
352
     * @throws \Assert\AssertionFailedException
353
     */
354
    public static function notSame($value1, $value2, $message = null, $propertyPath = null)
355
    {
356
        if ($value1 === $value2) {
357
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
358
                $message ?: 'Value "%s" is the same as expected value "%s".',
359
                static::stringify($value1),
360
                static::stringify($value2)
361
            );
362
            throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, array('expected' => $value2));
363
        }
364
365
        return true;
366
    }
367
368
    /**
369
     * Assert that value is not in array of choices.
370
     *
371
     * @param mixed $value
372
     * @param array $choices
373
     * @param string|null $message
374
     * @param string|null $propertyPath
375
     * @return bool
376
     * @throws \Assert\AssertionFailedException
377
     */
378
    public static function notInArray($value, array $choices, $message = null, $propertyPath = null)
379
    {
380
        if (in_array($value, $choices) === true) {
381
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
382
                $message ?: 'Value "%s" is in given "%s".',
383
                static::stringify($value),
384
                static::stringify($choices)
385
            );
386
            throw static::createException($value, $message, static::INVALID_VALUE_IN_ARRAY, $propertyPath);
387
        }
388
389
        return true;
390
    }
391
392
    /**
393
     * Assert that value is a php integer.
394
     *
395
     * @param mixed $value
396
     * @param string|null $message
397
     * @param string|null $propertyPath
398
     * @return bool
399
     * @throws \Assert\AssertionFailedException
400
     */
401
    public static function integer($value, $message = null, $propertyPath = null)
402
    {
403
        if (! is_int($value)) {
404
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
405
                $message ?: 'Value "%s" is not an integer.',
406
                static::stringify($value)
407
            );
408
409
            throw static::createException($value, $message, static::INVALID_INTEGER, $propertyPath);
410
        }
411
412
        return true;
413
    }
414
415
    /**
416
     * Assert that value is a php float.
417
     *
418
     * @param mixed $value
419
     * @param string|null $message
420
     * @param string|null $propertyPath
421
     * @return bool
422
     * @throws \Assert\AssertionFailedException
423
     */
424
    public static function float($value, $message = null, $propertyPath = null)
425
    {
426
        if (! is_float($value)) {
427
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
428
                $message ?: 'Value "%s" is not a float.',
429
                static::stringify($value)
430
            );
431
432
            throw static::createException($value, $message, static::INVALID_FLOAT, $propertyPath);
433
        }
434
435
        return true;
436
    }
437
438
    /**
439
     * Validates if an integer or integerish is a digit.
440
     *
441
     * @param mixed $value
442
     * @param string|null $message
443
     * @param string|null $propertyPath
444
     * @return bool
445
     * @throws \Assert\AssertionFailedException
446
     */
447
    public static function digit($value, $message = null, $propertyPath = null)
448
    {
449
        if (! ctype_digit((string)$value)) {
450
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
451
                $message ?: 'Value "%s" is not a digit.',
452
                static::stringify($value)
453
            );
454
455
            throw static::createException($value, $message, static::INVALID_DIGIT, $propertyPath);
456
        }
457
458
        return true;
459
    }
460
461
    /**
462
     * Assert that value is a php integer'ish.
463
     *
464
     * @param mixed $value
465
     * @param string|null $message
466
     * @param string|null $propertyPath
467
     * @return bool
468
     * @throws \Assert\AssertionFailedException
469
     */
470
    public static function integerish($value, $message = null, $propertyPath = null)
471
    {
472
        if (is_object($value) || strval(intval($value)) != $value || is_bool($value) || is_null($value)) {
473
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
474
                $message ?: 'Value "%s" is not an integer or a number castable to integer.',
475
                static::stringify($value)
476
            );
477
478
            throw static::createException($value, $message, static::INVALID_INTEGERISH, $propertyPath);
479
        }
480
481
        return true;
482
    }
483
484
    /**
485
     * Assert that value is php boolean
486
     *
487
     * @param mixed $value
488
     * @param string|null $message
489
     * @param string|null $propertyPath
490
     * @return bool
491
     * @throws \Assert\AssertionFailedException
492
     */
493
    public static function boolean($value, $message = null, $propertyPath = null)
494
    {
495
        if (! is_bool($value)) {
496
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
497
                $message ?: 'Value "%s" is not a boolean.',
498
                static::stringify($value)
499
            );
500
501
            throw static::createException($value, $message, static::INVALID_BOOLEAN, $propertyPath);
502
        }
503
504
        return true;
505
    }
506
507
    /**
508
     * Assert that value is a PHP scalar
509
     *
510
     * @param mixed $value
511
     * @param string|null $message
512
     * @param string|null $propertyPath
513
     * @return bool
514
     * @throws \Assert\AssertionFailedException
515
     */
516
    public static function scalar($value, $message = null, $propertyPath = null)
517
    {
518
        if (!is_scalar($value)) {
519
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
520
                $message ?: 'Value "%s" is not a scalar.',
521
                static::stringify($value)
522
            );
523
524
            throw static::createException($value, $message, static::INVALID_SCALAR, $propertyPath);
525
        }
526
527
        return true;
528
    }
529
530
    /**
531
     * Assert that value is not empty
532
     *
533
     * @param mixed $value
534
     * @param string|null $message
535
     * @param string|null $propertyPath
536
     * @return bool
537
     * @throws \Assert\AssertionFailedException
538
     */
539
    public static function notEmpty($value, $message = null, $propertyPath = null)
540
    {
541
        if (empty($value)) {
542
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
543
                $message ?: 'Value "%s" is empty, but non empty value was expected.',
544
                static::stringify($value)
545
            );
546
547
            throw static::createException($value, $message, static::VALUE_EMPTY, $propertyPath);
548
        }
549
550
        return true;
551
    }
552
553
    /**
554
     * Assert that value is empty
555
     *
556
     * @param mixed $value
557
     * @param string|null $message
558
     * @param string|null $propertyPath
559
     * @return bool
560
     * @throws \Assert\AssertionFailedException
561
     */
562
    public static function noContent($value, $message = null, $propertyPath = null)
563
    {
564
        if (!empty($value)) {
565
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
566
                $message ?: 'Value "%s" is not empty, but empty value was expected.',
567
                static::stringify($value)
568
            );
569
570
            throw static::createException($value, $message, static::VALUE_NOT_EMPTY, $propertyPath);
571
        }
572
573
        return true;
574
    }
575
576
    /**
577
     * Assert that value is null
578
     *
579
     * @param mixed $value
580
     * @param string|null $message
581
     * @param string|null $propertyPath
582
     * @return bool
583
     * @throws \Assert\AssertionFailedException
584
     */
585
    public static function null($value, $message = null, $propertyPath = null)
586
    {
587
        if ($value !== null) {
588
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
589
                $message ?: 'Value "%s" is not null, but null value was expected.',
590
                static::stringify($value)
591
            );
592
593
            throw static::createException($value, $message, static::VALUE_NOT_NULL, $propertyPath);
594
        }
595
596
        return true;
597
    }
598
599
    /**
600
     * Assert that value is not null
601
     *
602
     * @param mixed $value
603
     * @param string|null $message
604
     * @param string|null $propertyPath
605
     * @return bool
606
     * @throws \Assert\AssertionFailedException
607
     */
608
    public static function notNull($value, $message = null, $propertyPath = null)
609
    {
610
        if ($value === null) {
611
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
612
                $message ?: 'Value "%s" is null, but non null value was expected.',
613
                static::stringify($value)
614
            );
615
616
            throw static::createException($value, $message, static::VALUE_NULL, $propertyPath);
617
        }
618
619
        return true;
620
    }
621
622
    /**
623
     * Assert that value is a string
624
     *
625
     * @param mixed $value
626
     * @param string|null $message
627
     * @param string|null $propertyPath
628
     * @return bool
629
     * @throws \Assert\AssertionFailedException
630
     */
631
    public static function string($value, $message = null, $propertyPath = null)
632
    {
633
        if (! is_string($value)) {
634
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
635
                $message ?: 'Value "%s" expected to be string, type %s given.',
636
                static::stringify($value),
637
                gettype($value)
638
            );
639
640
            throw static::createException($value, $message, static::INVALID_STRING, $propertyPath);
641
        }
642
643
        return true;
644
    }
645
646
    /**
647
     * Assert that value matches a regex
648
     *
649
     * @param mixed $value
650
     * @param string $pattern
651
     * @param string|null $message
652
     * @param string|null $propertyPath
653
     * @return bool
654
     * @throws \Assert\AssertionFailedException
655
     */
656
    public static function regex($value, $pattern, $message = null, $propertyPath = null)
657
    {
658
        static::string($value, $message, $propertyPath);
659
660
        if (! preg_match($pattern, $value)) {
661
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
662
                $message ?: 'Value "%s" does not match expression.',
663
                static::stringify($value)
664
            );
665
666
            throw static::createException($value, $message, static::INVALID_REGEX, $propertyPath, array('pattern' => $pattern));
667
        }
668
669
        return true;
670
    }
671
672
    /**
673
     * Assert that string has a given length.
674
     *
675
     * @param mixed $value
676
     * @param int $length
677
     * @param string|null $message
678
     * @param string|null $propertyPath
679
     * @param string $encoding
680
     * @return bool
681
     * @throws \Assert\AssertionFailedException
682
     */
683
    public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
684
    {
685
        static::string($value, $message, $propertyPath);
686
687
        if (mb_strlen($value, $encoding) !== $length) {
688
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
689
                $message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
690
                static::stringify($value),
691
                $length,
692
                mb_strlen($value, $encoding)
693
            );
694
695
            $constraints = array('length' => $length, 'encoding' => $encoding);
696
            throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
697
        }
698
699
        return true;
700
    }
701
702
    /**
703
     * Assert that a string is at least $minLength chars long.
704
     *
705
     * @param mixed $value
706
     * @param int $minLength
707
     * @param string|null $message
708
     * @param string|null $propertyPath
709
     * @param string $encoding
710
     * @return bool
711
     * @throws \Assert\AssertionFailedException
712
     */
713
    public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8')
714
    {
715
        static::string($value, $message, $propertyPath);
716
717
        if (mb_strlen($value, $encoding) < $minLength) {
718
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
719
                $message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
720
                static::stringify($value),
721
                $minLength,
722
                mb_strlen($value, $encoding)
723
            );
724
725
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
726
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
727
        }
728
729
        return true;
730
    }
731
732
    /**
733
     * Assert that string value is not longer than $maxLength chars.
734
     *
735
     * @param mixed $value
736
     * @param integer $maxLength
737
     * @param string|null $message
738
     * @param string|null $propertyPath
739
     * @param string $encoding
740
     * @return bool
741
     * @throws \Assert\AssertionFailedException
742
     */
743
    public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
744
    {
745
        static::string($value, $message, $propertyPath);
746
747
        if (mb_strlen($value, $encoding) > $maxLength) {
748
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
749
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
750
                static::stringify($value),
751
                $maxLength,
752
                mb_strlen($value, $encoding)
753
            );
754
755
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
756
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
757
        }
758
759
        return true;
760
    }
761
762
    /**
763
     * Assert that string length is between min,max lengths.
764
     *
765
     * @param mixed $value
766
     * @param integer $minLength
767
     * @param integer $maxLength
768
     * @param string|null $message
769
     * @param string|null $propertyPath
770
     * @param string $encoding
771
     * @return bool
772
     * @throws \Assert\AssertionFailedException
773
     */
774
    public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
775
    {
776
        static::string($value, $message, $propertyPath);
777
778
        if (mb_strlen($value, $encoding) < $minLength) {
779
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
780
                $message ?: 'Value "%s" is too short, it should have at least %d characters, but only has %d characters.',
781
                static::stringify($value),
782
                $minLength,
783
                mb_strlen($value, $encoding)
784
            );
785
786
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
787
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
788
        }
789
790
        if (mb_strlen($value, $encoding) > $maxLength) {
791
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
792
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
793
                static::stringify($value),
794
                $maxLength,
795
                mb_strlen($value, $encoding)
796
            );
797
798
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
799
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
800
        }
801
802
        return true;
803
    }
804
805
    /**
806
     * Assert that string starts with a sequence of chars.
807
     *
808
     * @param mixed $string
809
     * @param string $needle
810
     * @param string|null $message
811
     * @param string|null $propertyPath
812
     * @param string $encoding
813
     * @return bool
814
     * @throws \Assert\AssertionFailedException
815
     */
816
    public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
817
    {
818
        static::string($string, $message, $propertyPath);
819
820
        if (mb_strpos($string, $needle, null, $encoding) !== 0) {
821
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
822
                $message ?: 'Value "%s" does not start with "%s".',
823
                static::stringify($string),
824
                static::stringify($needle)
825
            );
826
827
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
828
            throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints);
829
        }
830
831
        return true;
832
    }
833
834
    /**
835
     * Assert that string ends with a sequence of chars.
836
     *
837
     * @param mixed $string
838
     * @param string $needle
839
     * @param string|null $message
840
     * @param string|null $propertyPath
841
     * @param string $encoding
842
     * @return bool
843
     * @throws \Assert\AssertionFailedException
844
     */
845
    public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
846
    {
847
        static::string($string, $message, $propertyPath);
848
849
        $stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding);
850
851
        if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) {
852
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
853
                $message ?: 'Value "%s" does not end with "%s".',
854
                static::stringify($string),
855
                static::stringify($needle)
856
            );
857
858
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
859
            throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints);
860
        }
861
862
        return true;
863
    }
864
865
    /**
866
     * Assert that string contains a sequence of chars.
867
     *
868
     * @param mixed $string
869
     * @param string $needle
870
     * @param string|null $message
871
     * @param string|null $propertyPath
872
     * @param string $encoding
873
     * @return bool
874
     * @throws \Assert\AssertionFailedException
875
     */
876
    public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
877
    {
878
        static::string($string, $message, $propertyPath);
879
880
        if (mb_strpos($string, $needle, null, $encoding) === false) {
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" does not contain "%s".',
883
                static::stringify($string),
884
                static::stringify($needle)
885
            );
886
887
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
888
            throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints);
889
        }
890
891
        return true;
892
    }
893
894
    /**
895
     * Assert that value is in array of choices.
896
     *
897
     * @param mixed $value
898
     * @param array $choices
899
     * @param string|null $message
900
     * @param string|null $propertyPath
901
     * @return bool
902
     * @throws \Assert\AssertionFailedException
903
     */
904
    public static function choice($value, array $choices, $message = null, $propertyPath = null)
905
    {
906
        if (! in_array($value, $choices, true)) {
907
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
908
                $message ?: 'Value "%s" is not an element of the valid values: %s',
909
                static::stringify($value),
910
                implode(", ", array_map('Assert\Assertion::stringify', $choices))
911
            );
912
913
            throw static::createException($value, $message, static::INVALID_CHOICE, $propertyPath, array('choices' => $choices));
914
        }
915
916
        return true;
917
    }
918
919
    /**
920
     * Alias of {@see choice()}
921
     *
922
     * @param mixed $value
923
     * @param array $choices
924
     * @param string|null $message
925
     * @param string|null $propertyPath
926
     * @return bool
927
     */
928
    public static function inArray($value, array $choices, $message = null, $propertyPath = null)
929
    {
930
        return static::choice($value, $choices, $message, $propertyPath);
931
    }
932
933
    /**
934
     * Assert that value is numeric.
935
     *
936
     * @param mixed $value
937
     * @param string|null $message
938
     * @param string|null $propertyPath
939
     * @return bool
940
     * @throws \Assert\AssertionFailedException
941
     */
942
    public static function numeric($value, $message = null, $propertyPath = null)
943
    {
944
        if (! is_numeric($value)) {
945
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
946
                $message ?: 'Value "%s" is not numeric.',
947
                static::stringify($value)
948
            );
949
950
            throw static::createException($value, $message, static::INVALID_NUMERIC, $propertyPath);
951
        }
952
953
        return true;
954
    }
955
956
    /**
957
     * Assert that value is an array.
958
     *
959
     * @param mixed $value
960
     * @param string|null $message
961
     * @param string|null $propertyPath
962
     * @return bool
963
     * @throws \Assert\AssertionFailedException
964
     */
965
    public static function isArray($value, $message = null, $propertyPath = null)
966
    {
967
        if (! is_array($value)) {
968
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
969
                $message ?: 'Value "%s" is not an array.',
970
                static::stringify($value)
971
            );
972
973
            throw static::createException($value, $message, static::INVALID_ARRAY, $propertyPath);
974
        }
975
976
        return true;
977
    }
978
979
    /**
980
     * Assert that value is an array or a traversable object.
981
     *
982
     * @param mixed $value
983
     * @param string|null $message
984
     * @param string|null $propertyPath
985
     * @return bool
986
     * @throws \Assert\AssertionFailedException
987
     */
988
    public static function isTraversable($value, $message = null, $propertyPath = null)
989
    {
990
        if (! is_array($value) && ! $value instanceof \Traversable) {
991
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
992
                $message ?: 'Value "%s" is not an array and does not implement Traversable.',
993
                static::stringify($value)
994
            );
995
996
            throw static::createException($value, $message, static::INVALID_TRAVERSABLE, $propertyPath);
997
        }
998
999
        return true;
1000
    }
1001
1002
    /**
1003
     * Assert that value is an array or an array-accessible object.
1004
     *
1005
     * @param mixed $value
1006
     * @param string|null $message
1007
     * @param string|null $propertyPath
1008
     * @return bool
1009
     * @throws \Assert\AssertionFailedException
1010
     */
1011
    public static function isArrayAccessible($value, $message = null, $propertyPath = null)
1012
    {
1013
        if (! is_array($value) && ! $value instanceof \ArrayAccess) {
1014
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1015
                $message ?: 'Value "%s" is not an array and does not implement ArrayAccess.',
1016
                static::stringify($value)
1017
            );
1018
1019
            throw static::createException($value, $message, static::INVALID_ARRAY_ACCESSIBLE, $propertyPath);
1020
        }
1021
1022
        return true;
1023
    }
1024
1025
    /**
1026
     * Assert that key exists in an array
1027
     *
1028
     * @param mixed $value
1029
     * @param string|integer $key
1030
     * @param string|null $message
1031
     * @param string|null $propertyPath
1032
     * @return bool
1033
     * @throws \Assert\AssertionFailedException
1034
     */
1035
    public static function keyExists($value, $key, $message = null, $propertyPath = null)
1036
    {
1037
        static::isArray($value, $message, $propertyPath);
1038
1039
        if (! array_key_exists($key, $value)) {
1040
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1041
                $message ?: 'Array does not contain an element with key "%s"',
1042
                static::stringify($key)
1043
            );
1044
1045
            throw static::createException($value, $message, static::INVALID_KEY_EXISTS, $propertyPath, array('key' => $key));
1046
        }
1047
1048
        return true;
1049
    }
1050
1051
    /**
1052
     * Assert that key does not exist in an array
1053
     *
1054
     * @param mixed $value
1055
     * @param string|integer $key
1056
     * @param string|null $message
1057
     * @param string|null $propertyPath
1058
     * @return bool
1059
     * @throws \Assert\AssertionFailedException
1060
     */
1061
    public static function keyNotExists($value, $key, $message = null, $propertyPath = null)
1062
    {
1063
        static::isArray($value, $message, $propertyPath);
1064
1065
        if (array_key_exists($key, $value)) {
1066
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1067
                $message ?: 'Array contains an element with key "%s"',
1068
                self::stringify($key)
1069
            );
1070
1071
            throw static::createException($value, $message, static::INVALID_KEY_NOT_EXISTS, $propertyPath, array('key' => $key));
1072
        }
1073
1074
        return true;
1075
    }
1076
1077
    /**
1078
     * Assert that key exists in an array/array-accessible object using isset()
1079
     *
1080
     * @param mixed $value
1081
     * @param string|integer $key
1082
     * @param string|null $message
1083
     * @param string|null $propertyPath
1084
     * @return bool
1085
     * @throws \Assert\AssertionFailedException
1086
     */
1087
    public static function keyIsset($value, $key, $message = null, $propertyPath = null)
1088
    {
1089
        static::isArrayAccessible($value, $message, $propertyPath);
1090
1091
        if (! isset($value[$key])) {
1092
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1093
                $message ?: 'The element with key "%s" was not found',
1094
                static::stringify($key)
1095
            );
1096
1097
            throw static::createException($value, $message, static::INVALID_KEY_ISSET, $propertyPath, array('key' => $key));
1098
        }
1099
1100
        return true;
1101
    }
1102
1103
    /**
1104
     * Assert that key exists in an array/array-accessible object and its value is not empty.
1105
     *
1106
     * @param mixed $value
1107
     * @param string|integer $key
1108
     * @param string|null $message
1109
     * @param string|null $propertyPath
1110
     * @return bool
1111
     * @throws \Assert\AssertionFailedException
1112
     */
1113
    public static function notEmptyKey($value, $key, $message = null, $propertyPath = null)
1114
    {
1115
        static::keyIsset($value, $key, $message, $propertyPath);
1116
        static::notEmpty($value[$key], $message, $propertyPath);
1117
1118
        return true;
1119
    }
1120
1121
    /**
1122
     * Assert that value is not blank
1123
     *
1124
     * @param mixed $value
1125
     * @param string|null $message
1126
     * @param string|null $propertyPath
1127
     * @return bool
1128
     * @throws \Assert\AssertionFailedException
1129
     */
1130
    public static function notBlank($value, $message = null, $propertyPath = null)
1131
    {
1132
        if (false === $value || (empty($value) && '0' != $value) || (is_string($value) && '' === trim($value))) {
1133
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1134
                $message ?: 'Value "%s" is blank, but was expected to contain a value.',
1135
                static::stringify($value)
1136
            );
1137
1138
            throw static::createException($value, $message, static::INVALID_NOT_BLANK, $propertyPath);
1139
        }
1140
1141
        return true;
1142
    }
1143
1144
    /**
1145
     * Assert that value is instance of given class-name.
1146
     *
1147
     * @param mixed $value
1148
     * @param string $className
1149
     * @param string|null $message
1150
     * @param string|null $propertyPath
1151
     * @return bool
1152
     * @throws \Assert\AssertionFailedException
1153
     */
1154
    public static function isInstanceOf($value, $className, $message = null, $propertyPath = null)
1155
    {
1156
        if (! ($value instanceof $className)) {
1157
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1158
                $message ?: 'Class "%s" was expected to be instanceof of "%s" but is not.',
1159
                static::stringify($value),
1160
                $className
1161
            );
1162
1163
            throw static::createException($value, $message, static::INVALID_INSTANCE_OF, $propertyPath, array('class' => $className));
1164
        }
1165
1166
        return true;
1167
    }
1168
1169
    /**
1170
     * Assert that value is not instance of given class-name.
1171
     *
1172
     * @param mixed $value
1173
     * @param string $className
1174
     * @param string|null $message
1175
     * @param string|null $propertyPath
1176
     * @return bool
1177
     * @throws \Assert\AssertionFailedException
1178
     */
1179
    public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null)
1180
    {
1181
        if ($value instanceof $className) {
1182
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1183
                $message ?: 'Class "%s" was not expected to be instanceof of "%s".',
1184
                static::stringify($value),
1185
                $className
1186
            );
1187
1188
            throw static::createException($value, $message, static::INVALID_NOT_INSTANCE_OF, $propertyPath, array('class' => $className));
1189
        }
1190
1191
        return true;
1192
    }
1193
1194
    /**
1195
     * Assert that value is subclass of given class-name.
1196
     *
1197
     * @param mixed $value
1198
     * @param string $className
1199
     * @param string|null $message
1200
     * @param string|null $propertyPath
1201
     * @return bool
1202
     * @throws \Assert\AssertionFailedException
1203
     */
1204
    public static function subclassOf($value, $className, $message = null, $propertyPath = null)
1205
    {
1206
        if (! is_subclass_of($value, $className)) {
1207
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1208
                $message ?: 'Class "%s" was expected to be subclass of "%s".',
1209
                static::stringify($value),
1210
                $className
1211
            );
1212
1213
            throw static::createException($value, $message, static::INVALID_SUBCLASS_OF, $propertyPath, array('class' => $className));
1214
        }
1215
1216
        return true;
1217
    }
1218
1219
    /**
1220
     * Assert that value is in range of numbers.
1221
     *
1222
     * @param mixed $value
1223
     * @param integer $minValue
1224
     * @param integer $maxValue
1225
     * @param string|null $message
1226
     * @param string|null $propertyPath
1227
     * @return bool
1228
     * @throws \Assert\AssertionFailedException
1229
     */
1230
    public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null)
1231
    {
1232
        static::numeric($value, $message, $propertyPath);
1233
1234
        if ($value < $minValue || $value > $maxValue) {
1235
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1236
                $message ?: 'Number "%s" was expected to be at least "%d" and at most "%d".',
1237
                static::stringify($value),
1238
                static::stringify($minValue),
1239
                static::stringify($maxValue)
1240
            );
1241
1242
            throw static::createException($value, $message, static::INVALID_RANGE, $propertyPath, array('min' => $minValue, 'max' => $maxValue));
1243
        }
1244
1245
        return true;
1246
    }
1247
1248
    /**
1249
     * Assert that a value is at least as big as a given limit
1250
     *
1251
     * @param mixed $value
1252
     * @param mixed $minValue
1253
     * @param string|null $message
1254
     * @param string|null $propertyPath
1255
     * @return bool
1256
     * @throws \Assert\AssertionFailedException
1257
     */
1258
    public static function min($value, $minValue, $message = null, $propertyPath = null)
1259
    {
1260
        static::numeric($value, $message, $propertyPath);
1261
1262
        if ($value < $minValue) {
1263
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1264
                $message ?: 'Number "%s" was expected to be at least "%s".',
1265
                static::stringify($value),
1266
                static::stringify($minValue)
1267
            );
1268
1269
            throw static::createException($value, $message, static::INVALID_MIN, $propertyPath, array('min' => $minValue));
1270
        }
1271
1272
        return true;
1273
    }
1274
1275
    /**
1276
     * Assert that a number is smaller as a given limit
1277
     *
1278
     * @param mixed $value
1279
     * @param mixed $maxValue
1280
     * @param string|null $message
1281
     * @param string|null $propertyPath
1282
     * @return bool
1283
     * @throws \Assert\AssertionFailedException
1284
     */
1285
    public static function max($value, $maxValue, $message = null, $propertyPath = null)
1286
    {
1287
        static::numeric($value, $message, $propertyPath);
1288
1289
        if ($value > $maxValue) {
1290
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1291
                $message ?: 'Number "%s" was expected to be at most "%s".',
1292
                static::stringify($value),
1293
                static::stringify($maxValue)
1294
            );
1295
1296
            throw static::createException($value, $message, static::INVALID_MAX, $propertyPath, array('max' => $maxValue));
1297
        }
1298
1299
        return true;
1300
    }
1301
1302
    /**
1303
     * Assert that a file exists
1304
     *
1305
     * @param string $value
1306
     * @param string|null $message
1307
     * @param string|null $propertyPath
1308
     * @return bool
1309
     * @throws \Assert\AssertionFailedException
1310
     */
1311
    public static function file($value, $message = null, $propertyPath = null)
1312
    {
1313
        static::string($value, $message, $propertyPath);
1314
        static::notEmpty($value, $message, $propertyPath);
1315
1316
        if (! is_file($value)) {
1317
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1318
                $message ?: 'File "%s" was expected to exist.',
1319
                static::stringify($value)
1320
            );
1321
1322
            throw static::createException($value, $message, static::INVALID_FILE, $propertyPath);
1323
        }
1324
1325
        return true;
1326
    }
1327
1328
    /**
1329
     * Assert that a directory exists
1330
     *
1331
     * @param string $value
1332
     * @param string|null $message
1333
     * @param string|null $propertyPath
1334
     * @return bool
1335
     * @throws \Assert\AssertionFailedException
1336
     */
1337
    public static function directory($value, $message = null, $propertyPath = null)
1338
    {
1339
        static::string($value, $message, $propertyPath);
1340
1341
        if (! is_dir($value)) {
1342
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1343
                $message ?: 'Path "%s" was expected to be a directory.',
1344
                static::stringify($value)
1345
            );
1346
1347
            throw static::createException($value, $message, static::INVALID_DIRECTORY, $propertyPath);
1348
        }
1349
1350
        return true;
1351
    }
1352
1353
    /**
1354
     * Assert that the value is something readable
1355
     *
1356
     * @param string $value
1357
     * @param string|null $message
1358
     * @param string|null $propertyPath
1359
     * @return bool
1360
     * @throws \Assert\AssertionFailedException
1361
     */
1362
    public static function readable($value, $message = null, $propertyPath = null)
1363
    {
1364
        static::string($value, $message, $propertyPath);
1365
1366
        if (! is_readable($value)) {
1367
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1368
                $message ?: 'Path "%s" was expected to be readable.',
1369
                static::stringify($value)
1370
            );
1371
1372
            throw static::createException($value, $message, static::INVALID_READABLE, $propertyPath);
1373
        }
1374
1375
        return true;
1376
    }
1377
1378
    /**
1379
     * Assert that the value is something writeable
1380
     *
1381
     * @param string $value
1382
     * @param string|null $message
1383
     * @param string|null $propertyPath
1384
     * @return bool
1385
     * @throws \Assert\AssertionFailedException
1386
     */
1387
    public static function writeable($value, $message = null, $propertyPath = null)
1388
    {
1389
        static::string($value, $message, $propertyPath);
1390
1391
        if (! is_writeable($value)) {
1392
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1393
                $message ?: 'Path "%s" was expected to be writeable.',
1394
                static::stringify($value)
1395
            );
1396
1397
            throw static::createException($value, $message, static::INVALID_WRITEABLE, $propertyPath);
1398
        }
1399
1400
        return true;
1401
    }
1402
1403
    /**
1404
     * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL).
1405
     *
1406
     * @param mixed $value
1407
     * @param string|null $message
1408
     * @param string|null $propertyPath
1409
     * @return bool
1410
     * @throws \Assert\AssertionFailedException
1411
     */
1412
    public static function email($value, $message = null, $propertyPath = null)
1413
    {
1414
        static::string($value, $message, $propertyPath);
1415
1416
        if (! filter_var($value, FILTER_VALIDATE_EMAIL)) {
1417
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1418
                $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1419
                static::stringify($value)
1420
            );
1421
1422
            throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1423
        } else {
1424
            $host = substr($value, strpos($value, '@') + 1);
1425
1426
            // Likely not a FQDN, bug in PHP FILTER_VALIDATE_EMAIL prior to PHP 5.3.3
1427
            if (version_compare(PHP_VERSION, '5.3.3', '<') && strpos($host, '.') === false) {
1428
                $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1429
                    $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1430
                    static::stringify($value)
1431
                );
1432
1433
                throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1434
            }
1435
        }
1436
1437
        return true;
1438
    }
1439
1440
    /**
1441
     * Assert that value is an URL.
1442
     *
1443
     * This code snipped was taken from the Symfony project and modified to the special demands of this method.
1444
     *
1445
     * @param mixed $value
1446
     * @param string|null $message
1447
     * @param string|null $propertyPath
1448
     * @return bool
1449
     * @throws \Assert\AssertionFailedException
1450
     *
1451
     *
1452
     * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php
1453
     * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php
1454
     */
1455
    public static function url($value, $message = null, $propertyPath = null)
1456
    {
1457
        static::string($value, $message, $propertyPath);
1458
1459
        $protocols = array('http', 'https');
1460
1461
        $pattern = '~^
1462
            (%s)://                                 # protocol
1463
            (([\pL\pN-]+:)?([\pL\pN-]+)@)?          # basic auth
1464
            (
1465
                ([\pL\pN\pS-\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
1466
                    |                                                 # or
1467
                \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}                    # an IP address
1468
                    |                                                 # or
1469
                \[
1470
                    (?:(?:(?:(?:(?:(?:(?:[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})))?::))))
1471
                \]  # an IPv6 address
1472
            )
1473
            (:[0-9]+)?                              # a port (optional)
1474
            (/?|/\S+|\?\S*|\#\S*)                   # a /, nothing, a / with something, a query or a fragment
1475
        $~ixu';
1476
1477
        $pattern = sprintf($pattern, implode('|', $protocols));
1478
1479
        if (!preg_match($pattern, $value)) {
1480
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1481
                $message ?: 'Value "%s" was expected to be a valid URL starting with http or https',
1482
                static::stringify($value)
1483
            );
1484
1485
            throw static::createException($value, $message, static::INVALID_URL, $propertyPath);
1486
        }
1487
1488
        return true;
1489
    }
1490
1491
    /**
1492
     * Assert that value is alphanumeric.
1493
     *
1494
     * @param mixed $value
1495
     * @param string|null $message
1496
     * @param string|null $propertyPath
1497
     * @return bool
1498
     * @throws \Assert\AssertionFailedException
1499
     */
1500
    public static function alnum($value, $message = null, $propertyPath = null)
1501
    {
1502
        try {
1503
            static::regex($value, '(^([a-zA-Z]{1}[a-zA-Z0-9]*)$)', $message, $propertyPath);
1504
        } catch (AssertionFailedException $e) {
1505
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1506
                $message ?: 'Value "%s" is not alphanumeric, starting with letters and containing only letters and numbers.',
1507
                static::stringify($value)
1508
            );
1509
1510
            throw static::createException($value, $message, static::INVALID_ALNUM, $propertyPath);
1511
        }
1512
1513
        return true;
1514
    }
1515
1516
    /**
1517
     * Assert that the value is boolean True.
1518
     *
1519
     * @param mixed $value
1520
     * @param string|null $message
1521
     * @param string|null $propertyPath
1522
     * @return bool
1523
     * @throws \Assert\AssertionFailedException
1524
     */
1525
    public static function true($value, $message = null, $propertyPath = null)
1526
    {
1527
        if ($value !== true) {
1528
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1529
                $message ?: 'Value "%s" is not TRUE.',
1530
                static::stringify($value)
1531
            );
1532
1533
            throw static::createException($value, $message, static::INVALID_TRUE, $propertyPath);
1534
        }
1535
1536
        return true;
1537
    }
1538
1539
    /**
1540
     * Assert that the value is boolean False.
1541
     *
1542
     * @param mixed $value
1543
     * @param string|null $message
1544
     * @param string|null $propertyPath
1545
     * @return bool
1546
     * @throws \Assert\AssertionFailedException
1547
     */
1548
    public static function false($value, $message = null, $propertyPath = null)
1549
    {
1550
        if ($value !== false) {
1551
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1552
                $message ?: 'Value "%s" is not FALSE.',
1553
                static::stringify($value)
1554
            );
1555
1556
            throw static::createException($value, $message, static::INVALID_FALSE, $propertyPath);
1557
        }
1558
1559
        return true;
1560
    }
1561
1562
    /**
1563
     * Assert that the class exists.
1564
     *
1565
     * @param mixed $value
1566
     * @param string|null $message
1567
     * @param string|null $propertyPath
1568
     * @return bool
1569
     * @throws \Assert\AssertionFailedException
1570
     */
1571
    public static function classExists($value, $message = null, $propertyPath = null)
1572
    {
1573
        if (! class_exists($value)) {
1574
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1575
                $message ?: 'Class "%s" does not exist.',
1576
                static::stringify($value)
1577
            );
1578
1579
            throw static::createException($value, $message, static::INVALID_CLASS, $propertyPath);
1580
        }
1581
1582
        return true;
1583
    }
1584
1585
    /**
1586
     * Assert that the interface exists.
1587
     *
1588
     * @param mixed $value
1589
     * @param string|null $message
1590
     * @param string|null $propertyPath
1591
     * @return bool
1592
     * @throws \Assert\AssertionFailedException
1593
     */
1594
    public static function interfaceExists($value, $message = null, $propertyPath = null)
1595
    {
1596
        if (! interface_exists($value)) {
1597
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1598
                $message ?: 'Interface "%s" does not exist.',
1599
                static::stringify($value)
1600
            );
1601
1602
            throw static::createException($value, $message, static::INVALID_INTERFACE, $propertyPath);
1603
        }
1604
1605
        return true;
1606
    }
1607
1608
    /**
1609
     * Assert that the class implements the interface
1610
     *
1611
     * @param mixed $class
1612
     * @param string $interfaceName
1613
     * @param string|null $message
1614
     * @param string|null $propertyPath
1615
     * @return bool
1616
     * @throws \Assert\AssertionFailedException
1617
     */
1618
    public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null)
1619
    {
1620
        $reflection = new \ReflectionClass($class);
1621
        if (! $reflection->implementsInterface($interfaceName)) {
1622
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1623
                $message ?: 'Class "%s" does not implement interface "%s".',
1624
                static::stringify($class),
1625
                static::stringify($interfaceName)
1626
            );
1627
1628
            throw static::createException($class, $message, static::INTERFACE_NOT_IMPLEMENTED, $propertyPath, array('interface' => $interfaceName));
1629
        }
1630
1631
        return true;
1632
    }
1633
1634
    /**
1635
     * Assert that the given string is a valid json string.
1636
     *
1637
     * NOTICE:
1638
     * Since this does a json_decode to determine its validity
1639
     * you probably should consider, when using the variable
1640
     * content afterwards, just to decode and check for yourself instead
1641
     * of using this assertion.
1642
     *
1643
     * @param mixed $value
1644
     * @param string|null $message
1645
     * @param string|null $propertyPath
1646
     * @return bool
1647
     * @throws \Assert\AssertionFailedException
1648
     */
1649
    public static function isJsonString($value, $message = null, $propertyPath = null)
1650
    {
1651
        if (null === json_decode($value) && JSON_ERROR_NONE !== json_last_error()) {
1652
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1653
                $message ?: 'Value "%s" is not a valid JSON string.',
1654
                static::stringify($value)
1655
            );
1656
1657
            throw static::createException($value, $message, static::INVALID_JSON_STRING, $propertyPath);
1658
        }
1659
1660
        return true;
1661
    }
1662
1663
    /**
1664
     * Assert that the given string is a valid UUID
1665
     *
1666
     * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed.
1667
     *
1668
     * @param string $value
1669
     * @param string|null $message
1670
     * @param string|null $propertyPath
1671
     * @return bool
1672
     * @throws \Assert\AssertionFailedException
1673
     */
1674
    public static function uuid($value, $message = null, $propertyPath = null)
1675
    {
1676
        $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...
1677
1678
        if ($value === '00000000-0000-0000-0000-000000000000') {
1679
            return true;
1680
        }
1681
1682
        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)) {
1683
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1684
                $message ?: 'Value "%s" is not a valid UUID.',
1685
                static::stringify($value)
1686
            );
1687
1688
            throw static::createException($value, $message, static::INVALID_UUID, $propertyPath);
1689
        }
1690
1691
        return true;
1692
    }
1693
1694
    /**
1695
     * Assert that the given string is a valid E164 Phone Number
1696
     *
1697
     * @link https://en.wikipedia.org/wiki/E.164
1698
     *
1699
     * @param string $value
1700
     * @param string|null $message
1701
     * @param string|null $propertyPath
1702
     * @return bool
1703
     * @throws \Assert\AssertionFailedException
1704
     */
1705
    public static function e164($value, $message = null, $propertyPath = null)
1706
    {
1707
        if (!preg_match('/^\+?[1-9]\d{1,14}$/', $value)) {
1708
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1709
                $message ?: 'Value "%s" is not a valid E164.',
1710
                static::stringify($value)
1711
            );
1712
1713
            throw static::createException($value, $message, static::INVALID_E164, $propertyPath);
1714
        }
1715
1716
        return true;
1717
    }
1718
1719
    /**
1720
     * Assert that the count of countable is equal to count.
1721
     *
1722
     * @param array|\Countable $countable
1723
     * @param int              $count
1724
     * @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...
1725
     * @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...
1726
     * @return bool
1727
     * @throws \Assert\AssertionFailedException
1728
     */
1729
    public static function count($countable, $count, $message = null, $propertyPath = null)
1730
    {
1731
        if ($count !== count($countable)) {
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 ?: 'List does not contain exactly "%d" elements.',
1734
                static::stringify($count)
1735
            );
1736
1737
            throw static::createException($countable, $message, static::INVALID_COUNT, $propertyPath, array('count' => $count));
1738
        }
1739
1740
        return true;
1741
    }
1742
1743
    /**
1744
     * static call handler to implement:
1745
     *  - "null or assertion" delegation
1746
     *  - "all" delegation
1747
     *
1748
     * @param string $method
1749
     * @param array $args
1750
     *
1751
     * @return bool|mixed
1752
     */
1753
    public static function __callStatic($method, $args)
1754
    {
1755
        if (strpos($method, "nullOr") === 0) {
1756
            if (! array_key_exists(0, $args)) {
1757
                throw new BadMethodCallException("Missing the first argument.");
1758
            }
1759
1760
            if ($args[0] === null) {
1761
                return true;
1762
            }
1763
1764
            $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...
1765
1766
            return call_user_func_array(array(get_called_class(), $method), $args);
1767
        }
1768
1769
        if (strpos($method, "all") === 0) {
1770
            if (! array_key_exists(0, $args)) {
1771
                throw new BadMethodCallException("Missing the first argument.");
1772
            }
1773
1774
            static::isTraversable($args[0]);
1775
1776
            $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...
1777
            $values      = array_shift($args);
1778
            $calledClass = get_called_class();
1779
1780
            foreach ($values as $value) {
1781
                call_user_func_array(array($calledClass, $method), array_merge(array($value), $args));
1782
            }
1783
1784
            return true;
1785
        }
1786
1787
        throw new BadMethodCallException("No assertion Assertion#" . $method . " exists.");
1788
    }
1789
1790
    /**
1791
     * Determines if the values array has every choice as key and that this choice has content.
1792
     *
1793
     * @param array $values
1794
     * @param array $choices
1795
     * @param null  $message
1796
     * @param null  $propertyPath
1797
     * @return bool
1798
     */
1799
    public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null)
1800
    {
1801
        self::notEmpty($values, $message, $propertyPath);
1802
1803
        foreach ($choices as $choice) {
1804
            self::notEmptyKey($values, $choice, $message, $propertyPath);
1805
        }
1806
1807
        return true;
1808
    }
1809
1810
    /**
1811
     * Determines that the named method is defined in the provided object.
1812
     *
1813
     * @param string $value
1814
     * @param mixed  $object
1815
     * @param null   $message
1816
     * @param null   $propertyPath
1817
     * @return bool
1818
     * @throws
1819
     */
1820
    public static function methodExists($value, $object, $message = null, $propertyPath = null)
1821
    {
1822
        self::isObject($object, $message, $propertyPath);
1823
1824
        if (!method_exists($object, $value)) {
1825
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1826
                $message ?: 'Expected "%s" does not exist in provided object.',
1827
                static::stringify($value)
1828
            );
1829
1830
            throw static::createException($value, $message, static::INVALID_METHOD, $propertyPath);
1831
        }
1832
1833
        return true;
1834
    }
1835
1836
    /**
1837
     * Determines that the provided value is an object.
1838
     *
1839
     * @param mixed $value
1840
     * @param null  $message
1841
     * @param null  $propertyPath
1842
     * @return bool
1843
     */
1844
    public static function isObject($value, $message = null, $propertyPath = null)
1845
    {
1846
        if (!is_object($value)) {
1847
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1848
                $message ?: 'Provided "%s" is not a valid object.',
1849
                static::stringify($value)
1850
            );
1851
1852
            throw static::createException($value, $message, static::INVALID_OBJECT, $propertyPath);
1853
        }
1854
1855
        return true;
1856
    }
1857
1858
    /**
1859
     * Determines if the value is less than given limit.
1860
     *
1861
     * @param mixed $value
1862
     * @param mixed $limit
1863
     * @param null  $message
1864
     * @param null  $propertyPath
1865
     * @return bool
1866
     */
1867
    public static function lessThan($value, $limit, $message = null, $propertyPath = null)
1868
    {
1869
        if ($value >= $limit) {
1870
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1871
                $message ?: 'Provided "%s" is not less than "%s".',
1872
                static::stringify($value),
1873
                static::stringify($limit)
1874
            );
1875
1876
            throw static::createException($value, $message, static::INVALID_LESS, $propertyPath);
1877
        }
1878
1879
        return true;
1880
    }
1881
1882
    /**
1883
     * Determines if the value is less or than given limit.
1884
     *
1885
     * @param mixed $value
1886
     * @param mixed $limit
1887
     * @param null  $message
1888
     * @param null  $propertyPath
1889
     * @return bool
1890
     */
1891
    public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1892
    {
1893
        if ($value > $limit) {
1894
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1895
                $message ?: 'Provided "%s" is not less or equal than "%s".',
1896
                static::stringify($value),
1897
                static::stringify($limit)
1898
            );
1899
1900
            throw static::createException($value, $message, static::INVALID_LESS_OR_EQUAL, $propertyPath);
1901
        }
1902
1903
        return true;
1904
    }
1905
1906
    /**
1907
     * Determines if the value is greater than given limit.
1908
     *
1909
     * @param mixed $value
1910
     * @param mixed $limit
1911
     * @param null  $message
1912
     * @param null  $propertyPath
1913
     * @return bool
1914
     */
1915
    public static function greaterThan($value, $limit, $message = null, $propertyPath = null)
1916
    {
1917
        if ($value <= $limit) {
1918
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1919
                $message ?: 'Provided "%s" is not greater than "%s".',
1920
                static::stringify($value),
1921
                static::stringify($limit)
1922
            );
1923
1924
            throw static::createException($value, $message, static::INVALID_GREATER, $propertyPath);
1925
        }
1926
1927
        return true;
1928
    }
1929
1930
    /**
1931
     * Determines if the value is greater or equal than given limit.
1932
     *
1933
     * @param mixed $value
1934
     * @param mixed $limit
1935
     * @param null  $message
1936
     * @param null  $propertyPath
1937
     * @return bool
1938
     */
1939
    public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1940
    {
1941
        if ($value < $limit) {
1942
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1943
                $message ?: 'Provided "%s" is not greater or equal than "%s".',
1944
                static::stringify($value),
1945
                static::stringify($limit)
1946
            );
1947
1948
            throw static::createException($value, $message, static::INVALID_GREATER_OR_EQUAL, $propertyPath);
1949
        }
1950
1951
        return true;
1952
    }
1953
1954
    /**
1955
     * Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit.
1956
     *
1957
     * @param mixed $value
1958
     * @param mixed $lowerLimit
1959
     * @param mixed $upperLimit
1960
     * @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...
1961
     * @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...
1962
     * @return bool
1963
     */
1964
    public static function between($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null)
1965
    {
1966
        if ($lowerLimit > $value || $value > $upperLimit) {
1967
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1968
                $message ?: 'Provided "%s" is neither greater than or equal to "%s" nor less than or equal to "%s".',
1969
                static::stringify($value),
1970
                static::stringify($lowerLimit),
1971
                static::stringify($upperLimit)
1972
            );
1973
1974
            throw static::createException($value, $message, static::INVALID_BETWEEN, $propertyPath);
1975
        }
1976
1977
        return true;
1978
    }
1979
1980
    /**
1981
     * Assert that a value is greater than a lower limit, and less than an upper limit.
1982
     *
1983
     * @param mixed $value
1984
     * @param mixed $lowerLimit
1985
     * @param mixed $upperLimit
1986
     * @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...
1987
     * @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...
1988
     * @return bool
1989
     */
1990
    public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null)
1991
    {
1992
        if ($lowerLimit >= $value || $value >= $upperLimit) {
1993
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1994
                $message ?: 'Provided "%s" is neither greater than "%s" nor less than "%s".',
1995
                static::stringify($value),
1996
                static::stringify($lowerLimit),
1997
                static::stringify($upperLimit)
1998
            );
1999
2000
            throw static::createException($value, $message, static::INVALID_BETWEEN_EXCLUSIVE, $propertyPath);
2001
        }
2002
2003
        return true;
2004
    }
2005
2006
    /**
2007
     * Assert that extension is loaded.
2008
     *
2009
     * @param mixed $value
2010
     * @param string|null $message
2011
     * @param string|null $propertyPath
2012
     * @return bool
2013
     * @throws \Assert\AssertionFailedException
2014
     */
2015
    public static function extensionLoaded($value, $message = null, $propertyPath = null)
2016
    {
2017
        if (! extension_loaded($value)) {
2018
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2019
                $message ?: 'Extension "%s" is required.',
2020
                static::stringify($value)
2021
            );
2022
2023
            throw static::createException($value, $message, static::INVALID_EXTENSION, $propertyPath);
2024
        }
2025
2026
        return true;
2027
    }
2028
2029
    /**
2030
     * Assert that date is valid and corresponds to the given format.
2031
     *
2032
     * @param string      $value
2033
     * @param string      $format supports all of the options date(), except for the following:
2034
     *                            N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r.
2035
     * @param string|null $message
2036
     * @param string|null $propertyPath
2037
     * @return bool
2038
     *
2039
     * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters
2040
     */
2041
     public static function date($value, $format, $message = null, $propertyPath = null)
2042
     {
2043
         static::string($value, $message, $propertyPath);
2044
         static::string($format, $message, $propertyPath);
2045
2046
         $dateTime = \DateTime::createFromFormat($format, $value);
2047
2048
         if (false === $dateTime || $value !== $dateTime->format($format)) {
2049
             $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2050
                 $message ?: 'Date "%s" is invalid or does not match format "%s".',
2051
                 static::stringify($value),
2052
                 static::stringify($format)
2053
             );
2054
2055
             throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format));
2056
         }
2057
2058
         return true;
2059
     }
2060
2061
    /**
2062
     * Determines that the provided value is callable.
2063
     *
2064
     * @param mixed $value
2065
     * @param null $message
2066
     * @param null $propertyPath
2067
     * @return bool
2068
     */
2069
    public static function isCallable($value, $message = null, $propertyPath = null)
2070
    {
2071
        if (!is_callable($value)) {
2072
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2073
                $message ?: 'Provided "%s" is not a callable.',
2074
                static::stringify($value)
2075
            );
2076
2077
            throw static::createException($value, $message, static::INVALID_CALLABLE, $propertyPath);
2078
        }
2079
2080
        return true;
2081
    }
2082
2083
    /**
2084
     * Assert that the provided value is valid according to a callback.
2085
     *
2086
     * If the callback returns `false` the assertion will fail.
2087
     *
2088
     * @param mixed $value
2089
     * @param callable $callback
2090
     * @param string|null $message
2091
     * @param string|null $propertyPath
2092
     * @return bool
2093
     */
2094
    public static function satisfy($value, $callback, $message = null, $propertyPath = null)
2095
    {
2096
        static::isCallable($callback);
2097
2098
        if (call_user_func($callback, $value) === false) {
2099
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2100
                $message ?: 'Provided "%s" is invalid according to custom rule.',
2101
                static::stringify($value)
2102
            );
2103
2104
            throw static::createException($value, $message, static::INVALID_SATISFY, $propertyPath);
2105
        }
2106
2107
        return true;
2108
    }
2109
2110
    /**
2111
     * Assert that value is an IPv4 or IPv6 address
2112
     * (using input_filter/FILTER_VALIDATE_IP).
2113
     *
2114
     * @param string      $value
2115
     * @param null|int    $flag
2116
     * @param string|null $message
2117
     * @param string|null $propertyPath
2118
     * @return bool
2119
     *
2120
     * @link http://php.net/manual/filter.filters.flags.php
2121
     */
2122
    public static function ip($value, $flag = null, $message = null, $propertyPath = null)
2123
    {
2124
        self::string($value, $message, $propertyPath);
2125
        if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) {
2126
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2127
                $message ?: 'Value "%s" was expected to be a valid IP address.',
2128
                self::stringify($value)
2129
            );
2130
            throw static::createException($value, $message, static::INVALID_IP, $propertyPath);
2131
        }
2132
2133
        return true;
2134
    }
2135
2136
    /**
2137
     * Assert that value is an IPv4 address
2138
     * (using input_filter/FILTER_VALIDATE_IP).
2139
     *
2140
     * @param string      $value
2141
     * @param null|int    $flag
2142
     * @param string|null $message
2143
     * @param string|null $propertyPath
2144
     * @return bool
2145
     *
2146
     * @link http://php.net/manual/filter.filters.flags.php
2147
     */
2148
    public static function ipv4($value, $flag = null, $message = null, $propertyPath = null)
2149
    {
2150
        self::ip($value, $flag | FILTER_FLAG_IPV4, $message ?: 'Value "%s" was expected to be a valid IPv4 address.', $propertyPath);
2151
2152
        return true;
2153
    }
2154
2155
    /**
2156
     * Assert that value is an IPv6 address
2157
     * (using input_filter/FILTER_VALIDATE_IP).
2158
     *
2159
     * @param string      $value
2160
     * @param null|int    $flag
2161
     * @param string|null $message
2162
     * @param string|null $propertyPath
2163
     * @return bool
2164
     *
2165
     * @link http://php.net/manual/filter.filters.flags.php
2166
     */
2167
    public static function ipv6($value, $flag = null, $message = null, $propertyPath = null)
2168
    {
2169
        self::ip($value, $flag | FILTER_FLAG_IPV6, $message ?: 'Value "%s" was expected to be a valid IPv6 address.', $propertyPath);
2170
2171
        return true;
2172
    }
2173
2174
    /**
2175
     * Make a string version of a value.
2176
     *
2177
     * @param mixed $value
2178
     * @return string
2179
     */
2180
    protected static function stringify($value)
2181
    {
2182
        if (is_bool($value)) {
2183
            return $value ? '<TRUE>' : '<FALSE>';
2184
        }
2185
2186
        if (is_scalar($value)) {
2187
            $val = (string)$value;
2188
2189
            if (strlen($val) > 100) {
2190
                $val = substr($val, 0, 97) . '...';
2191
            }
2192
2193
            return $val;
2194
        }
2195
2196
        if (is_array($value)) {
2197
            return '<ARRAY>';
2198
        }
2199
2200
        if (is_object($value)) {
2201
            return get_class($value);
2202
        }
2203
2204
        if (is_resource($value)) {
2205
            return get_resource_type($value);
2206
        }
2207
2208
        if ($value === null) {
2209
            return '<NULL>';
2210
        }
2211
2212
        return gettype($value);
2213
    }
2214
2215
    /**
2216
     * Assert that a constant is defined.
2217
     *
2218
     * @param mixed $constant
2219
     * @param string|null $message
2220
     * @param string|null $propertyPath
2221
     * @return bool
2222
     * @throws \Assert\AssertionFailedException
2223
     */
2224
    public static function defined($constant, $message = null, $propertyPath = null)
2225
    {
2226
        if (!defined($constant)) {
2227
            $message = sprintf($message ?: 'Value "%s" expected to be a defined constant.', $constant);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2228
2229
            throw static::createException($constant, $message, static::INVALID_CONSTANT, $propertyPath);
2230
        }
2231
2232
        return true;
2233
    }
2234
}
2235