@@ 782-791 (lines=10) @@ | ||
779 | * @param null $exception |
|
780 | * @throws Exception |
|
781 | */ |
|
782 | public static function eq($value, $expect, $message = '', $exception = null) |
|
783 | { |
|
784 | if ($expect != $value) { |
|
785 | static::throwException(\sprintf( |
|
786 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
787 | static::valueToString($value), |
|
788 | static::valueToString($expect) |
|
789 | ), $exception); |
|
790 | } |
|
791 | } |
|
792 | ||
793 | /** |
|
794 | * @param mixed $value |
|
@@ 821-830 (lines=10) @@ | ||
818 | * @param null $exception |
|
819 | * @throws Exception |
|
820 | */ |
|
821 | public static function same($value, $expect, $message = '', $exception = null) |
|
822 | { |
|
823 | if ($expect !== $value) { |
|
824 | static::throwException(\sprintf( |
|
825 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
826 | static::valueToString($value), |
|
827 | static::valueToString($expect) |
|
828 | ), $exception); |
|
829 | } |
|
830 | } |
|
831 | ||
832 | /** |
|
833 | * @param mixed $value |
|
@@ 856-865 (lines=10) @@ | ||
853 | * @param null $exception |
|
854 | * @throws Exception |
|
855 | */ |
|
856 | public static function greaterThan($value, $limit, $message = '', $exception = null) |
|
857 | { |
|
858 | if ($value <= $limit) { |
|
859 | static::throwException(\sprintf( |
|
860 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
861 | static::valueToString($value), |
|
862 | static::valueToString($limit) |
|
863 | ), $exception); |
|
864 | } |
|
865 | } |
|
866 | ||
867 | /** |
|
868 | * @param mixed $value |
|
@@ 874-883 (lines=10) @@ | ||
871 | * @param null $exception |
|
872 | * @throws Exception |
|
873 | */ |
|
874 | public static function greaterThanEq($value, $limit, $message = '', $exception = null) |
|
875 | { |
|
876 | if ($value < $limit) { |
|
877 | static::throwException(\sprintf( |
|
878 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
879 | static::valueToString($value), |
|
880 | static::valueToString($limit) |
|
881 | ), $exception); |
|
882 | } |
|
883 | } |
|
884 | ||
885 | /** |
|
886 | * @param mixed $value |
|
@@ 892-901 (lines=10) @@ | ||
889 | * @param null $exception |
|
890 | * @throws Exception |
|
891 | */ |
|
892 | public static function lessThan($value, $limit, $message = '', $exception = null) |
|
893 | { |
|
894 | if ($value >= $limit) { |
|
895 | static::throwException(\sprintf( |
|
896 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
897 | static::valueToString($value), |
|
898 | static::valueToString($limit) |
|
899 | ), $exception); |
|
900 | } |
|
901 | } |
|
902 | ||
903 | /** |
|
904 | * @param mixed $value |
|
@@ 910-919 (lines=10) @@ | ||
907 | * @param null $exception |
|
908 | * @throws Exception |
|
909 | */ |
|
910 | public static function lessThanEq($value, $limit, $message = '', $exception = null) |
|
911 | { |
|
912 | if ($value > $limit) { |
|
913 | static::throwException(\sprintf( |
|
914 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
915 | static::valueToString($value), |
|
916 | static::valueToString($limit) |
|
917 | ), $exception); |
|
918 | } |
|
919 | } |
|
920 | ||
921 | /** |
|
922 | * Inclusive range, so Assert::(3, 3, 5) passes. |
|
@@ 974-983 (lines=10) @@ | ||
971 | * @param null $exception |
|
972 | * @throws Exception |
|
973 | */ |
|
974 | public static function contains($value, $subString, $message = '', $exception = null) |
|
975 | { |
|
976 | if (false === \strpos($value, $subString)) { |
|
977 | static::throwException(\sprintf( |
|
978 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
|
979 | static::valueToString($value), |
|
980 | static::valueToString($subString) |
|
981 | ), $exception); |
|
982 | } |
|
983 | } |
|
984 | ||
985 | /** |
|
986 | * @param mixed $value |
|
@@ 992-1001 (lines=10) @@ | ||
989 | * @param null $exception |
|
990 | * @throws Exception |
|
991 | */ |
|
992 | public static function notContains($value, $subString, $message = '', $exception = null) |
|
993 | { |
|
994 | if (false !== \strpos($value, $subString)) { |
|
995 | static::throwException(\sprintf( |
|
996 | $message ?: '%2$s was not expected to be contained in a value. Got: %s', |
|
997 | static::valueToString($value), |
|
998 | static::valueToString($subString) |
|
999 | ), $exception); |
|
1000 | } |
|
1001 | } |
|
1002 | ||
1003 | /** |
|
1004 | * @param mixed $value |
|
@@ 1026-1035 (lines=10) @@ | ||
1023 | * @param null $exception |
|
1024 | * @throws Exception |
|
1025 | */ |
|
1026 | public static function startsWith($value, $prefix, $message = '', $exception = null) |
|
1027 | { |
|
1028 | if (0 !== \strpos($value, $prefix)) { |
|
1029 | static::throwException(\sprintf( |
|
1030 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
|
1031 | static::valueToString($value), |
|
1032 | static::valueToString($prefix) |
|
1033 | ), $exception); |
|
1034 | } |
|
1035 | } |
|
1036 | ||
1037 | /** |
|
1038 | * @param mixed $value |
|
@@ 1248-1257 (lines=10) @@ | ||
1245 | * @param null $exception |
|
1246 | * @throws Exception |
|
1247 | */ |
|
1248 | public static function length($value, $length, $message = '', $exception = null) |
|
1249 | { |
|
1250 | if ($length !== static::strlen($value)) { |
|
1251 | static::throwException(\sprintf( |
|
1252 | $message ?: 'Expected a value to contain %2$s characters. Got: %s', |
|
1253 | static::valueToString($value), |
|
1254 | $length |
|
1255 | ), $exception); |
|
1256 | } |
|
1257 | } |
|
1258 | ||
1259 | /** |
|
1260 | * Inclusive min. |
|
@@ 1268-1277 (lines=10) @@ | ||
1265 | * @param null $exception |
|
1266 | * @throws Exception |
|
1267 | */ |
|
1268 | public static function minLength($value, $min, $message = '', $exception = null) |
|
1269 | { |
|
1270 | if (static::strlen($value) < $min) { |
|
1271 | static::throwException(\sprintf( |
|
1272 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
|
1273 | static::valueToString($value), |
|
1274 | $min |
|
1275 | ), $exception); |
|
1276 | } |
|
1277 | } |
|
1278 | ||
1279 | /** |
|
1280 | * Inclusive max. |
|
@@ 1288-1297 (lines=10) @@ | ||
1285 | * @param null $exception |
|
1286 | * @throws Exception |
|
1287 | */ |
|
1288 | public static function maxLength($value, $max, $message = '', $exception = null) |
|
1289 | { |
|
1290 | if (static::strlen($value) > $max) { |
|
1291 | static::throwException(\sprintf( |
|
1292 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
|
1293 | static::valueToString($value), |
|
1294 | $max |
|
1295 | ), $exception); |
|
1296 | } |
|
1297 | } |
|
1298 | ||
1299 | /** |
|
1300 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
|
@@ 1436-1445 (lines=10) @@ | ||
1433 | * @param null $exception |
|
1434 | * @throws Exception |
|
1435 | */ |
|
1436 | public static function subclassOf($value, $class, $message = '', $exception = null) |
|
1437 | { |
|
1438 | if (!\is_subclass_of($value, $class)) { |
|
1439 | static::throwException(\sprintf( |
|
1440 | $message ?: 'Expected a sub-class of %2$s. Got: %s', |
|
1441 | static::valueToString($value), |
|
1442 | static::valueToString($class) |
|
1443 | ), $exception); |
|
1444 | } |
|
1445 | } |
|
1446 | ||
1447 | /** |
|
1448 | * @psalm-assert class-string $value |