| @@ 939-949 (lines=11) @@ | ||
| 936 | * |
|
| 937 | * @throws InvalidArgumentException |
|
| 938 | */ |
|
| 939 | public static function range($value, $min, $max, $message = '') |
|
| 940 | { |
|
| 941 | if ($value < $min || $value > $max) { |
|
| 942 | static::reportInvalidArgument(\sprintf( |
|
| 943 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
|
| 944 | static::valueToString($value), |
|
| 945 | static::valueToString($min), |
|
| 946 | static::valueToString($max) |
|
| 947 | )); |
|
| 948 | } |
|
| 949 | } |
|
| 950 | ||
| 951 | /** |
|
| 952 | * Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion. |
|
| @@ 1283-1294 (lines=12) @@ | ||
| 1280 | * |
|
| 1281 | * @throws InvalidArgumentException |
|
| 1282 | */ |
|
| 1283 | public static function length($value, $length, $message = '') |
|
| 1284 | { |
|
| 1285 | static::string($value, $message); |
|
| 1286 | ||
| 1287 | if ($length !== static::strlen($value)) { |
|
| 1288 | static::reportInvalidArgument(\sprintf( |
|
| 1289 | $message ?: 'Expected a value to contain %2$s characters. Got: %s', |
|
| 1290 | static::valueToString($value), |
|
| 1291 | $length |
|
| 1292 | )); |
|
| 1293 | } |
|
| 1294 | } |
|
| 1295 | ||
| 1296 | /** |
|
| 1297 | * Inclusive min. |
|
| @@ 1305-1315 (lines=11) @@ | ||
| 1302 | * |
|
| 1303 | * @throws InvalidArgumentException |
|
| 1304 | */ |
|
| 1305 | public static function minLength($value, $min, $message = '') |
|
| 1306 | { |
|
| 1307 | static::string($value); |
|
| 1308 | if (static::strlen($value) < $min) { |
|
| 1309 | static::reportInvalidArgument(\sprintf( |
|
| 1310 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
|
| 1311 | static::valueToString($value), |
|
| 1312 | $min |
|
| 1313 | )); |
|
| 1314 | } |
|
| 1315 | } |
|
| 1316 | ||
| 1317 | /** |
|
| 1318 | * Inclusive max. |
|
| @@ 1326-1337 (lines=12) @@ | ||
| 1323 | * |
|
| 1324 | * @throws InvalidArgumentException |
|
| 1325 | */ |
|
| 1326 | public static function maxLength($value, $max, $message = '') |
|
| 1327 | { |
|
| 1328 | static::string($value, $message); |
|
| 1329 | ||
| 1330 | if (static::strlen($value) > $max) { |
|
| 1331 | static::reportInvalidArgument(\sprintf( |
|
| 1332 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
|
| 1333 | static::valueToString($value), |
|
| 1334 | $max |
|
| 1335 | )); |
|
| 1336 | } |
|
| 1337 | } |
|
| 1338 | ||
| 1339 | /** |
|
| 1340 | * Inclusive, so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
|