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