Completed
Pull Request — master (#193)
by Timothy
02:05
created

Assertion::defined()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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