Complex classes like Assertion often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Assertion, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
179 | class Assertion |
||
180 | { |
||
181 | const INVALID_FLOAT = 9; |
||
182 | const INVALID_INTEGER = 10; |
||
183 | const INVALID_DIGIT = 11; |
||
184 | const INVALID_INTEGERISH = 12; |
||
185 | const INVALID_BOOLEAN = 13; |
||
186 | const VALUE_EMPTY = 14; |
||
187 | const VALUE_NULL = 15; |
||
188 | const VALUE_NOT_NULL = 25; |
||
189 | const INVALID_STRING = 16; |
||
190 | const INVALID_REGEX = 17; |
||
191 | const INVALID_MIN_LENGTH = 18; |
||
192 | const INVALID_MAX_LENGTH = 19; |
||
193 | const INVALID_STRING_START = 20; |
||
194 | const INVALID_STRING_CONTAINS = 21; |
||
195 | const INVALID_CHOICE = 22; |
||
196 | const INVALID_NUMERIC = 23; |
||
197 | const INVALID_ARRAY = 24; |
||
198 | const INVALID_KEY_EXISTS = 26; |
||
199 | const INVALID_NOT_BLANK = 27; |
||
200 | const INVALID_INSTANCE_OF = 28; |
||
201 | const INVALID_SUBCLASS_OF = 29; |
||
202 | const INVALID_RANGE = 30; |
||
203 | const INVALID_ALNUM = 31; |
||
204 | const INVALID_TRUE = 32; |
||
205 | const INVALID_EQ = 33; |
||
206 | const INVALID_SAME = 34; |
||
207 | const INVALID_MIN = 35; |
||
208 | const INVALID_MAX = 36; |
||
209 | const INVALID_LENGTH = 37; |
||
210 | const INVALID_FALSE = 38; |
||
211 | const INVALID_STRING_END = 39; |
||
212 | const INVALID_UUID = 40; |
||
213 | const INVALID_COUNT = 41; |
||
214 | const INVALID_NOT_EQ = 42; |
||
215 | const INVALID_NOT_SAME = 43; |
||
216 | const INVALID_TRAVERSABLE = 44; |
||
217 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
218 | const INVALID_KEY_ISSET = 46; |
||
219 | const INVALID_VALUE_IN_ARRAY = 47; |
||
220 | const INVALID_E164 = 48; |
||
221 | const INVALID_DIRECTORY = 101; |
||
222 | const INVALID_FILE = 102; |
||
223 | const INVALID_READABLE = 103; |
||
224 | const INVALID_WRITEABLE = 104; |
||
225 | const INVALID_CLASS = 105; |
||
226 | const INVALID_INTERFACE = 106; |
||
227 | const INVALID_EMAIL = 201; |
||
228 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
229 | const INVALID_URL = 203; |
||
230 | const INVALID_NOT_INSTANCE_OF = 204; |
||
231 | const VALUE_NOT_EMPTY = 205; |
||
232 | const INVALID_JSON_STRING = 206; |
||
233 | const INVALID_OBJECT = 207; |
||
234 | const INVALID_METHOD = 208; |
||
235 | const INVALID_SCALAR = 209; |
||
236 | const INVALID_LESS = 210; |
||
237 | const INVALID_LESS_OR_EQUAL = 211; |
||
238 | const INVALID_GREATER = 212; |
||
239 | const INVALID_GREATER_OR_EQUAL = 213; |
||
240 | const INVALID_DATE = 214; |
||
241 | const INVALID_CALLABLE = 215; |
||
242 | const INVALID_KEY_NOT_EXISTS = 216; |
||
243 | const INVALID_SATISFY = 217; |
||
244 | const INVALID_IP = 218; |
||
245 | const INVALID_BETWEEN = 219; |
||
246 | const INVALID_BETWEEN_EXCLUSIVE = 220; |
||
247 | const INVALID_EXTENSION = 222; |
||
248 | const INVALID_CONSTANT = 221; |
||
249 | const INVALID_VERSION = 223; |
||
250 | |||
251 | /** |
||
252 | * Exception to throw when an assertion failed. |
||
253 | * |
||
254 | * @var string |
||
255 | */ |
||
256 | protected static $exceptionClass = 'Assert\InvalidArgumentException'; |
||
257 | |||
258 | /** |
||
259 | * Helper method that handles building the assertion failure exceptions. |
||
260 | * They are returned from this method so that the stack trace still shows |
||
261 | * the assertions method. |
||
262 | * |
||
263 | * @param mixed $value |
||
264 | * @param string $message |
||
265 | * @param int $code |
||
266 | * @param string|null $propertyPath |
||
267 | * @param array $constraints |
||
268 | * |
||
269 | * @return mixed |
||
|
|||
270 | */ |
||
271 | protected static function createException($value, $message, $code, $propertyPath, array $constraints = array()) |
||
272 | { |
||
273 | $exceptionClass = static::$exceptionClass; |
||
274 | return new $exceptionClass($message, $code, $propertyPath, $value, $constraints); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Assert that two values are equal (using == ). |
||
279 | * |
||
280 | * @param mixed $value |
||
281 | * @param mixed $value2 |
||
282 | * @param string|null $message |
||
283 | * @param string|null $propertyPath |
||
284 | * @return bool |
||
285 | * @throws \Assert\AssertionFailedException |
||
286 | */ |
||
287 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
288 | { |
||
289 | if ($value != $value2) { |
||
290 | $message = sprintf( |
||
291 | $message ?: 'Value "%s" does not equal expected value "%s".', |
||
292 | static::stringify($value), |
||
293 | static::stringify($value2) |
||
294 | ); |
||
295 | |||
296 | throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, array('expected' => $value2)); |
||
297 | } |
||
298 | |||
299 | return true; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Assert that two values are the same (using ===). |
||
304 | * |
||
305 | * @param mixed $value |
||
306 | * @param mixed $value2 |
||
307 | * @param string|null $message |
||
308 | * @param string|null $propertyPath |
||
309 | * @return bool |
||
310 | * @throws \Assert\AssertionFailedException |
||
311 | */ |
||
312 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
313 | { |
||
314 | if ($value !== $value2) { |
||
315 | $message = sprintf( |
||
316 | $message ?: 'Value "%s" is not the same as expected value "%s".', |
||
317 | static::stringify($value), |
||
318 | static::stringify($value2) |
||
319 | ); |
||
320 | |||
321 | throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, array('expected' => $value2)); |
||
322 | } |
||
323 | |||
324 | return true; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Assert that two values are not equal (using == ). |
||
329 | * |
||
330 | * @param mixed $value1 |
||
331 | * @param mixed $value2 |
||
332 | * @param string|null $message |
||
333 | * @param string|null $propertyPath |
||
334 | * @return bool |
||
335 | * @throws \Assert\AssertionFailedException |
||
336 | */ |
||
337 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
338 | { |
||
339 | if ($value1 == $value2) { |
||
340 | $message = sprintf( |
||
341 | $message ?: 'Value "%s" is equal to expected value "%s".', |
||
342 | static::stringify($value1), |
||
343 | static::stringify($value2) |
||
344 | ); |
||
345 | throw static::createException($value1, $message, static::INVALID_NOT_EQ, $propertyPath, array('expected' => $value2)); |
||
346 | } |
||
347 | |||
348 | return true; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Assert that two values are not the same (using === ). |
||
353 | * |
||
354 | * @param mixed $value1 |
||
355 | * @param mixed $value2 |
||
356 | * @param string|null $message |
||
357 | * @param string|null $propertyPath |
||
358 | * @return bool |
||
359 | * @throws \Assert\AssertionFailedException |
||
360 | */ |
||
361 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
362 | { |
||
363 | if ($value1 === $value2) { |
||
364 | $message = sprintf( |
||
365 | $message ?: 'Value "%s" is the same as expected value "%s".', |
||
366 | static::stringify($value1), |
||
367 | static::stringify($value2) |
||
368 | ); |
||
369 | throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, array('expected' => $value2)); |
||
370 | } |
||
371 | |||
372 | return true; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Assert that value is not in array of choices. |
||
377 | * |
||
378 | * @param mixed $value |
||
379 | * @param array $choices |
||
380 | * @param string|null $message |
||
381 | * @param string|null $propertyPath |
||
382 | * @return bool |
||
383 | * @throws \Assert\AssertionFailedException |
||
384 | */ |
||
385 | public static function notInArray($value, array $choices, $message = null, $propertyPath = null) |
||
386 | { |
||
387 | if (in_array($value, $choices) === true) { |
||
388 | $message = sprintf( |
||
389 | $message ?: 'Value "%s" is in given "%s".', |
||
390 | static::stringify($value), |
||
391 | static::stringify($choices) |
||
392 | ); |
||
393 | throw static::createException($value, $message, static::INVALID_VALUE_IN_ARRAY, $propertyPath); |
||
394 | } |
||
395 | |||
396 | return true; |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Assert that value is a php integer. |
||
401 | * |
||
402 | * @param mixed $value |
||
403 | * @param string|null $message |
||
404 | * @param string|null $propertyPath |
||
405 | * @return bool |
||
406 | * @throws \Assert\AssertionFailedException |
||
407 | */ |
||
408 | public static function integer($value, $message = null, $propertyPath = null) |
||
421 | |||
422 | /** |
||
423 | * Assert that value is a php float. |
||
424 | * |
||
425 | * @param mixed $value |
||
426 | * @param string|null $message |
||
427 | * @param string|null $propertyPath |
||
428 | * @return bool |
||
429 | * @throws \Assert\AssertionFailedException |
||
430 | */ |
||
431 | public static function float($value, $message = null, $propertyPath = null) |
||
444 | |||
445 | /** |
||
446 | * Validates if an integer or integerish is a digit. |
||
447 | * |
||
448 | * @param mixed $value |
||
449 | * @param string|null $message |
||
450 | * @param string|null $propertyPath |
||
451 | * @return bool |
||
452 | * @throws \Assert\AssertionFailedException |
||
453 | */ |
||
454 | public static function digit($value, $message = null, $propertyPath = null) |
||
467 | |||
468 | /** |
||
469 | * Assert that value is a php integer'ish. |
||
470 | * |
||
471 | * @param mixed $value |
||
472 | * @param string|null $message |
||
473 | * @param string|null $propertyPath |
||
474 | * @return bool |
||
475 | * @throws \Assert\AssertionFailedException |
||
476 | */ |
||
477 | public static function integerish($value, $message = null, $propertyPath = null) |
||
490 | |||
491 | /** |
||
492 | * Assert that value is php boolean |
||
493 | * |
||
494 | * @param mixed $value |
||
495 | * @param string|null $message |
||
496 | * @param string|null $propertyPath |
||
497 | * @return bool |
||
498 | * @throws \Assert\AssertionFailedException |
||
499 | */ |
||
500 | public static function boolean($value, $message = null, $propertyPath = null) |
||
513 | |||
514 | /** |
||
515 | * Assert that value is a PHP scalar |
||
516 | * |
||
517 | * @param mixed $value |
||
518 | * @param string|null $message |
||
519 | * @param string|null $propertyPath |
||
520 | * @return bool |
||
521 | * @throws \Assert\AssertionFailedException |
||
522 | */ |
||
523 | public static function scalar($value, $message = null, $propertyPath = null) |
||
536 | |||
537 | /** |
||
538 | * Assert that value is not empty |
||
539 | * |
||
540 | * @param mixed $value |
||
541 | * @param string|null $message |
||
542 | * @param string|null $propertyPath |
||
543 | * @return bool |
||
544 | * @throws \Assert\AssertionFailedException |
||
545 | */ |
||
546 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
559 | |||
560 | /** |
||
561 | * Assert that value is empty |
||
562 | * |
||
563 | * @param mixed $value |
||
564 | * @param string|null $message |
||
565 | * @param string|null $propertyPath |
||
566 | * @return bool |
||
567 | * @throws \Assert\AssertionFailedException |
||
568 | */ |
||
569 | public static function noContent($value, $message = null, $propertyPath = null) |
||
582 | |||
583 | /** |
||
584 | * Assert that value is null |
||
585 | * |
||
586 | * @param mixed $value |
||
587 | * @param string|null $message |
||
588 | * @param string|null $propertyPath |
||
589 | * @return bool |
||
590 | * @throws \Assert\AssertionFailedException |
||
591 | */ |
||
592 | public static function null($value, $message = null, $propertyPath = null) |
||
605 | |||
606 | /** |
||
607 | * Assert that value is not null |
||
608 | * |
||
609 | * @param mixed $value |
||
610 | * @param string|null $message |
||
611 | * @param string|null $propertyPath |
||
612 | * @return bool |
||
613 | * @throws \Assert\AssertionFailedException |
||
614 | */ |
||
615 | public static function notNull($value, $message = null, $propertyPath = null) |
||
628 | |||
629 | /** |
||
630 | * Assert that value is a string |
||
631 | * |
||
632 | * @param mixed $value |
||
633 | * @param string|null $message |
||
634 | * @param string|null $propertyPath |
||
635 | * @return bool |
||
636 | * @throws \Assert\AssertionFailedException |
||
637 | */ |
||
638 | public static function string($value, $message = null, $propertyPath = null) |
||
652 | |||
653 | /** |
||
654 | * Assert that value matches a regex |
||
655 | * |
||
656 | * @param mixed $value |
||
657 | * @param string $pattern |
||
658 | * @param string|null $message |
||
659 | * @param string|null $propertyPath |
||
660 | * @return bool |
||
661 | * @throws \Assert\AssertionFailedException |
||
662 | */ |
||
663 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
678 | |||
679 | /** |
||
680 | * Assert that string has a given length. |
||
681 | * |
||
682 | * @param mixed $value |
||
683 | * @param int $length |
||
684 | * @param string|null $message |
||
685 | * @param string|null $propertyPath |
||
686 | * @param string $encoding |
||
687 | * @return bool |
||
688 | * @throws \Assert\AssertionFailedException |
||
689 | */ |
||
690 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
708 | |||
709 | /** |
||
710 | * Assert that a string is at least $minLength chars long. |
||
711 | * |
||
712 | * @param mixed $value |
||
713 | * @param int $minLength |
||
714 | * @param string|null $message |
||
715 | * @param string|null $propertyPath |
||
716 | * @param string $encoding |
||
717 | * @return bool |
||
718 | * @throws \Assert\AssertionFailedException |
||
719 | */ |
||
720 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
738 | |||
739 | /** |
||
740 | * Assert that string value is not longer than $maxLength chars. |
||
741 | * |
||
742 | * @param mixed $value |
||
743 | * @param integer $maxLength |
||
744 | * @param string|null $message |
||
745 | * @param string|null $propertyPath |
||
746 | * @param string $encoding |
||
747 | * @return bool |
||
748 | * @throws \Assert\AssertionFailedException |
||
749 | */ |
||
750 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
768 | |||
769 | /** |
||
770 | * Assert that string length is between min,max lengths. |
||
771 | * |
||
772 | * @param mixed $value |
||
773 | * @param integer $minLength |
||
774 | * @param integer $maxLength |
||
775 | * @param string|null $message |
||
776 | * @param string|null $propertyPath |
||
777 | * @param string $encoding |
||
778 | * @return bool |
||
779 | * @throws \Assert\AssertionFailedException |
||
780 | */ |
||
781 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
811 | |||
812 | /** |
||
813 | * Assert that string starts with a sequence of chars. |
||
814 | * |
||
815 | * @param mixed $string |
||
816 | * @param string $needle |
||
817 | * @param string|null $message |
||
818 | * @param string|null $propertyPath |
||
819 | * @param string $encoding |
||
820 | * @return bool |
||
821 | * @throws \Assert\AssertionFailedException |
||
822 | */ |
||
823 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
840 | |||
841 | /** |
||
842 | * Assert that string ends with a sequence of chars. |
||
843 | * |
||
844 | * @param mixed $string |
||
845 | * @param string $needle |
||
846 | * @param string|null $message |
||
847 | * @param string|null $propertyPath |
||
848 | * @param string $encoding |
||
849 | * @return bool |
||
850 | * @throws \Assert\AssertionFailedException |
||
851 | */ |
||
852 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
871 | |||
872 | /** |
||
873 | * Assert that string contains a sequence of chars. |
||
874 | * |
||
875 | * @param mixed $string |
||
876 | * @param string $needle |
||
877 | * @param string|null $message |
||
878 | * @param string|null $propertyPath |
||
879 | * @param string $encoding |
||
880 | * @return bool |
||
881 | * @throws \Assert\AssertionFailedException |
||
882 | */ |
||
883 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
900 | |||
901 | /** |
||
902 | * Assert that value is in array of choices. |
||
903 | * |
||
904 | * @param mixed $value |
||
905 | * @param array $choices |
||
906 | * @param string|null $message |
||
907 | * @param string|null $propertyPath |
||
908 | * @return bool |
||
909 | * @throws \Assert\AssertionFailedException |
||
910 | */ |
||
911 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
925 | |||
926 | /** |
||
927 | * Alias of {@see choice()} |
||
928 | * |
||
929 | * @param mixed $value |
||
930 | * @param array $choices |
||
931 | * @param string|null $message |
||
932 | * @param string|null $propertyPath |
||
933 | * @return bool |
||
934 | */ |
||
935 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
939 | |||
940 | /** |
||
941 | * Assert that value is numeric. |
||
942 | * |
||
943 | * @param mixed $value |
||
944 | * @param string|null $message |
||
945 | * @param string|null $propertyPath |
||
946 | * @return bool |
||
947 | * @throws \Assert\AssertionFailedException |
||
948 | */ |
||
949 | public static function numeric($value, $message = null, $propertyPath = null) |
||
962 | |||
963 | /** |
||
964 | * Assert that value is an array. |
||
965 | * |
||
966 | * @param mixed $value |
||
967 | * @param string|null $message |
||
968 | * @param string|null $propertyPath |
||
969 | * @return bool |
||
970 | * @throws \Assert\AssertionFailedException |
||
971 | */ |
||
972 | public static function isArray($value, $message = null, $propertyPath = null) |
||
985 | |||
986 | /** |
||
987 | * Assert that value is an array or a traversable object. |
||
988 | * |
||
989 | * @param mixed $value |
||
990 | * @param string|null $message |
||
991 | * @param string|null $propertyPath |
||
992 | * @return bool |
||
993 | * @throws \Assert\AssertionFailedException |
||
994 | */ |
||
995 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
1008 | |||
1009 | /** |
||
1010 | * Assert that value is an array or an array-accessible object. |
||
1011 | * |
||
1012 | * @param mixed $value |
||
1013 | * @param string|null $message |
||
1014 | * @param string|null $propertyPath |
||
1015 | * @return bool |
||
1016 | * @throws \Assert\AssertionFailedException |
||
1017 | */ |
||
1018 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
1031 | |||
1032 | /** |
||
1033 | * Assert that key exists in an array |
||
1034 | * |
||
1035 | * @param mixed $value |
||
1036 | * @param string|integer $key |
||
1037 | * @param string|null $message |
||
1038 | * @param string|null $propertyPath |
||
1039 | * @return bool |
||
1040 | * @throws \Assert\AssertionFailedException |
||
1041 | */ |
||
1042 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
1057 | |||
1058 | /** |
||
1059 | * Assert that key does not exist in an array |
||
1060 | * |
||
1061 | * @param mixed $value |
||
1062 | * @param string|integer $key |
||
1063 | * @param string|null $message |
||
1064 | * @param string|null $propertyPath |
||
1065 | * @return bool |
||
1066 | * @throws \Assert\AssertionFailedException |
||
1067 | */ |
||
1068 | public static function keyNotExists($value, $key, $message = null, $propertyPath = null) |
||
1083 | |||
1084 | /** |
||
1085 | * Assert that key exists in an array/array-accessible object using isset() |
||
1086 | * |
||
1087 | * @param mixed $value |
||
1088 | * @param string|integer $key |
||
1089 | * @param string|null $message |
||
1090 | * @param string|null $propertyPath |
||
1091 | * @return bool |
||
1092 | * @throws \Assert\AssertionFailedException |
||
1093 | */ |
||
1094 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
1109 | |||
1110 | /** |
||
1111 | * Assert that key exists in an array/array-accessible object and its value is not empty. |
||
1112 | * |
||
1113 | * @param mixed $value |
||
1114 | * @param string|integer $key |
||
1115 | * @param string|null $message |
||
1116 | * @param string|null $propertyPath |
||
1117 | * @return bool |
||
1118 | * @throws \Assert\AssertionFailedException |
||
1119 | */ |
||
1120 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
1127 | |||
1128 | /** |
||
1129 | * Assert that value is not blank |
||
1130 | * |
||
1131 | * @param mixed $value |
||
1132 | * @param string|null $message |
||
1133 | * @param string|null $propertyPath |
||
1134 | * @return bool |
||
1135 | * @throws \Assert\AssertionFailedException |
||
1136 | */ |
||
1137 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
1150 | |||
1151 | /** |
||
1152 | * Assert that value is instance of given class-name. |
||
1153 | * |
||
1154 | * @param mixed $value |
||
1155 | * @param string $className |
||
1156 | * @param string|null $message |
||
1157 | * @param string|null $propertyPath |
||
1158 | * @return bool |
||
1159 | * @throws \Assert\AssertionFailedException |
||
1160 | */ |
||
1161 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
1175 | |||
1176 | /** |
||
1177 | * Assert that value is not instance of given class-name. |
||
1178 | * |
||
1179 | * @param mixed $value |
||
1180 | * @param string $className |
||
1181 | * @param string|null $message |
||
1182 | * @param string|null $propertyPath |
||
1183 | * @return bool |
||
1184 | * @throws \Assert\AssertionFailedException |
||
1185 | */ |
||
1186 | public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
1200 | |||
1201 | /** |
||
1202 | * Assert that value is subclass of given class-name. |
||
1203 | * |
||
1204 | * @param mixed $value |
||
1205 | * @param string $className |
||
1206 | * @param string|null $message |
||
1207 | * @param string|null $propertyPath |
||
1208 | * @return bool |
||
1209 | * @throws \Assert\AssertionFailedException |
||
1210 | */ |
||
1211 | public static function subclassOf($value, $className, $message = null, $propertyPath = null) |
||
1225 | |||
1226 | /** |
||
1227 | * Assert that value is in range of numbers. |
||
1228 | * |
||
1229 | * @param mixed $value |
||
1230 | * @param mixed $minValue |
||
1231 | * @param mixed $maxValue |
||
1232 | * @param string|null $message |
||
1233 | * @param string|null $propertyPath |
||
1234 | * @return bool |
||
1235 | * @throws \Assert\AssertionFailedException |
||
1236 | */ |
||
1237 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
1254 | |||
1255 | /** |
||
1256 | * Assert that a value is at least as big as a given limit |
||
1257 | * |
||
1258 | * @param mixed $value |
||
1259 | * @param mixed $minValue |
||
1260 | * @param string|null $message |
||
1261 | * @param string|null $propertyPath |
||
1262 | * @return bool |
||
1263 | * @throws \Assert\AssertionFailedException |
||
1264 | */ |
||
1265 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
1281 | |||
1282 | /** |
||
1283 | * Assert that a number is smaller as a given limit |
||
1284 | * |
||
1285 | * @param mixed $value |
||
1286 | * @param mixed $maxValue |
||
1287 | * @param string|null $message |
||
1288 | * @param string|null $propertyPath |
||
1289 | * @return bool |
||
1290 | * @throws \Assert\AssertionFailedException |
||
1291 | */ |
||
1292 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
1308 | |||
1309 | /** |
||
1310 | * Assert that a file exists |
||
1311 | * |
||
1312 | * @param string $value |
||
1313 | * @param string|null $message |
||
1314 | * @param string|null $propertyPath |
||
1315 | * @return bool |
||
1316 | * @throws \Assert\AssertionFailedException |
||
1317 | */ |
||
1318 | public static function file($value, $message = null, $propertyPath = null) |
||
1334 | |||
1335 | /** |
||
1336 | * Assert that a directory exists |
||
1337 | * |
||
1338 | * @param string $value |
||
1339 | * @param string|null $message |
||
1340 | * @param string|null $propertyPath |
||
1341 | * @return bool |
||
1342 | * @throws \Assert\AssertionFailedException |
||
1343 | */ |
||
1344 | public static function directory($value, $message = null, $propertyPath = null) |
||
1359 | |||
1360 | /** |
||
1361 | * Assert that the value is something readable |
||
1362 | * |
||
1363 | * @param string $value |
||
1364 | * @param string|null $message |
||
1365 | * @param string|null $propertyPath |
||
1366 | * @return bool |
||
1367 | * @throws \Assert\AssertionFailedException |
||
1368 | */ |
||
1369 | public static function readable($value, $message = null, $propertyPath = null) |
||
1384 | |||
1385 | /** |
||
1386 | * Assert that the value is something writeable |
||
1387 | * |
||
1388 | * @param string $value |
||
1389 | * @param string|null $message |
||
1390 | * @param string|null $propertyPath |
||
1391 | * @return bool |
||
1392 | * @throws \Assert\AssertionFailedException |
||
1393 | */ |
||
1394 | public static function writeable($value, $message = null, $propertyPath = null) |
||
1409 | |||
1410 | /** |
||
1411 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
1412 | * |
||
1413 | * @param mixed $value |
||
1414 | * @param string|null $message |
||
1415 | * @param string|null $propertyPath |
||
1416 | * @return bool |
||
1417 | * @throws \Assert\AssertionFailedException |
||
1418 | */ |
||
1419 | public static function email($value, $message = null, $propertyPath = null) |
||
1446 | |||
1447 | /** |
||
1448 | * Assert that value is an URL. |
||
1449 | * |
||
1450 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
1451 | * |
||
1452 | * @param mixed $value |
||
1453 | * @param string|null $message |
||
1454 | * @param string|null $propertyPath |
||
1455 | * @return bool |
||
1456 | * @throws \Assert\AssertionFailedException |
||
1457 | * |
||
1458 | * |
||
1459 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
1460 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
1461 | */ |
||
1462 | public static function url($value, $message = null, $propertyPath = null) |
||
1497 | |||
1498 | /** |
||
1499 | * Assert that value is alphanumeric. |
||
1500 | * |
||
1501 | * @param mixed $value |
||
1502 | * @param string|null $message |
||
1503 | * @param string|null $propertyPath |
||
1504 | * @return bool |
||
1505 | * @throws \Assert\AssertionFailedException |
||
1506 | */ |
||
1507 | public static function alnum($value, $message = null, $propertyPath = null) |
||
1522 | |||
1523 | /** |
||
1524 | * Assert that the value is boolean True. |
||
1525 | * |
||
1526 | * @param mixed $value |
||
1527 | * @param string|null $message |
||
1528 | * @param string|null $propertyPath |
||
1529 | * @return bool |
||
1530 | * @throws \Assert\AssertionFailedException |
||
1531 | */ |
||
1532 | public static function true($value, $message = null, $propertyPath = null) |
||
1545 | |||
1546 | /** |
||
1547 | * Assert that the value is boolean False. |
||
1548 | * |
||
1549 | * @param mixed $value |
||
1550 | * @param string|null $message |
||
1551 | * @param string|null $propertyPath |
||
1552 | * @return bool |
||
1553 | * @throws \Assert\AssertionFailedException |
||
1554 | */ |
||
1555 | public static function false($value, $message = null, $propertyPath = null) |
||
1568 | |||
1569 | /** |
||
1570 | * Assert that the class exists. |
||
1571 | * |
||
1572 | * @param mixed $value |
||
1573 | * @param string|null $message |
||
1574 | * @param string|null $propertyPath |
||
1575 | * @return bool |
||
1576 | * @throws \Assert\AssertionFailedException |
||
1577 | */ |
||
1578 | public static function classExists($value, $message = null, $propertyPath = null) |
||
1591 | |||
1592 | /** |
||
1593 | * Assert that the interface exists. |
||
1594 | * |
||
1595 | * @param mixed $value |
||
1596 | * @param string|null $message |
||
1597 | * @param string|null $propertyPath |
||
1598 | * @return bool |
||
1599 | * @throws \Assert\AssertionFailedException |
||
1600 | */ |
||
1601 | public static function interfaceExists($value, $message = null, $propertyPath = null) |
||
1614 | |||
1615 | /** |
||
1616 | * Assert that the class implements the interface |
||
1617 | * |
||
1618 | * @param mixed $class |
||
1619 | * @param string $interfaceName |
||
1620 | * @param string|null $message |
||
1621 | * @param string|null $propertyPath |
||
1622 | * @return bool |
||
1623 | * @throws \Assert\AssertionFailedException |
||
1624 | */ |
||
1625 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
1640 | |||
1641 | /** |
||
1642 | * Assert that the given string is a valid json string. |
||
1643 | * |
||
1644 | * NOTICE: |
||
1645 | * Since this does a json_decode to determine its validity |
||
1646 | * you probably should consider, when using the variable |
||
1647 | * content afterwards, just to decode and check for yourself instead |
||
1648 | * of using this assertion. |
||
1649 | * |
||
1650 | * @param mixed $value |
||
1651 | * @param string|null $message |
||
1652 | * @param string|null $propertyPath |
||
1653 | * @return bool |
||
1654 | * @throws \Assert\AssertionFailedException |
||
1655 | */ |
||
1656 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
1669 | |||
1670 | /** |
||
1671 | * Assert that the given string is a valid UUID |
||
1672 | * |
||
1673 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
1674 | * |
||
1675 | * @param string $value |
||
1676 | * @param string|null $message |
||
1677 | * @param string|null $propertyPath |
||
1678 | * @return bool |
||
1679 | * @throws \Assert\AssertionFailedException |
||
1680 | */ |
||
1681 | public static function uuid($value, $message = null, $propertyPath = null) |
||
1700 | |||
1701 | /** |
||
1702 | * Assert that the given string is a valid E164 Phone Number |
||
1703 | * |
||
1704 | * @link https://en.wikipedia.org/wiki/E.164 |
||
1705 | * |
||
1706 | * @param string $value |
||
1707 | * @param string|null $message |
||
1708 | * @param string|null $propertyPath |
||
1709 | * @return bool |
||
1710 | * @throws \Assert\AssertionFailedException |
||
1711 | */ |
||
1712 | public static function e164($value, $message = null, $propertyPath = null) |
||
1725 | |||
1726 | /** |
||
1727 | * Assert that the count of countable is equal to count. |
||
1728 | * |
||
1729 | * @param array|\Countable $countable |
||
1730 | * @param int $count |
||
1731 | * @param string|null $message |
||
1732 | * @param string|null $propertyPath |
||
1733 | * @return bool |
||
1734 | * @throws \Assert\AssertionFailedException |
||
1735 | */ |
||
1736 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
1749 | |||
1750 | /** |
||
1751 | * static call handler to implement: |
||
1752 | * - "null or assertion" delegation |
||
1753 | * - "all" delegation |
||
1754 | * |
||
1755 | * @param string $method |
||
1756 | * @param array $args |
||
1757 | * |
||
1758 | * @return bool|mixed |
||
1759 | */ |
||
1760 | public static function __callStatic($method, $args) |
||
1796 | |||
1797 | /** |
||
1798 | * Determines if the values array has every choice as key and that this choice has content. |
||
1799 | * |
||
1800 | * @param array $values |
||
1801 | * @param array $choices |
||
1802 | * @param string|null $message |
||
1803 | * @param string|null $propertyPath |
||
1804 | * @return bool |
||
1805 | */ |
||
1806 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
1816 | |||
1817 | /** |
||
1818 | * Determines that the named method is defined in the provided object. |
||
1819 | * |
||
1820 | * @param string $value |
||
1821 | * @param mixed $object |
||
1822 | * @param string|null $message |
||
1823 | * @param string|null $propertyPath |
||
1824 | * @return bool |
||
1825 | * @throws |
||
1826 | */ |
||
1827 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
1842 | |||
1843 | /** |
||
1844 | * Determines that the provided value is an object. |
||
1845 | * |
||
1846 | * @param mixed $value |
||
1847 | * @param string|null $message |
||
1848 | * @param string|null $propertyPath |
||
1849 | * @return bool |
||
1850 | */ |
||
1851 | public static function isObject($value, $message = null, $propertyPath = null) |
||
1864 | |||
1865 | /** |
||
1866 | * Determines if the value is less than given limit. |
||
1867 | * |
||
1868 | * @param mixed $value |
||
1869 | * @param mixed $limit |
||
1870 | * @param string|null $message |
||
1871 | * @param string|null $propertyPath |
||
1872 | * @return bool |
||
1873 | */ |
||
1874 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
1888 | |||
1889 | /** |
||
1890 | * Determines if the value is less or than given limit. |
||
1891 | * |
||
1892 | * @param mixed $value |
||
1893 | * @param mixed $limit |
||
1894 | * @param string|null $message |
||
1895 | * @param string|null $propertyPath |
||
1896 | * @return bool |
||
1897 | */ |
||
1898 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
1912 | |||
1913 | /** |
||
1914 | * Determines if the value is greater than given limit. |
||
1915 | * |
||
1916 | * @param mixed $value |
||
1917 | * @param mixed $limit |
||
1918 | * @param string|null $message |
||
1919 | * @param string|null $propertyPath |
||
1920 | * @return bool |
||
1921 | */ |
||
1922 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
1936 | |||
1937 | /** |
||
1938 | * Determines if the value is greater or equal than given limit. |
||
1939 | * |
||
1940 | * @param mixed $value |
||
1941 | * @param mixed $limit |
||
1942 | * @param string|null $message |
||
1943 | * @param string|null $propertyPath |
||
1944 | * @return bool |
||
1945 | */ |
||
1946 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
1960 | |||
1961 | /** |
||
1962 | * Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. |
||
1963 | * |
||
1964 | * @param mixed $value |
||
1965 | * @param mixed $lowerLimit |
||
1966 | * @param mixed $upperLimit |
||
1967 | * @param string $message |
||
1968 | * @param string $propertyPath |
||
1969 | * @return bool |
||
1970 | */ |
||
1971 | public static function between($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
1986 | |||
1987 | /** |
||
1988 | * Assert that a value is greater than a lower limit, and less than an upper limit. |
||
1989 | * |
||
1990 | * @param mixed $value |
||
1991 | * @param mixed $lowerLimit |
||
1992 | * @param mixed $upperLimit |
||
1993 | * @param string $message |
||
1994 | * @param string $propertyPath |
||
1995 | * @return bool |
||
1996 | */ |
||
1997 | public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
2012 | |||
2013 | /** |
||
2014 | * Assert that extension is loaded. |
||
2015 | * |
||
2016 | * @param mixed $value |
||
2017 | * @param string|null $message |
||
2018 | * @param string|null $propertyPath |
||
2019 | * @return bool |
||
2020 | * @throws \Assert\AssertionFailedException |
||
2021 | */ |
||
2022 | public static function extensionLoaded($value, $message = null, $propertyPath = null) |
||
2035 | |||
2036 | /** |
||
2037 | * Assert comparison of two versions. |
||
2038 | * |
||
2039 | * @param string $version1 |
||
2040 | * @param string $operator |
||
2041 | * @param string $version2 |
||
2042 | * @param string|null $message |
||
2043 | * @param string|null $propertyPath |
||
2044 | * @return bool |
||
2045 | * @throws \Assert\AssertionFailedException |
||
2046 | */ |
||
2047 | public static function version($version1, $operator, $version2, $message = null, $propertyPath = null) |
||
2064 | |||
2065 | /** |
||
2066 | * Assert on PHP version. |
||
2067 | * |
||
2068 | * @param string $operator |
||
2069 | * @param mixed $version |
||
2070 | * @param string|null $message |
||
2071 | * @param string|null $propertyPath |
||
2072 | * @return bool |
||
2073 | * @throws \Assert\AssertionFailedException |
||
2074 | */ |
||
2075 | public static function phpVersion($operator, $version, $message = null, $propertyPath = null) |
||
2081 | |||
2082 | /** |
||
2083 | * Assert that extension is loaded and a specific version is installed. |
||
2084 | * |
||
2085 | * @param string $extension |
||
2086 | * @param string $operator |
||
2087 | * @param mixed $version |
||
2088 | * @param string|null $message |
||
2089 | * @param string|null $propertyPath |
||
2090 | * @return bool |
||
2091 | * @throws \Assert\AssertionFailedException |
||
2092 | */ |
||
2093 | public static function extensionVersion($extension, $operator, $version, $message = null, $propertyPath = null) |
||
2099 | |||
2100 | /** |
||
2101 | * Assert that date is valid and corresponds to the given format. |
||
2102 | * |
||
2103 | * @param string $value |
||
2104 | * @param string $format supports all of the options date(), except for the following: |
||
2105 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r. |
||
2106 | * @param string|null $message |
||
2107 | * @param string|null $propertyPath |
||
2108 | * @return bool |
||
2109 | * |
||
2110 | * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
2111 | */ |
||
2112 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
2131 | |||
2132 | /** |
||
2133 | * Determines that the provided value is callable. |
||
2134 | * |
||
2135 | * @param mixed $value |
||
2136 | * @param string|null $message |
||
2137 | * @param string|null $propertyPath |
||
2138 | * @return bool |
||
2139 | */ |
||
2140 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
2153 | |||
2154 | /** |
||
2155 | * Assert that the provided value is valid according to a callback. |
||
2156 | * |
||
2157 | * If the callback returns `false` the assertion will fail. |
||
2158 | * |
||
2159 | * @param mixed $value |
||
2160 | * @param callable $callback |
||
2161 | * @param string|null $message |
||
2162 | * @param string|null $propertyPath |
||
2163 | * @return bool |
||
2164 | */ |
||
2165 | public static function satisfy($value, $callback, $message = null, $propertyPath = null) |
||
2180 | |||
2181 | /** |
||
2182 | * Assert that value is an IPv4 or IPv6 address |
||
2183 | * (using input_filter/FILTER_VALIDATE_IP). |
||
2184 | * |
||
2185 | * @param string $value |
||
2186 | * @param null|int $flag |
||
2187 | * @param string|null $message |
||
2188 | * @param string|null $propertyPath |
||
2189 | * @return bool |
||
2190 | * |
||
2191 | * @link http://php.net/manual/filter.filters.flags.php |
||
2192 | */ |
||
2193 | public static function ip($value, $flag = null, $message = null, $propertyPath = null) |
||
2206 | |||
2207 | /** |
||
2208 | * Assert that value is an IPv4 address |
||
2209 | * (using input_filter/FILTER_VALIDATE_IP). |
||
2210 | * |
||
2211 | * @param string $value |
||
2212 | * @param null|int $flag |
||
2213 | * @param string|null $message |
||
2214 | * @param string|null $propertyPath |
||
2215 | * @return bool |
||
2216 | * |
||
2217 | * @link http://php.net/manual/filter.filters.flags.php |
||
2218 | */ |
||
2219 | public static function ipv4($value, $flag = null, $message = null, $propertyPath = null) |
||
2225 | |||
2226 | /** |
||
2227 | * Assert that value is an IPv6 address |
||
2228 | * (using input_filter/FILTER_VALIDATE_IP). |
||
2229 | * |
||
2230 | * @param string $value |
||
2231 | * @param null|int $flag |
||
2232 | * @param string|null $message |
||
2233 | * @param string|null $propertyPath |
||
2234 | * @return bool |
||
2235 | * |
||
2236 | * @link http://php.net/manual/filter.filters.flags.php |
||
2237 | */ |
||
2238 | public static function ipv6($value, $flag = null, $message = null, $propertyPath = null) |
||
2244 | |||
2245 | /** |
||
2246 | * Make a string version of a value. |
||
2247 | * |
||
2248 | * @param mixed $value |
||
2249 | * @return string |
||
2250 | */ |
||
2251 | protected static function stringify($value) |
||
2285 | |||
2286 | /** |
||
2287 | * Assert that a constant is defined. |
||
2288 | * |
||
2289 | * @param mixed $constant |
||
2290 | * @param string|null $message |
||
2291 | * @param string|null $propertyPath |
||
2292 | * @return bool |
||
2293 | * @throws \Assert\AssertionFailedException |
||
2294 | */ |
||
2295 | public static function defined($constant, $message = null, $propertyPath = null) |
||
2305 | } |
||
2306 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.