These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the webmozart/assert package. |
||
5 | * |
||
6 | * (c) Bernhard Schussek <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Webmozart\Assert; |
||
13 | |||
14 | use BadMethodCallException; |
||
15 | use Closure; |
||
16 | use Exception; |
||
17 | use InvalidArgumentException; |
||
18 | use Throwable; |
||
19 | use Traversable; |
||
20 | |||
21 | /** |
||
22 | * Efficient assertions to validate the input/output of your methods. |
||
23 | * |
||
24 | * @method static void nullOrString($value, $message = '') |
||
25 | * @method static void nullOrStringNotEmpty($value, $message = '') |
||
26 | * @method static void nullOrInteger($value, $message = '') |
||
27 | * @method static void nullOrIntegerish($value, $message = '') |
||
28 | * @method static void nullOrFloat($value, $message = '') |
||
29 | * @method static void nullOrNumeric($value, $message = '') |
||
30 | * @method static void nullOrBoolean($value, $message = '') |
||
31 | * @method static void nullOrScalar($value, $message = '') |
||
32 | * @method static void nullOrObject($value, $message = '') |
||
33 | * @method static void nullOrResource($value, $type = null, $message = '') |
||
34 | * @method static void nullOrIsCallable($value, $message = '') |
||
35 | * @method static void nullOrIsArray($value, $message = '') |
||
36 | * @method static void nullOrIsTraversable($value, $message = '') |
||
37 | * @method static void nullOrIsInstanceOf($value, $class, $message = '') |
||
38 | * @method static void nullOrNotInstanceOf($value, $class, $message = '') |
||
39 | * @method static void nullOrIsEmpty($value, $message = '') |
||
40 | * @method static void nullOrNotEmpty($value, $message = '') |
||
41 | * @method static void nullOrTrue($value, $message = '') |
||
42 | * @method static void nullOrFalse($value, $message = '') |
||
43 | * @method static void nullOrEq($value, $value2, $message = '') |
||
44 | * @method static void nullOrNotEq($value,$value2, $message = '') |
||
45 | * @method static void nullOrSame($value, $value2, $message = '') |
||
46 | * @method static void nullOrNotSame($value, $value2, $message = '') |
||
47 | * @method static void nullOrGreaterThan($value, $value2, $message = '') |
||
48 | * @method static void nullOrGreaterThanEq($value, $value2, $message = '') |
||
49 | * @method static void nullOrLessThan($value, $value2, $message = '') |
||
50 | * @method static void nullOrLessThanEq($value, $value2, $message = '') |
||
51 | * @method static void nullOrRange($value, $min, $max, $message = '') |
||
52 | * @method static void nullOrOneOf($value, $values, $message = '') |
||
53 | * @method static void nullOrContains($value, $subString, $message = '') |
||
54 | * @method static void nullOrStartsWith($value, $prefix, $message = '') |
||
55 | * @method static void nullOrStartsWithLetter($value, $message = '') |
||
56 | * @method static void nullOrEndsWith($value, $suffix, $message = '') |
||
57 | * @method static void nullOrRegex($value, $pattern, $message = '') |
||
58 | * @method static void nullOrAlpha($value, $message = '') |
||
59 | * @method static void nullOrDigits($value, $message = '') |
||
60 | * @method static void nullOrAlnum($value, $message = '') |
||
61 | * @method static void nullOrLower($value, $message = '') |
||
62 | * @method static void nullOrUpper($value, $message = '') |
||
63 | * @method static void nullOrLength($value, $length, $message = '') |
||
64 | * @method static void nullOrMinLength($value, $min, $message = '') |
||
65 | * @method static void nullOrMaxLength($value, $max, $message = '') |
||
66 | * @method static void nullOrLengthBetween($value, $min, $max, $message = '') |
||
67 | * @method static void nullOrFileExists($value, $message = '') |
||
68 | * @method static void nullOrFile($value, $message = '') |
||
69 | * @method static void nullOrDirectory($value, $message = '') |
||
70 | * @method static void nullOrReadable($value, $message = '') |
||
71 | * @method static void nullOrWritable($value, $message = '') |
||
72 | * @method static void nullOrClassExists($value, $message = '') |
||
73 | * @method static void nullOrSubclassOf($value, $class, $message = '') |
||
74 | * @method static void nullOrImplementsInterface($value, $interface, $message = '') |
||
75 | * @method static void nullOrPropertyExists($value, $property, $message = '') |
||
76 | * @method static void nullOrPropertyNotExists($value, $property, $message = '') |
||
77 | * @method static void nullOrMethodExists($value, $method, $message = '') |
||
78 | * @method static void nullOrMethodNotExists($value, $method, $message = '') |
||
79 | * @method static void nullOrKeyExists($value, $key, $message = '') |
||
80 | * @method static void nullOrKeyNotExists($value, $key, $message = '') |
||
81 | * @method static void nullOrCount($value, $key, $message = '') |
||
82 | * @method static void nullOrUuid($values, $message = '') |
||
83 | * @method static void allString($values, $message = '') |
||
84 | * @method static void allStringNotEmpty($values, $message = '') |
||
85 | * @method static void allInteger($values, $message = '') |
||
86 | * @method static void allIntegerish($values, $message = '') |
||
87 | * @method static void allFloat($values, $message = '') |
||
88 | * @method static void allNumeric($values, $message = '') |
||
89 | * @method static void allBoolean($values, $message = '') |
||
90 | * @method static void allScalar($values, $message = '') |
||
91 | * @method static void allObject($values, $message = '') |
||
92 | * @method static void allResource($values, $type = null, $message = '') |
||
93 | * @method static void allIsCallable($values, $message = '') |
||
94 | * @method static void allIsArray($values, $message = '') |
||
95 | * @method static void allIsTraversable($values, $message = '') |
||
96 | * @method static void allIsInstanceOf($values, $class, $message = '') |
||
97 | * @method static void allNotInstanceOf($values, $class, $message = '') |
||
98 | * @method static void allNull($values, $message = '') |
||
99 | * @method static void allNotNull($values, $message = '') |
||
100 | * @method static void allIsEmpty($values, $message = '') |
||
101 | * @method static void allNotEmpty($values, $message = '') |
||
102 | * @method static void allTrue($values, $message = '') |
||
103 | * @method static void allFalse($values, $message = '') |
||
104 | * @method static void allEq($values, $value2, $message = '') |
||
105 | * @method static void allNotEq($values,$value2, $message = '') |
||
106 | * @method static void allSame($values, $value2, $message = '') |
||
107 | * @method static void allNotSame($values, $value2, $message = '') |
||
108 | * @method static void allGreaterThan($values, $value2, $message = '') |
||
109 | * @method static void allGreaterThanEq($values, $value2, $message = '') |
||
110 | * @method static void allLessThan($values, $value2, $message = '') |
||
111 | * @method static void allLessThanEq($values, $value2, $message = '') |
||
112 | * @method static void allRange($values, $min, $max, $message = '') |
||
113 | * @method static void allOneOf($values, $values, $message = '') |
||
114 | * @method static void allContains($values, $subString, $message = '') |
||
115 | * @method static void allStartsWith($values, $prefix, $message = '') |
||
116 | * @method static void allStartsWithLetter($values, $message = '') |
||
117 | * @method static void allEndsWith($values, $suffix, $message = '') |
||
118 | * @method static void allRegex($values, $pattern, $message = '') |
||
119 | * @method static void allAlpha($values, $message = '') |
||
120 | * @method static void allDigits($values, $message = '') |
||
121 | * @method static void allAlnum($values, $message = '') |
||
122 | * @method static void allLower($values, $message = '') |
||
123 | * @method static void allUpper($values, $message = '') |
||
124 | * @method static void allLength($values, $length, $message = '') |
||
125 | * @method static void allMinLength($values, $min, $message = '') |
||
126 | * @method static void allMaxLength($values, $max, $message = '') |
||
127 | * @method static void allLengthBetween($values, $min, $max, $message = '') |
||
128 | * @method static void allFileExists($values, $message = '') |
||
129 | * @method static void allFile($values, $message = '') |
||
130 | * @method static void allDirectory($values, $message = '') |
||
131 | * @method static void allReadable($values, $message = '') |
||
132 | * @method static void allWritable($values, $message = '') |
||
133 | * @method static void allClassExists($values, $message = '') |
||
134 | * @method static void allSubclassOf($values, $class, $message = '') |
||
135 | * @method static void allImplementsInterface($values, $interface, $message = '') |
||
136 | * @method static void allPropertyExists($values, $property, $message = '') |
||
137 | * @method static void allPropertyNotExists($values, $property, $message = '') |
||
138 | * @method static void allMethodExists($values, $method, $message = '') |
||
139 | * @method static void allMethodNotExists($values, $method, $message = '') |
||
140 | * @method static void allKeyExists($values, $key, $message = '') |
||
141 | * @method static void allKeyNotExists($values, $key, $message = '') |
||
142 | * @method static void allCount($values, $key, $message = '') |
||
143 | * @method static void allUuid($values, $message = '') |
||
144 | * |
||
145 | * @since 1.0 |
||
146 | * |
||
147 | * @author Bernhard Schussek <[email protected]> |
||
148 | */ |
||
149 | class Assert |
||
150 | { |
||
151 | 90 | public static function string($value, $message = '') |
|
152 | { |
||
153 | 90 | if (!is_string($value)) { |
|
154 | 14 | static::reportInvalidArgument(sprintf( |
|
155 | 14 | $message ?: 'Expected a string. Got: %s', |
|
156 | 14 | static::typeToString($value) |
|
157 | )); |
||
158 | } |
||
159 | 76 | } |
|
160 | |||
161 | 12 | public static function stringNotEmpty($value, $message = '') |
|
162 | { |
||
163 | 12 | static::string($value, $message); |
|
164 | 8 | static::notEmpty($value, $message); |
|
165 | 4 | } |
|
166 | |||
167 | 17 | View Code Duplication | public static function integer($value, $message = '') |
168 | { |
||
169 | 17 | if (!is_int($value)) { |
|
170 | 13 | static::reportInvalidArgument(sprintf( |
|
171 | 13 | $message ?: 'Expected an integer. Got: %s', |
|
172 | 13 | static::typeToString($value) |
|
173 | )); |
||
174 | } |
||
175 | 4 | } |
|
176 | |||
177 | 16 | View Code Duplication | public static function integerish($value, $message = '') |
178 | { |
||
179 | 16 | if (!is_numeric($value) || $value != (int) $value) { |
|
180 | 4 | static::reportInvalidArgument(sprintf( |
|
181 | 4 | $message ?: 'Expected an integerish value. Got: %s', |
|
182 | 4 | static::typeToString($value) |
|
183 | )); |
||
184 | } |
||
185 | 12 | } |
|
186 | |||
187 | 16 | View Code Duplication | public static function float($value, $message = '') |
188 | { |
||
189 | 16 | if (!is_float($value)) { |
|
190 | 8 | static::reportInvalidArgument(sprintf( |
|
191 | 8 | $message ?: 'Expected a float. Got: %s', |
|
192 | 8 | static::typeToString($value) |
|
193 | )); |
||
194 | } |
||
195 | 8 | } |
|
196 | |||
197 | 20 | View Code Duplication | public static function numeric($value, $message = '') |
198 | { |
||
199 | 20 | if (!is_numeric($value)) { |
|
200 | 4 | static::reportInvalidArgument(sprintf( |
|
201 | 4 | $message ?: 'Expected a numeric. Got: %s', |
|
202 | 4 | static::typeToString($value) |
|
203 | )); |
||
204 | } |
||
205 | 16 | } |
|
206 | |||
207 | 16 | View Code Duplication | public static function boolean($value, $message = '') |
208 | { |
||
209 | 16 | if (!is_bool($value)) { |
|
210 | 8 | static::reportInvalidArgument(sprintf( |
|
211 | 8 | $message ?: 'Expected a boolean. Got: %s', |
|
212 | 8 | static::typeToString($value) |
|
213 | )); |
||
214 | } |
||
215 | 8 | } |
|
216 | |||
217 | 23 | View Code Duplication | public static function scalar($value, $message = '') |
218 | { |
||
219 | 23 | if (!is_scalar($value)) { |
|
220 | 11 | static::reportInvalidArgument(sprintf( |
|
221 | 11 | $message ?: 'Expected a scalar. Got: %s', |
|
222 | 11 | static::typeToString($value) |
|
223 | )); |
||
224 | } |
||
225 | 12 | } |
|
226 | |||
227 | 23 | View Code Duplication | public static function object($value, $message = '') |
228 | { |
||
229 | 23 | if (!is_object($value)) { |
|
230 | 15 | static::reportInvalidArgument(sprintf( |
|
231 | 15 | $message ?: 'Expected an object. Got: %s', |
|
232 | 15 | static::typeToString($value) |
|
233 | )); |
||
234 | } |
||
235 | 8 | } |
|
236 | |||
237 | 16 | public static function resource($value, $type = null, $message = '') |
|
238 | { |
||
239 | 16 | if (!is_resource($value)) { |
|
240 | 4 | static::reportInvalidArgument(sprintf( |
|
241 | 4 | $message ?: 'Expected a resource. Got: %s', |
|
242 | 4 | static::typeToString($value) |
|
243 | )); |
||
244 | } |
||
245 | |||
246 | 12 | if ($type && $type !== get_resource_type($value)) { |
|
247 | 4 | static::reportInvalidArgument(sprintf( |
|
248 | 4 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
|
249 | 4 | static::typeToString($value), |
|
250 | 4 | $type |
|
251 | )); |
||
252 | } |
||
253 | 8 | } |
|
254 | |||
255 | 20 | View Code Duplication | public static function isCallable($value, $message = '') |
256 | { |
||
257 | 20 | if (!is_callable($value)) { |
|
258 | 8 | static::reportInvalidArgument(sprintf( |
|
259 | 8 | $message ?: 'Expected a callable. Got: %s', |
|
260 | 8 | static::typeToString($value) |
|
261 | )); |
||
262 | } |
||
263 | 12 | } |
|
264 | |||
265 | 20 | View Code Duplication | public static function isArray($value, $message = '') |
266 | { |
||
267 | 20 | if (!is_array($value)) { |
|
268 | 12 | static::reportInvalidArgument(sprintf( |
|
269 | 12 | $message ?: 'Expected an array. Got: %s', |
|
270 | 12 | static::typeToString($value) |
|
271 | )); |
||
272 | } |
||
273 | 8 | } |
|
274 | |||
275 | 20 | public static function isTraversable($value, $message = '') |
|
276 | { |
||
277 | 20 | @trigger_error( |
|
0 ignored issues
–
show
|
|||
278 | 20 | sprintf( |
|
279 | 20 | 'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.', |
|
280 | 20 | __METHOD__ |
|
281 | ), |
||
282 | 20 | E_USER_DEPRECATED |
|
283 | ); |
||
284 | |||
285 | 20 | View Code Duplication | if (!is_array($value) && !($value instanceof Traversable)) { |
286 | 8 | static::reportInvalidArgument(sprintf( |
|
287 | 8 | $message ?: 'Expected a traversable. Got: %s', |
|
288 | 8 | static::typeToString($value) |
|
289 | )); |
||
290 | } |
||
291 | 12 | } |
|
292 | |||
293 | 516 | public static function isIterable($value, $message = '') |
|
294 | { |
||
295 | 516 | View Code Duplication | if (!is_array($value) && !($value instanceof Traversable)) { |
296 | 8 | static::reportInvalidArgument(sprintf( |
|
297 | 8 | $message ?: 'Expected an iterable. Got: %s', |
|
298 | 8 | static::typeToString($value) |
|
299 | )); |
||
300 | } |
||
301 | 512 | } |
|
302 | |||
303 | 16 | View Code Duplication | public static function isInstanceOf($value, $class, $message = '') |
304 | { |
||
305 | 16 | if (!($value instanceof $class)) { |
|
306 | 12 | static::reportInvalidArgument(sprintf( |
|
307 | 12 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
308 | 12 | static::typeToString($value), |
|
309 | 12 | $class |
|
310 | )); |
||
311 | } |
||
312 | 4 | } |
|
313 | |||
314 | 16 | View Code Duplication | public static function notInstanceOf($value, $class, $message = '') |
315 | { |
||
316 | 16 | if ($value instanceof $class) { |
|
317 | 4 | static::reportInvalidArgument(sprintf( |
|
318 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
319 | 4 | static::typeToString($value), |
|
320 | 4 | $class |
|
321 | )); |
||
322 | } |
||
323 | 12 | } |
|
324 | |||
325 | 23 | public static function isEmpty($value, $message = '') |
|
326 | { |
||
327 | 23 | if (!empty($value)) { |
|
328 | 8 | static::reportInvalidArgument(sprintf( |
|
329 | 8 | $message ?: 'Expected an empty value. Got: %s', |
|
330 | 8 | static::valueToString($value) |
|
331 | )); |
||
332 | } |
||
333 | 15 | } |
|
334 | |||
335 | 31 | public static function notEmpty($value, $message = '') |
|
336 | { |
||
337 | 31 | if (empty($value)) { |
|
338 | 19 | static::reportInvalidArgument(sprintf( |
|
339 | 19 | $message ?: 'Expected a non-empty value. Got: %s', |
|
340 | 19 | static::valueToString($value) |
|
341 | )); |
||
342 | } |
||
343 | 12 | } |
|
344 | |||
345 | 11 | public static function null($value, $message = '') |
|
346 | { |
||
347 | 11 | if (null !== $value) { |
|
348 | 8 | static::reportInvalidArgument(sprintf( |
|
349 | 8 | $message ?: 'Expected null. Got: %s', |
|
350 | 8 | static::valueToString($value) |
|
351 | )); |
||
352 | } |
||
353 | 3 | } |
|
354 | |||
355 | 11 | public static function notNull($value, $message = '') |
|
356 | { |
||
357 | 11 | if (null === $value) { |
|
358 | 3 | static::reportInvalidArgument( |
|
359 | 3 | $message ?: 'Expected a value other than null.' |
|
360 | ); |
||
361 | } |
||
362 | 8 | } |
|
363 | |||
364 | 15 | public static function true($value, $message = '') |
|
365 | { |
||
366 | 15 | if (true !== $value) { |
|
367 | 11 | static::reportInvalidArgument(sprintf( |
|
368 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
369 | 11 | static::valueToString($value) |
|
370 | )); |
||
371 | } |
||
372 | 4 | } |
|
373 | |||
374 | 19 | public static function false($value, $message = '') |
|
375 | { |
||
376 | 19 | if (false !== $value) { |
|
377 | 15 | static::reportInvalidArgument(sprintf( |
|
378 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
379 | 15 | static::valueToString($value) |
|
380 | )); |
||
381 | } |
||
382 | 4 | } |
|
383 | |||
384 | 32 | public static function eq($value, $value2, $message = '') |
|
385 | { |
||
386 | 32 | if ($value2 != $value) { |
|
387 | 16 | static::reportInvalidArgument(sprintf( |
|
388 | 16 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
389 | 16 | static::valueToString($value), |
|
390 | 16 | static::valueToString($value2) |
|
391 | )); |
||
392 | } |
||
393 | 16 | } |
|
394 | |||
395 | 16 | public static function notEq($value, $value2, $message = '') |
|
396 | { |
||
397 | 16 | if ($value2 == $value) { |
|
398 | 12 | static::reportInvalidArgument(sprintf( |
|
399 | 12 | $message ?: 'Expected a different value than %s.', |
|
400 | 12 | static::valueToString($value2) |
|
401 | )); |
||
402 | } |
||
403 | 4 | } |
|
404 | |||
405 | 16 | public static function same($value, $value2, $message = '') |
|
406 | { |
||
407 | 16 | if ($value2 !== $value) { |
|
408 | 12 | static::reportInvalidArgument(sprintf( |
|
409 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
410 | 12 | static::valueToString($value), |
|
411 | 12 | static::valueToString($value2) |
|
412 | )); |
||
413 | } |
||
414 | 4 | } |
|
415 | |||
416 | 16 | public static function notSame($value, $value2, $message = '') |
|
417 | { |
||
418 | 16 | if ($value2 === $value) { |
|
419 | 4 | static::reportInvalidArgument(sprintf( |
|
420 | 4 | $message ?: 'Expected a value not identical to %s.', |
|
421 | 4 | static::valueToString($value2) |
|
422 | )); |
||
423 | } |
||
424 | 12 | } |
|
425 | |||
426 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
427 | { |
||
428 | 8 | if ($value <= $limit) { |
|
429 | 4 | static::reportInvalidArgument(sprintf( |
|
430 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
431 | 4 | static::valueToString($value), |
|
432 | 4 | static::valueToString($limit) |
|
433 | )); |
||
434 | } |
||
435 | 4 | } |
|
436 | |||
437 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
438 | { |
||
439 | 12 | if ($value < $limit) { |
|
440 | 4 | static::reportInvalidArgument(sprintf( |
|
441 | 4 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
442 | 4 | static::valueToString($value), |
|
443 | 4 | static::valueToString($limit) |
|
444 | )); |
||
445 | } |
||
446 | 8 | } |
|
447 | |||
448 | 8 | public static function lessThan($value, $limit, $message = '') |
|
449 | { |
||
450 | 8 | if ($value >= $limit) { |
|
451 | 4 | static::reportInvalidArgument(sprintf( |
|
452 | 4 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
453 | 4 | static::valueToString($value), |
|
454 | 4 | static::valueToString($limit) |
|
455 | )); |
||
456 | } |
||
457 | 4 | } |
|
458 | |||
459 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
460 | { |
||
461 | 12 | if ($value > $limit) { |
|
462 | 4 | static::reportInvalidArgument(sprintf( |
|
463 | 4 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
464 | 4 | static::valueToString($value), |
|
465 | 4 | static::valueToString($limit) |
|
466 | )); |
||
467 | } |
||
468 | 8 | } |
|
469 | |||
470 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
471 | { |
||
472 | 16 | if ($value < $min || $value > $max) { |
|
473 | 8 | static::reportInvalidArgument(sprintf( |
|
474 | 8 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
|
475 | 8 | static::valueToString($value), |
|
476 | 8 | static::valueToString($min), |
|
477 | 8 | static::valueToString($max) |
|
478 | )); |
||
479 | } |
||
480 | 8 | } |
|
481 | |||
482 | 8 | public static function oneOf($value, array $values, $message = '') |
|
483 | { |
||
484 | 8 | if (!in_array($value, $values, true)) { |
|
485 | 4 | static::reportInvalidArgument(sprintf( |
|
486 | 4 | $message ?: 'Expected one of: %2$s. Got: %s', |
|
487 | 4 | static::valueToString($value), |
|
488 | 4 | implode(', ', array_map(array('static', 'valueToString'), $values)) |
|
489 | )); |
||
490 | } |
||
491 | 4 | } |
|
492 | |||
493 | 20 | View Code Duplication | public static function contains($value, $subString, $message = '') |
494 | { |
||
495 | 20 | if (false === strpos($value, $subString)) { |
|
496 | 8 | static::reportInvalidArgument(sprintf( |
|
497 | 8 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
|
498 | 8 | static::valueToString($value), |
|
499 | 8 | static::valueToString($subString) |
|
500 | )); |
||
501 | } |
||
502 | 12 | } |
|
503 | |||
504 | 12 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
505 | { |
||
506 | 12 | if (0 !== strpos($value, $prefix)) { |
|
507 | 8 | static::reportInvalidArgument(sprintf( |
|
508 | 8 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
|
509 | 8 | static::valueToString($value), |
|
510 | 8 | static::valueToString($prefix) |
|
511 | )); |
||
512 | } |
||
513 | 4 | } |
|
514 | |||
515 | 12 | public static function startsWithLetter($value, $message = '') |
|
516 | { |
||
517 | 12 | $valid = isset($value[0]); |
|
518 | |||
519 | 12 | if ($valid) { |
|
520 | 8 | $locale = setlocale(LC_CTYPE, 0); |
|
521 | 8 | setlocale(LC_CTYPE, 'C'); |
|
522 | 8 | $valid = ctype_alpha($value[0]); |
|
523 | 8 | setlocale(LC_CTYPE, $locale); |
|
524 | } |
||
525 | |||
526 | 12 | if (!$valid) { |
|
527 | 8 | static::reportInvalidArgument(sprintf( |
|
528 | 8 | $message ?: 'Expected a value to start with a letter. Got: %s', |
|
529 | 8 | static::valueToString($value) |
|
530 | )); |
||
531 | } |
||
532 | 4 | } |
|
533 | |||
534 | 12 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
535 | { |
||
536 | 12 | if ($suffix !== substr($value, -static::strlen($suffix))) { |
|
537 | 8 | static::reportInvalidArgument(sprintf( |
|
538 | 8 | $message ?: 'Expected a value to end with %2$s. Got: %s', |
|
539 | 8 | static::valueToString($value), |
|
540 | 8 | static::valueToString($suffix) |
|
541 | )); |
||
542 | } |
||
543 | 4 | } |
|
544 | |||
545 | 12 | public static function regex($value, $pattern, $message = '') |
|
546 | { |
||
547 | 12 | if (!preg_match($pattern, $value)) { |
|
548 | 8 | static::reportInvalidArgument(sprintf( |
|
549 | 8 | $message ?: 'The value %s does not match the expected pattern.', |
|
550 | 8 | static::valueToString($value) |
|
551 | )); |
||
552 | } |
||
553 | 4 | } |
|
554 | |||
555 | 12 | View Code Duplication | public static function alpha($value, $message = '') |
556 | { |
||
557 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
558 | 12 | setlocale(LC_CTYPE, 'C'); |
|
559 | 12 | $valid = !ctype_alpha($value); |
|
560 | 12 | setlocale(LC_CTYPE, $locale); |
|
561 | |||
562 | 12 | if ($valid) { |
|
563 | 8 | static::reportInvalidArgument(sprintf( |
|
564 | 8 | $message ?: 'Expected a value to contain only letters. Got: %s', |
|
565 | 8 | static::valueToString($value) |
|
566 | )); |
||
567 | } |
||
568 | 4 | } |
|
569 | |||
570 | 12 | View Code Duplication | public static function digits($value, $message = '') |
571 | { |
||
572 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
573 | 12 | setlocale(LC_CTYPE, 'C'); |
|
574 | 12 | $valid = !ctype_digit($value); |
|
575 | 12 | setlocale(LC_CTYPE, $locale); |
|
576 | |||
577 | 12 | if ($valid) { |
|
578 | 8 | static::reportInvalidArgument(sprintf( |
|
579 | 8 | $message ?: 'Expected a value to contain digits only. Got: %s', |
|
580 | 8 | static::valueToString($value) |
|
581 | )); |
||
582 | } |
||
583 | 4 | } |
|
584 | |||
585 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
586 | { |
||
587 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
588 | 12 | setlocale(LC_CTYPE, 'C'); |
|
589 | 12 | $valid = !ctype_alnum($value); |
|
590 | 12 | setlocale(LC_CTYPE, $locale); |
|
591 | |||
592 | 12 | if ($valid) { |
|
593 | 8 | static::reportInvalidArgument(sprintf( |
|
594 | 8 | $message ?: 'Expected a value to contain letters and digits only. Got: %s', |
|
595 | 8 | static::valueToString($value) |
|
596 | )); |
||
597 | } |
||
598 | 4 | } |
|
599 | |||
600 | 16 | View Code Duplication | public static function lower($value, $message = '') |
601 | { |
||
602 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
603 | 16 | setlocale(LC_CTYPE, 'C'); |
|
604 | 16 | $valid = !ctype_lower($value); |
|
605 | 16 | setlocale(LC_CTYPE, $locale); |
|
606 | |||
607 | 16 | if ($valid) { |
|
608 | 12 | static::reportInvalidArgument(sprintf( |
|
609 | 12 | $message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
|
610 | 12 | static::valueToString($value) |
|
611 | )); |
||
612 | } |
||
613 | 4 | } |
|
614 | |||
615 | 16 | View Code Duplication | public static function upper($value, $message = '') |
616 | { |
||
617 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
618 | 16 | setlocale(LC_CTYPE, 'C'); |
|
619 | 16 | $valid = !ctype_upper($value); |
|
620 | 16 | setlocale(LC_CTYPE, $locale); |
|
621 | |||
622 | 16 | if ($valid) { |
|
623 | 12 | static::reportInvalidArgument(sprintf( |
|
624 | 12 | $message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
|
625 | 12 | static::valueToString($value) |
|
626 | )); |
||
627 | } |
||
628 | 4 | } |
|
629 | |||
630 | 24 | public static function length($value, $length, $message = '') |
|
631 | { |
||
632 | 24 | if ($length !== static::strlen($value)) { |
|
633 | 16 | static::reportInvalidArgument(sprintf( |
|
634 | 16 | $message ?: 'Expected a value to contain %2$s characters. Got: %s', |
|
635 | 16 | static::valueToString($value), |
|
636 | 16 | $length |
|
637 | )); |
||
638 | } |
||
639 | 8 | } |
|
640 | |||
641 | 24 | public static function minLength($value, $min, $message = '') |
|
642 | { |
||
643 | 24 | if (static::strlen($value) < $min) { |
|
644 | 8 | static::reportInvalidArgument(sprintf( |
|
645 | 8 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
|
646 | 8 | static::valueToString($value), |
|
647 | 8 | $min |
|
648 | )); |
||
649 | } |
||
650 | 16 | } |
|
651 | |||
652 | 24 | public static function maxLength($value, $max, $message = '') |
|
653 | { |
||
654 | 24 | if (static::strlen($value) > $max) { |
|
655 | 8 | static::reportInvalidArgument(sprintf( |
|
656 | 8 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
|
657 | 8 | static::valueToString($value), |
|
658 | 8 | $max |
|
659 | )); |
||
660 | } |
||
661 | 16 | } |
|
662 | |||
663 | 40 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
664 | { |
||
665 | 40 | $length = static::strlen($value); |
|
666 | |||
667 | 40 | if ($length < $min || $length > $max) { |
|
668 | 16 | static::reportInvalidArgument(sprintf( |
|
669 | 16 | $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
|
670 | 16 | static::valueToString($value), |
|
671 | 16 | $min, |
|
672 | 16 | $max |
|
673 | )); |
||
674 | } |
||
675 | 24 | } |
|
676 | |||
677 | 36 | public static function fileExists($value, $message = '') |
|
678 | { |
||
679 | 36 | static::string($value); |
|
680 | |||
681 | 36 | if (!file_exists($value)) { |
|
682 | 12 | static::reportInvalidArgument(sprintf( |
|
683 | 12 | $message ?: 'The file %s does not exist.', |
|
684 | 12 | static::valueToString($value) |
|
685 | )); |
||
686 | } |
||
687 | 24 | } |
|
688 | |||
689 | 12 | View Code Duplication | public static function file($value, $message = '') |
690 | { |
||
691 | 12 | static::fileExists($value, $message); |
|
692 | |||
693 | 8 | if (!is_file($value)) { |
|
694 | 4 | static::reportInvalidArgument(sprintf( |
|
695 | 4 | $message ?: 'The path %s is not a file.', |
|
696 | 4 | static::valueToString($value) |
|
697 | )); |
||
698 | } |
||
699 | 4 | } |
|
700 | |||
701 | 12 | View Code Duplication | public static function directory($value, $message = '') |
702 | { |
||
703 | 12 | static::fileExists($value, $message); |
|
704 | |||
705 | 8 | if (!is_dir($value)) { |
|
706 | 4 | static::reportInvalidArgument(sprintf( |
|
707 | 4 | $message ?: 'The path %s is no directory.', |
|
708 | 4 | static::valueToString($value) |
|
709 | )); |
||
710 | } |
||
711 | 4 | } |
|
712 | |||
713 | public static function readable($value, $message = '') |
||
714 | { |
||
715 | if (!is_readable($value)) { |
||
716 | static::reportInvalidArgument(sprintf( |
||
717 | $message ?: 'The path %s is not readable.', |
||
718 | static::valueToString($value) |
||
719 | )); |
||
720 | } |
||
721 | } |
||
722 | |||
723 | public static function writable($value, $message = '') |
||
724 | { |
||
725 | if (!is_writable($value)) { |
||
726 | static::reportInvalidArgument(sprintf( |
||
727 | $message ?: 'The path %s is not writable.', |
||
728 | static::valueToString($value) |
||
729 | )); |
||
730 | } |
||
731 | } |
||
732 | |||
733 | 8 | public static function classExists($value, $message = '') |
|
734 | { |
||
735 | 8 | if (!class_exists($value)) { |
|
736 | 4 | static::reportInvalidArgument(sprintf( |
|
737 | 4 | $message ?: 'Expected an existing class name. Got: %s', |
|
738 | 4 | static::valueToString($value) |
|
739 | )); |
||
740 | } |
||
741 | 4 | } |
|
742 | |||
743 | 8 | public static function subclassOf($value, $class, $message = '') |
|
744 | { |
||
745 | 8 | if (!is_subclass_of($value, $class)) { |
|
746 | 4 | static::reportInvalidArgument(sprintf( |
|
747 | 4 | $message ?: 'Expected a sub-class of %2$s. Got: %s', |
|
748 | 4 | static::valueToString($value), |
|
749 | 4 | static::valueToString($class) |
|
750 | )); |
||
751 | } |
||
752 | 4 | } |
|
753 | |||
754 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
755 | { |
||
756 | 8 | if (!in_array($interface, class_implements($value))) { |
|
757 | 4 | static::reportInvalidArgument(sprintf( |
|
758 | 4 | $message ?: 'Expected an implementation of %2$s. Got: %s', |
|
759 | 4 | static::valueToString($value), |
|
760 | 4 | static::valueToString($interface) |
|
761 | )); |
||
762 | } |
||
763 | 4 | } |
|
764 | |||
765 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
766 | { |
||
767 | 12 | if (!property_exists($classOrObject, $property)) { |
|
768 | 4 | static::reportInvalidArgument(sprintf( |
|
769 | 4 | $message ?: 'Expected the property %s to exist.', |
|
770 | 4 | static::valueToString($property) |
|
771 | )); |
||
772 | } |
||
773 | 8 | } |
|
774 | |||
775 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
776 | { |
||
777 | 12 | if (property_exists($classOrObject, $property)) { |
|
778 | 8 | static::reportInvalidArgument(sprintf( |
|
779 | 8 | $message ?: 'Expected the property %s to not exist.', |
|
780 | 8 | static::valueToString($property) |
|
781 | )); |
||
782 | } |
||
783 | 4 | } |
|
784 | |||
785 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
786 | { |
||
787 | 27 | if (!method_exists($classOrObject, $method)) { |
|
788 | 19 | static::reportInvalidArgument(sprintf( |
|
789 | 19 | $message ?: 'Expected the method %s to exist.', |
|
790 | 19 | static::valueToString($method) |
|
791 | )); |
||
792 | } |
||
793 | 8 | } |
|
794 | |||
795 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
796 | { |
||
797 | 27 | if (method_exists($classOrObject, $method)) { |
|
798 | 8 | static::reportInvalidArgument(sprintf( |
|
799 | 8 | $message ?: 'Expected the method %s to not exist.', |
|
800 | 8 | static::valueToString($method) |
|
801 | )); |
||
802 | } |
||
803 | 19 | } |
|
804 | |||
805 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
806 | { |
||
807 | 12 | if (!array_key_exists($key, $array)) { |
|
808 | 4 | static::reportInvalidArgument(sprintf( |
|
809 | 4 | $message ?: 'Expected the key %s to exist.', |
|
810 | 4 | static::valueToString($key) |
|
811 | )); |
||
812 | } |
||
813 | 8 | } |
|
814 | |||
815 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
816 | { |
||
817 | 12 | if (array_key_exists($key, $array)) { |
|
818 | 8 | static::reportInvalidArgument(sprintf( |
|
819 | 8 | $message ?: 'Expected the key %s to not exist.', |
|
820 | 8 | static::valueToString($key) |
|
821 | )); |
||
822 | } |
||
823 | 4 | } |
|
824 | |||
825 | 8 | public static function count($array, $number, $message = '') |
|
826 | { |
||
827 | 8 | static::eq( |
|
828 | 8 | count($array), |
|
829 | 8 | $number, |
|
830 | 8 | $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array)) |
|
831 | ); |
||
832 | 4 | } |
|
833 | |||
834 | 48 | public static function uuid($value, $message = '') |
|
835 | { |
||
836 | 48 | $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); |
|
837 | |||
838 | // The nil UUID is special form of UUID that is specified to have all |
||
839 | // 128 bits set to zero. |
||
840 | 48 | if ('00000000-0000-0000-0000-000000000000' === $value) { |
|
841 | 4 | return; |
|
842 | } |
||
843 | |||
844 | 44 | 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)) { |
|
845 | 20 | static::reportInvalidArgument(sprintf( |
|
846 | 20 | $message ?: 'Value %s is not a valid UUID.', |
|
847 | 20 | static::valueToString($value) |
|
848 | )); |
||
849 | } |
||
850 | 24 | } |
|
851 | |||
852 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
853 | { |
||
854 | 24 | static::string($class); |
|
855 | |||
856 | 24 | $actual = 'none'; |
|
857 | |||
858 | try { |
||
859 | 24 | $expression(); |
|
860 | 24 | } catch (Exception $e) { |
|
861 | 20 | $actual = get_class($e); |
|
862 | 20 | if ($e instanceof $class) { |
|
863 | 20 | return; |
|
864 | } |
||
865 | 4 | } catch (Throwable $e) { |
|
866 | 4 | $actual = get_class($e); |
|
867 | 4 | if ($e instanceof $class) { |
|
868 | 4 | return; |
|
869 | } |
||
870 | } |
||
871 | |||
872 | 8 | static::reportInvalidArgument($message ?: sprintf( |
|
873 | 8 | 'Expected to throw "%s", got "%s"', |
|
874 | 8 | $class, |
|
875 | 8 | $actual |
|
876 | )); |
||
877 | } |
||
878 | |||
879 | 820 | public static function __callStatic($name, $arguments) |
|
880 | { |
||
881 | 820 | if ('nullOr' === substr($name, 0, 6)) { |
|
882 | 314 | if (null !== $arguments[0]) { |
|
883 | 243 | $method = lcfirst(substr($name, 6)); |
|
884 | 243 | call_user_func_array(array('static', $method), $arguments); |
|
885 | } |
||
886 | |||
887 | 193 | return; |
|
888 | } |
||
889 | |||
890 | 506 | if ('all' === substr($name, 0, 3)) { |
|
891 | 506 | static::isIterable($arguments[0]); |
|
892 | |||
893 | 506 | $method = lcfirst(substr($name, 3)); |
|
894 | 506 | $args = $arguments; |
|
895 | |||
896 | 506 | foreach ($arguments[0] as $entry) { |
|
897 | 506 | $args[0] = $entry; |
|
898 | |||
899 | 506 | call_user_func_array(array('static', $method), $args); |
|
900 | } |
||
901 | |||
902 | 250 | return; |
|
903 | } |
||
904 | |||
905 | throw new BadMethodCallException('No such method: '.$name); |
||
906 | } |
||
907 | |||
908 | 372 | protected static function valueToString($value) |
|
909 | { |
||
910 | 372 | if (null === $value) { |
|
911 | 11 | return 'null'; |
|
912 | } |
||
913 | |||
914 | 363 | if (true === $value) { |
|
915 | 15 | return 'true'; |
|
916 | } |
||
917 | |||
918 | 353 | if (false === $value) { |
|
919 | 13 | return 'false'; |
|
920 | } |
||
921 | |||
922 | 340 | if (is_array($value)) { |
|
923 | 1 | return 'array'; |
|
924 | } |
||
925 | |||
926 | 339 | if (is_object($value)) { |
|
927 | 1 | return get_class($value); |
|
928 | } |
||
929 | |||
930 | 338 | if (is_resource($value)) { |
|
931 | 1 | return 'resource'; |
|
932 | } |
||
933 | |||
934 | 338 | if (is_string($value)) { |
|
935 | 264 | return '"'.$value.'"'; |
|
936 | } |
||
937 | |||
938 | 82 | return (string) $value; |
|
939 | } |
||
940 | |||
941 | 137 | protected static function typeToString($value) |
|
942 | { |
||
943 | 137 | return is_object($value) ? get_class($value) : gettype($value); |
|
944 | } |
||
945 | |||
946 | 124 | protected static function strlen($value) |
|
947 | { |
||
948 | 124 | if (!function_exists('mb_detect_encoding')) { |
|
949 | return strlen($value); |
||
950 | } |
||
951 | |||
952 | 124 | if (false === $encoding = mb_detect_encoding($value)) { |
|
953 | return strlen($value); |
||
954 | } |
||
955 | |||
956 | 124 | return mb_strwidth($value, $encoding); |
|
957 | } |
||
958 | |||
959 | 520 | protected static function reportInvalidArgument($message) |
|
960 | { |
||
961 | 520 | throw new InvalidArgumentException($message); |
|
962 | } |
||
963 | |||
964 | private function __construct() |
||
965 | { |
||
966 | } |
||
967 | } |
||
968 |
If you suppress an error, we recommend checking for the error condition explicitly: