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