From 649d06fc3745739204d58bf413ddf913997769e8 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Fri, 22 Oct 2021 15:26:36 +0200 Subject: [PATCH] Add `Assert::numericString()` --- .github/workflows/ci.yaml | 2 - CHANGELOG.md | 2 + README.md | 1 + src/Assert.php | 20 +++++++ src/Mixin.php | 53 +++++++++++++++++++ .../static-analysis/assert-numericString.php | 47 ++++++++++++++++ 6 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 tests/static-analysis/assert-numericString.php diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b7c3a75..e7baed7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -88,8 +88,6 @@ jobs: strategy: matrix: php-version: - - "7.2" - - "7.3" - "7.4" - "8.0" - "8.1" 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..9ae0eca 100644 --- a/src/Mixin.php +++ b/src/Mixin.php @@ -69,6 +69,59 @@ 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 iterable $value + * + * @param mixed $value + * + * @throws InvalidArgumentException + * + * @return void + */ + public static function allNullOrNumericString($value, $message = '') + { + static::isIterable($value); + + foreach ($value as $entry) { + null === $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; +}