Skip to content

Commit

Permalink
Add Assert::numericString()
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Oct 16, 2024
1 parent 4c45030 commit 9bb7da3
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changelog

## UNRELEASED

* Added `Assert::numericString()`

## 1.11.0

### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions src/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<numeric-string> $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<numeric-string|null> $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
Expand Down
47 changes: 47 additions & 0 deletions tests/static-analysis/assert-numericString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-pure
*
* @param mixed $value
*
* @return numeric-string
*/
function numericString($value): string
{
Assert::numericString($value);

return $value;
}

/**
* @psalm-pure
*
* @param mixed $value
*
* @return numeric-string|null
*/
function nullOrNumericString($value): ?string
{
Assert::nullOrNumericString($value);

return $value;
}

/**
* @psalm-pure
*
* @param mixed $value
*
* @return iterable<numeric-string>
*/
function allNumericString($value): iterable
{
Assert::allNumericString($value);

return $value;
}

0 comments on commit 9bb7da3

Please sign in to comment.