From 99c10d7073c0a5589a96d8d5218e84aec72c6ab9 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Fri, 22 Oct 2021 15:26:36 +0200 Subject: [PATCH] Add `Assert::numericString()` --- CHANGELOG.md | 2 + README.md | 1 + src/Assert.php | 20 ++++++++ src/Mixin.php | 34 ++++++++++++++ .../static-analysis/assert-numericString.php | 47 +++++++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 tests/static-analysis/assert-numericString.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7789d82..b4d49a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ Changelog ## UNRELEASED +* Added `Assert::numericString()` + ## 1.11.0 ### Added diff --git a/README.md b/README.md index 766099c..e4c5452 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ Method | Description -------------------------------------------------------- | -------------------------------------------------- `string($value, $message = '')` | Check that a value is a string `stringNotEmpty($value, $message = '')` | Check that a value is a non-empty string +`numericString($value, $message = '')` | Check that a value is a numeric-string `integer($value, $message = '')` | Check that a value is an integer `integerish($value, $message = '')` | Check that a value casts to an integer `positiveInteger($value, $message = '')` | Check that a value is a positive (non-zero) integer diff --git a/src/Assert.php b/src/Assert.php index db1f3a5..f809fd3 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -53,6 +53,26 @@ public static function string($value, $message = '') } } + /** + * @psalm-pure + * @psalm-assert numeric-string $value + * + * @param mixed $value + * + * @throws InvalidArgumentException + */ + public static function numericString($value, string $message = ''): void + { + static::string($value, $message ?: 'Expected a numeric string. Got: %s'); + + if (!\is_numeric($value)) { + static::reportInvalidArgument(\sprintf( + $message ?: 'Expected a numeric string. Got: %s', + static::typeToString($value) + )); + } + } + /** * @psalm-pure * @psalm-assert non-empty-string $value diff --git a/src/Mixin.php b/src/Mixin.php index 5670e2a..92c6196 100644 --- a/src/Mixin.php +++ b/src/Mixin.php @@ -69,6 +69,40 @@ public static function allNullOrString($value, $message = '') } } + /** + * @psalm-pure + * @psalm-assert numeric-string|null $value + * + * @param mixed $value + * + * @throws InvalidArgumentException + * + * @return void + */ + public static function nullOrNumericString($value, $message = '') + { + null === $value || static::numericString($value, $message); + } + + /** + * @psalm-pure + * @psalm-assert iterable $value + * + * @param mixed $value + * + * @throws InvalidArgumentException + * + * @return void + */ + public static function allNumericString($value, $message = '') + { + static::isIterable($value); + + foreach ($value as $entry) { + static::numericString($entry, $message); + } + } + /** * @psalm-pure * @psalm-assert non-empty-string|null $value diff --git a/tests/static-analysis/assert-numericString.php b/tests/static-analysis/assert-numericString.php new file mode 100644 index 0000000..4d656e2 --- /dev/null +++ b/tests/static-analysis/assert-numericString.php @@ -0,0 +1,47 @@ + + */ +function allNumericString($value): iterable +{ + Assert::allNumericString($value); + + return $value; +}