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 23, 2021
1 parent b419d64 commit de0552b
Show file tree
Hide file tree
Showing 5 changed files with 104 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.10.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
34 changes: 34 additions & 0 deletions src/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,40 @@ public static function allString($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 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 de0552b

Please sign in to comment.