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 InvalidArgumentException; |
||
16 | use Traversable; |
||
17 | use Exception; |
||
18 | use Throwable; |
||
19 | use Closure; |
||
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 | 86 | public static function string($value, $message = '') |
|
152 | { |
||
153 | 86 | 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 | 72 | } |
|
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 | $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 | 504 | public static function isTraversable($value, $message = '') |
|
276 | { |
||
277 | 504 | if (!is_array($value) && !($value instanceof Traversable)) { |
|
278 | 8 | static::reportInvalidArgument(sprintf( |
|
279 | 8 | $message ?: 'Expected a traversable. Got: %s', |
|
280 | 8 | static::typeToString($value) |
|
281 | )); |
||
282 | } |
||
283 | 500 | } |
|
284 | |||
285 | 16 | View Code Duplication | public static function isInstanceOf($value, $class, $message = '') |
286 | { |
||
287 | 16 | if (!($value instanceof $class)) { |
|
288 | 12 | static::reportInvalidArgument(sprintf( |
|
289 | 12 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
290 | 12 | static::typeToString($value), |
|
291 | $class |
||
292 | )); |
||
293 | } |
||
294 | 4 | } |
|
295 | |||
296 | 16 | View Code Duplication | public static function notInstanceOf($value, $class, $message = '') |
297 | { |
||
298 | 16 | if ($value instanceof $class) { |
|
299 | 4 | static::reportInvalidArgument(sprintf( |
|
300 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
301 | 4 | static::typeToString($value), |
|
302 | $class |
||
303 | )); |
||
304 | } |
||
305 | 12 | } |
|
306 | |||
307 | 23 | public static function isEmpty($value, $message = '') |
|
308 | { |
||
309 | 23 | if (!empty($value)) { |
|
310 | 8 | static::reportInvalidArgument(sprintf( |
|
311 | 8 | $message ?: 'Expected an empty value. Got: %s', |
|
312 | 8 | static::valueToString($value) |
|
313 | )); |
||
314 | } |
||
315 | 15 | } |
|
316 | |||
317 | 31 | public static function notEmpty($value, $message = '') |
|
318 | { |
||
319 | 31 | if (empty($value)) { |
|
320 | 19 | static::reportInvalidArgument(sprintf( |
|
321 | 19 | $message ?: 'Expected a non-empty value. Got: %s', |
|
322 | 19 | static::valueToString($value) |
|
323 | )); |
||
324 | } |
||
325 | 12 | } |
|
326 | |||
327 | 11 | public static function null($value, $message = '') |
|
328 | { |
||
329 | 11 | if (null !== $value) { |
|
330 | 8 | static::reportInvalidArgument(sprintf( |
|
331 | 8 | $message ?: 'Expected null. Got: %s', |
|
332 | 8 | static::valueToString($value) |
|
333 | )); |
||
334 | } |
||
335 | 3 | } |
|
336 | |||
337 | 11 | public static function notNull($value, $message = '') |
|
338 | { |
||
339 | 11 | if (null === $value) { |
|
340 | 3 | static::reportInvalidArgument( |
|
341 | 3 | $message ?: 'Expected a value other than null.' |
|
342 | ); |
||
343 | } |
||
344 | 8 | } |
|
345 | |||
346 | 15 | public static function true($value, $message = '') |
|
347 | { |
||
348 | 15 | if (true !== $value) { |
|
349 | 11 | static::reportInvalidArgument(sprintf( |
|
350 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
351 | 11 | static::valueToString($value) |
|
352 | )); |
||
353 | } |
||
354 | 4 | } |
|
355 | |||
356 | 19 | public static function false($value, $message = '') |
|
357 | { |
||
358 | 19 | if (false !== $value) { |
|
359 | 15 | static::reportInvalidArgument(sprintf( |
|
360 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
361 | 15 | static::valueToString($value) |
|
362 | )); |
||
363 | } |
||
364 | 4 | } |
|
365 | |||
366 | 32 | public static function eq($value, $value2, $message = '') |
|
367 | { |
||
368 | 32 | if ($value2 != $value) { |
|
369 | 16 | static::reportInvalidArgument(sprintf( |
|
370 | 16 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
371 | 16 | static::valueToString($value), |
|
372 | 16 | static::valueToString($value2) |
|
373 | )); |
||
374 | } |
||
375 | 16 | } |
|
376 | |||
377 | 16 | public static function notEq($value, $value2, $message = '') |
|
378 | { |
||
379 | 16 | if ($value2 == $value) { |
|
380 | 12 | static::reportInvalidArgument(sprintf( |
|
381 | 12 | $message ?: 'Expected a different value than %s.', |
|
382 | 12 | static::valueToString($value2) |
|
383 | )); |
||
384 | } |
||
385 | 4 | } |
|
386 | |||
387 | 16 | public static function same($value, $value2, $message = '') |
|
388 | { |
||
389 | 16 | if ($value2 !== $value) { |
|
390 | 12 | static::reportInvalidArgument(sprintf( |
|
391 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
392 | 12 | static::valueToString($value), |
|
393 | 12 | static::valueToString($value2) |
|
394 | )); |
||
395 | } |
||
396 | 4 | } |
|
397 | |||
398 | 16 | public static function notSame($value, $value2, $message = '') |
|
399 | { |
||
400 | 16 | if ($value2 === $value) { |
|
401 | 4 | static::reportInvalidArgument(sprintf( |
|
402 | 4 | $message ?: 'Expected a value not identical to %s.', |
|
403 | 4 | static::valueToString($value2) |
|
404 | )); |
||
405 | } |
||
406 | 12 | } |
|
407 | |||
408 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
409 | { |
||
410 | 8 | if ($value <= $limit) { |
|
411 | 4 | static::reportInvalidArgument(sprintf( |
|
412 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
413 | 4 | static::valueToString($value), |
|
414 | 4 | static::valueToString($limit) |
|
415 | )); |
||
416 | } |
||
417 | 4 | } |
|
418 | |||
419 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
420 | { |
||
421 | 12 | if ($value < $limit) { |
|
422 | 4 | static::reportInvalidArgument(sprintf( |
|
423 | 4 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
424 | 4 | static::valueToString($value), |
|
425 | 4 | static::valueToString($limit) |
|
426 | )); |
||
427 | } |
||
428 | 8 | } |
|
429 | |||
430 | 8 | public static function lessThan($value, $limit, $message = '') |
|
431 | { |
||
432 | 8 | if ($value >= $limit) { |
|
433 | 4 | static::reportInvalidArgument(sprintf( |
|
434 | 4 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
435 | 4 | static::valueToString($value), |
|
436 | 4 | static::valueToString($limit) |
|
437 | )); |
||
438 | } |
||
439 | 4 | } |
|
440 | |||
441 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
442 | { |
||
443 | 12 | if ($value > $limit) { |
|
444 | 4 | static::reportInvalidArgument(sprintf( |
|
445 | 4 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
446 | 4 | static::valueToString($value), |
|
447 | 4 | static::valueToString($limit) |
|
448 | )); |
||
449 | } |
||
450 | 8 | } |
|
451 | |||
452 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
453 | { |
||
454 | 16 | if ($value < $min || $value > $max) { |
|
455 | 8 | static::reportInvalidArgument(sprintf( |
|
456 | 8 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
|
457 | 8 | static::valueToString($value), |
|
458 | 8 | static::valueToString($min), |
|
459 | 8 | static::valueToString($max) |
|
460 | )); |
||
461 | } |
||
462 | 8 | } |
|
463 | |||
464 | 8 | public static function oneOf($value, array $values, $message = '') |
|
465 | { |
||
466 | 8 | if (!in_array($value, $values, true)) { |
|
467 | 4 | static::reportInvalidArgument(sprintf( |
|
468 | 4 | $message ?: 'Expected one of: %2$s. Got: %s', |
|
469 | 4 | static::valueToString($value), |
|
470 | 4 | implode(', ', array_map(array('static', 'valueToString'), $values)) |
|
471 | )); |
||
472 | } |
||
473 | 4 | } |
|
474 | |||
475 | 20 | View Code Duplication | public static function contains($value, $subString, $message = '') |
476 | { |
||
477 | 20 | if (false === strpos($value, $subString)) { |
|
478 | 8 | static::reportInvalidArgument(sprintf( |
|
479 | 8 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
|
480 | 8 | static::valueToString($value), |
|
481 | 8 | static::valueToString($subString) |
|
482 | )); |
||
483 | } |
||
484 | 12 | } |
|
485 | |||
486 | 12 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
487 | { |
||
488 | 12 | if (0 !== strpos($value, $prefix)) { |
|
489 | 8 | static::reportInvalidArgument(sprintf( |
|
490 | 8 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
|
491 | 8 | static::valueToString($value), |
|
492 | 8 | static::valueToString($prefix) |
|
493 | )); |
||
494 | } |
||
495 | 4 | } |
|
496 | |||
497 | 12 | public static function startsWithLetter($value, $message = '') |
|
498 | { |
||
499 | 12 | $valid = isset($value[0]); |
|
500 | |||
501 | 12 | if ($valid) { |
|
502 | 8 | $locale = setlocale(LC_CTYPE, 0); |
|
503 | 8 | setlocale(LC_CTYPE, 'C'); |
|
504 | 8 | $valid = ctype_alpha($value[0]); |
|
505 | 8 | setlocale(LC_CTYPE, $locale); |
|
506 | } |
||
507 | |||
508 | 12 | if (!$valid) { |
|
509 | 8 | static::reportInvalidArgument(sprintf( |
|
510 | 8 | $message ?: 'Expected a value to start with a letter. Got: %s', |
|
511 | 8 | static::valueToString($value) |
|
512 | )); |
||
513 | } |
||
514 | 4 | } |
|
515 | |||
516 | 12 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
517 | { |
||
518 | 12 | if ($suffix !== substr($value, -static::strlen($suffix))) { |
|
519 | 8 | static::reportInvalidArgument(sprintf( |
|
520 | 8 | $message ?: 'Expected a value to end with %2$s. Got: %s', |
|
521 | 8 | static::valueToString($value), |
|
522 | 8 | static::valueToString($suffix) |
|
523 | )); |
||
524 | } |
||
525 | 4 | } |
|
526 | |||
527 | 12 | public static function regex($value, $pattern, $message = '') |
|
528 | { |
||
529 | 12 | if (!preg_match($pattern, $value)) { |
|
530 | 8 | static::reportInvalidArgument(sprintf( |
|
531 | 8 | $message ?: 'The value %s does not match the expected pattern.', |
|
532 | 8 | static::valueToString($value) |
|
533 | )); |
||
534 | } |
||
535 | 4 | } |
|
536 | |||
537 | 12 | View Code Duplication | public static function alpha($value, $message = '') |
538 | { |
||
539 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
540 | 12 | setlocale(LC_CTYPE, 'C'); |
|
541 | 12 | $valid = !ctype_alpha($value); |
|
542 | 12 | setlocale(LC_CTYPE, $locale); |
|
543 | |||
544 | 12 | if ($valid) { |
|
545 | 8 | static::reportInvalidArgument(sprintf( |
|
546 | 8 | $message ?: 'Expected a value to contain only letters. Got: %s', |
|
547 | 8 | static::valueToString($value) |
|
548 | )); |
||
549 | } |
||
550 | 4 | } |
|
551 | |||
552 | 12 | View Code Duplication | public static function digits($value, $message = '') |
553 | { |
||
554 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
555 | 12 | setlocale(LC_CTYPE, 'C'); |
|
556 | 12 | $valid = !ctype_digit($value); |
|
557 | 12 | setlocale(LC_CTYPE, $locale); |
|
558 | |||
559 | 12 | if ($valid) { |
|
560 | 8 | static::reportInvalidArgument(sprintf( |
|
561 | 8 | $message ?: 'Expected a value to contain digits only. Got: %s', |
|
562 | 8 | static::valueToString($value) |
|
563 | )); |
||
564 | } |
||
565 | 4 | } |
|
566 | |||
567 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
568 | { |
||
569 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
570 | 12 | setlocale(LC_CTYPE, 'C'); |
|
571 | 12 | $valid = !ctype_alnum($value); |
|
572 | 12 | setlocale(LC_CTYPE, $locale); |
|
573 | |||
574 | 12 | if ($valid) { |
|
575 | 8 | static::reportInvalidArgument(sprintf( |
|
576 | 8 | $message ?: 'Expected a value to contain letters and digits only. Got: %s', |
|
577 | 8 | static::valueToString($value) |
|
578 | )); |
||
579 | } |
||
580 | 4 | } |
|
581 | |||
582 | 16 | View Code Duplication | public static function lower($value, $message = '') |
583 | { |
||
584 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
585 | 16 | setlocale(LC_CTYPE, 'C'); |
|
586 | 16 | $valid = !ctype_lower($value); |
|
587 | 16 | setlocale(LC_CTYPE, $locale); |
|
588 | |||
589 | 16 | if ($valid) { |
|
590 | 12 | static::reportInvalidArgument(sprintf( |
|
591 | 12 | $message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
|
592 | 12 | static::valueToString($value) |
|
593 | )); |
||
594 | } |
||
595 | 4 | } |
|
596 | |||
597 | 16 | View Code Duplication | public static function upper($value, $message = '') |
598 | { |
||
599 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
600 | 16 | setlocale(LC_CTYPE, 'C'); |
|
601 | 16 | $valid = !ctype_upper($value); |
|
602 | 16 | setlocale(LC_CTYPE, $locale); |
|
603 | |||
604 | 16 | if ($valid) { |
|
605 | 12 | static::reportInvalidArgument(sprintf( |
|
606 | 12 | $message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
|
607 | 12 | static::valueToString($value) |
|
608 | )); |
||
609 | } |
||
610 | 4 | } |
|
611 | |||
612 | 24 | public static function length($value, $length, $message = '') |
|
613 | { |
||
614 | 24 | if ($length !== static::strlen($value)) { |
|
615 | 16 | static::reportInvalidArgument(sprintf( |
|
616 | 16 | $message ?: 'Expected a value to contain %2$s characters. Got: %s', |
|
617 | 16 | static::valueToString($value), |
|
618 | $length |
||
619 | )); |
||
620 | } |
||
621 | 8 | } |
|
622 | |||
623 | 24 | public static function minLength($value, $min, $message = '') |
|
624 | { |
||
625 | 24 | if (static::strlen($value) < $min) { |
|
626 | 8 | static::reportInvalidArgument(sprintf( |
|
627 | 8 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
|
628 | 8 | static::valueToString($value), |
|
629 | $min |
||
630 | )); |
||
631 | } |
||
632 | 16 | } |
|
633 | |||
634 | 24 | public static function maxLength($value, $max, $message = '') |
|
635 | { |
||
636 | 24 | if (static::strlen($value) > $max) { |
|
637 | 8 | static::reportInvalidArgument(sprintf( |
|
638 | 8 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
|
639 | 8 | static::valueToString($value), |
|
640 | $max |
||
641 | )); |
||
642 | } |
||
643 | 16 | } |
|
644 | |||
645 | 40 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
646 | { |
||
647 | 40 | $length = static::strlen($value); |
|
648 | |||
649 | 40 | if ($length < $min || $length > $max) { |
|
650 | 16 | static::reportInvalidArgument(sprintf( |
|
651 | 16 | $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
|
652 | 16 | static::valueToString($value), |
|
653 | $min, |
||
654 | $max |
||
655 | )); |
||
656 | } |
||
657 | 24 | } |
|
658 | |||
659 | 36 | public static function fileExists($value, $message = '') |
|
660 | { |
||
661 | 36 | static::string($value); |
|
662 | |||
663 | 36 | if (!file_exists($value)) { |
|
664 | 12 | static::reportInvalidArgument(sprintf( |
|
665 | 12 | $message ?: 'The file %s does not exist.', |
|
666 | 12 | static::valueToString($value) |
|
667 | )); |
||
668 | } |
||
669 | 24 | } |
|
670 | |||
671 | 12 | View Code Duplication | public static function file($value, $message = '') |
672 | { |
||
673 | 12 | static::fileExists($value, $message); |
|
674 | |||
675 | 8 | if (!is_file($value)) { |
|
676 | 4 | static::reportInvalidArgument(sprintf( |
|
677 | 4 | $message ?: 'The path %s is not a file.', |
|
678 | 4 | static::valueToString($value) |
|
679 | )); |
||
680 | } |
||
681 | 4 | } |
|
682 | |||
683 | 12 | View Code Duplication | public static function directory($value, $message = '') |
684 | { |
||
685 | 12 | static::fileExists($value, $message); |
|
686 | |||
687 | 8 | if (!is_dir($value)) { |
|
688 | 4 | static::reportInvalidArgument(sprintf( |
|
689 | 4 | $message ?: 'The path %s is no directory.', |
|
690 | 4 | static::valueToString($value) |
|
691 | )); |
||
692 | } |
||
693 | 4 | } |
|
694 | |||
695 | public static function readable($value, $message = '') |
||
696 | { |
||
697 | if (!is_readable($value)) { |
||
698 | static::reportInvalidArgument(sprintf( |
||
699 | $message ?: 'The path %s is not readable.', |
||
700 | static::valueToString($value) |
||
701 | )); |
||
702 | } |
||
703 | } |
||
704 | |||
705 | public static function writable($value, $message = '') |
||
706 | { |
||
707 | if (!is_writable($value)) { |
||
708 | static::reportInvalidArgument(sprintf( |
||
709 | $message ?: 'The path %s is not writable.', |
||
710 | static::valueToString($value) |
||
711 | )); |
||
712 | } |
||
713 | } |
||
714 | |||
715 | 8 | public static function classExists($value, $message = '') |
|
716 | { |
||
717 | 8 | if (!class_exists($value)) { |
|
718 | 4 | static::reportInvalidArgument(sprintf( |
|
719 | 4 | $message ?: 'Expected an existing class name. Got: %s', |
|
720 | 4 | static::valueToString($value) |
|
721 | )); |
||
722 | } |
||
723 | 4 | } |
|
724 | |||
725 | 8 | public static function subclassOf($value, $class, $message = '') |
|
726 | { |
||
727 | 8 | if (!is_subclass_of($value, $class)) { |
|
728 | 4 | static::reportInvalidArgument(sprintf( |
|
729 | 4 | $message ?: 'Expected a sub-class of %2$s. Got: %s', |
|
730 | 4 | static::valueToString($value), |
|
731 | 4 | static::valueToString($class) |
|
732 | )); |
||
733 | } |
||
734 | 4 | } |
|
735 | |||
736 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
737 | { |
||
738 | 8 | if (!in_array($interface, class_implements($value))) { |
|
739 | 4 | static::reportInvalidArgument(sprintf( |
|
740 | 4 | $message ?: 'Expected an implementation of %2$s. Got: %s', |
|
741 | 4 | static::valueToString($value), |
|
742 | 4 | static::valueToString($interface) |
|
743 | )); |
||
744 | } |
||
745 | 4 | } |
|
746 | |||
747 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
748 | { |
||
749 | 12 | if (!property_exists($classOrObject, $property)) { |
|
750 | 4 | static::reportInvalidArgument(sprintf( |
|
751 | 4 | $message ?: 'Expected the property %s to exist.', |
|
752 | 4 | static::valueToString($property) |
|
753 | )); |
||
754 | } |
||
755 | 8 | } |
|
756 | |||
757 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
758 | { |
||
759 | 12 | if (property_exists($classOrObject, $property)) { |
|
760 | 8 | static::reportInvalidArgument(sprintf( |
|
761 | 8 | $message ?: 'Expected the property %s to not exist.', |
|
762 | 8 | static::valueToString($property) |
|
763 | )); |
||
764 | } |
||
765 | 4 | } |
|
766 | |||
767 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
768 | { |
||
769 | 27 | if (!method_exists($classOrObject, $method)) { |
|
770 | 19 | static::reportInvalidArgument(sprintf( |
|
771 | 19 | $message ?: 'Expected the method %s to exist.', |
|
772 | 19 | static::valueToString($method) |
|
773 | )); |
||
774 | } |
||
775 | 8 | } |
|
776 | |||
777 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
778 | { |
||
779 | 27 | if (method_exists($classOrObject, $method)) { |
|
780 | 8 | static::reportInvalidArgument(sprintf( |
|
781 | 8 | $message ?: 'Expected the method %s to not exist.', |
|
782 | 8 | static::valueToString($method) |
|
783 | )); |
||
784 | } |
||
785 | 19 | } |
|
786 | |||
787 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
788 | { |
||
789 | 12 | if (!array_key_exists($key, $array)) { |
|
790 | 4 | static::reportInvalidArgument(sprintf( |
|
791 | 4 | $message ?: 'Expected the key %s to exist.', |
|
792 | 4 | static::valueToString($key) |
|
793 | )); |
||
794 | } |
||
795 | 8 | } |
|
796 | |||
797 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
798 | { |
||
799 | 12 | if (array_key_exists($key, $array)) { |
|
800 | 8 | static::reportInvalidArgument(sprintf( |
|
801 | 8 | $message ?: 'Expected the key %s to not exist.', |
|
802 | 8 | static::valueToString($key) |
|
803 | )); |
||
804 | } |
||
805 | 4 | } |
|
806 | |||
807 | 8 | public static function count($array, $number, $message = '') |
|
808 | { |
||
809 | 8 | static::eq( |
|
810 | count($array), |
||
811 | $number, |
||
812 | 8 | $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array)) |
|
813 | ); |
||
814 | 4 | } |
|
815 | |||
816 | 48 | public static function uuid($value, $message = '') |
|
817 | { |
||
818 | 48 | $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); |
|
819 | |||
820 | // The nil UUID is special form of UUID that is specified to have all |
||
821 | // 128 bits set to zero. |
||
822 | 48 | if ('00000000-0000-0000-0000-000000000000' === $value) { |
|
823 | 4 | return; |
|
824 | } |
||
825 | |||
826 | 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)) { |
|
827 | 20 | static::reportInvalidArgument(sprintf( |
|
828 | 20 | $message ?: 'Value %s is not a valid UUID.', |
|
829 | 20 | static::valueToString($value) |
|
830 | )); |
||
831 | } |
||
832 | 24 | } |
|
833 | |||
834 | 20 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
835 | { |
||
836 | 20 | static::string($class); |
|
837 | |||
838 | 20 | $actual = 'none'; |
|
839 | try { |
||
840 | 20 | $expression(); |
|
841 | 20 | } catch (Exception $e) { |
|
842 | 20 | $actual = get_class($e); |
|
843 | 20 | if ($e instanceof $class) { |
|
844 | 20 | return; |
|
845 | } |
||
846 | } catch (Throwable $e) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
847 | $actual = get_class($e); |
||
848 | if ($e instanceof $class) { |
||
849 | return; |
||
850 | } |
||
851 | } |
||
852 | |||
853 | 8 | static::reportInvalidArgument($message ?: sprintf( |
|
854 | 8 | 'Expected expression to throw an exception of type "%s", got "%s"', |
|
855 | $class, |
||
856 | $actual |
||
857 | )); |
||
858 | } |
||
859 | |||
860 | 801 | public static function __callStatic($name, $arguments) |
|
861 | { |
||
862 | 801 | if ('nullOr' === substr($name, 0, 6)) { |
|
863 | 307 | if (null !== $arguments[0]) { |
|
864 | 237 | $method = lcfirst(substr($name, 6)); |
|
865 | 237 | call_user_func_array(array('static', $method), $arguments); |
|
866 | } |
||
867 | |||
868 | 188 | return; |
|
869 | } |
||
870 | |||
871 | 494 | if ('all' === substr($name, 0, 3)) { |
|
872 | 494 | static::isTraversable($arguments[0]); |
|
873 | |||
874 | 494 | $method = lcfirst(substr($name, 3)); |
|
875 | 494 | $args = $arguments; |
|
876 | |||
877 | 494 | foreach ($arguments[0] as $entry) { |
|
878 | 494 | $args[0] = $entry; |
|
879 | |||
880 | 494 | call_user_func_array(array('static', $method), $args); |
|
881 | } |
||
882 | |||
883 | 242 | return; |
|
884 | } |
||
885 | |||
886 | throw new BadMethodCallException('No such method: '.$name); |
||
887 | } |
||
888 | |||
889 | 372 | protected static function valueToString($value) |
|
890 | { |
||
891 | 372 | if (null === $value) { |
|
892 | 11 | return 'null'; |
|
893 | } |
||
894 | |||
895 | 363 | if (true === $value) { |
|
896 | 15 | return 'true'; |
|
897 | } |
||
898 | |||
899 | 353 | if (false === $value) { |
|
900 | 13 | return 'false'; |
|
901 | } |
||
902 | |||
903 | 340 | if (is_array($value)) { |
|
904 | 1 | return 'array'; |
|
905 | } |
||
906 | |||
907 | 339 | if (is_object($value)) { |
|
908 | 1 | return get_class($value); |
|
909 | } |
||
910 | |||
911 | 338 | if (is_resource($value)) { |
|
912 | 1 | return 'resource'; |
|
913 | } |
||
914 | |||
915 | 338 | if (is_string($value)) { |
|
916 | 264 | return '"'.$value.'"'; |
|
917 | } |
||
918 | |||
919 | 82 | return (string) $value; |
|
920 | } |
||
921 | |||
922 | 129 | protected static function typeToString($value) |
|
923 | { |
||
924 | 129 | return is_object($value) ? get_class($value) : gettype($value); |
|
925 | } |
||
926 | |||
927 | 124 | protected static function strlen($value) |
|
928 | { |
||
929 | 124 | if (!function_exists('mb_detect_encoding')) { |
|
930 | return strlen($value); |
||
931 | } |
||
932 | |||
933 | 124 | if (false === $encoding = mb_detect_encoding($value)) { |
|
934 | return strlen($value); |
||
935 | } |
||
936 | |||
937 | 124 | return mb_strwidth($value, $encoding); |
|
938 | } |
||
939 | |||
940 | 512 | protected static function reportInvalidArgument($message) |
|
941 | { |
||
942 | 512 | throw new InvalidArgumentException($message); |
|
943 | } |
||
944 | |||
945 | private function __construct() |
||
946 | { |
||
947 | } |
||
948 | } |
||
949 |