Skip to content

Commit 710be1c

Browse files
committed
Add Assert::numericString()
1 parent b419d64 commit 710be1c

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Changelog
33

44
## UNRELEASED
55

6+
* Added `Assert::numericString()`
7+
68
## 1.10.0
79

810
### Added

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Method | Description
8686
-------------------------------------------------------- | --------------------------------------------------
8787
`string($value, $message = '')` | Check that a value is a string
8888
`stringNotEmpty($value, $message = '')` | Check that a value is a non-empty string
89+
`numericString($value, $message = '')` | Check that a value is a numeric-string
8990
`integer($value, $message = '')` | Check that a value is an integer
9091
`integerish($value, $message = '')` | Check that a value casts to an integer
9192
`positiveInteger($value, $message = '')` | Check that a value is a positive (non-zero) integer

src/Assert.php

+20
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ public static function string($value, $message = '')
5353
}
5454
}
5555

56+
/**
57+
* @psalm-pure
58+
* @psalm-assert numeric-string $value
59+
*
60+
* @param mixed $value
61+
*
62+
* @throws InvalidArgumentException
63+
*/
64+
public static function numericString($value, string $message = ''): void
65+
{
66+
static::string($value, $message ?? 'Expected a numeric string. Got: %s');
67+
68+
if (!\is_numeric($value)) {
69+
static::reportInvalidArgument(\sprintf(
70+
$message ?? 'Expected a numeric string. Got: %s',
71+
static::typeToString($value)
72+
));
73+
}
74+
}
75+
5676
/**
5777
* @psalm-pure
5878
* @psalm-assert non-empty-string $value

src/Mixin.php

+34
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,40 @@ public static function allString($value, $message = '')
4949
}
5050
}
5151

52+
/**
53+
* @psalm-pure
54+
* @psalm-assert numeric-string|null $value
55+
*
56+
* @param mixed $value
57+
*
58+
* @throws InvalidArgumentException
59+
*
60+
* @return void
61+
*/
62+
public static function nullOrNumericString($value, $message = '')
63+
{
64+
null === $value || static::numericString($value, $message);
65+
}
66+
67+
/**
68+
* @psalm-pure
69+
* @psalm-assert iterable<numeric-string> $value
70+
*
71+
* @param mixed $value
72+
*
73+
* @throws InvalidArgumentException
74+
*
75+
* @return void
76+
*/
77+
public static function allNumericString($value, $message = '')
78+
{
79+
static::isIterable($value);
80+
81+
foreach ($value as $entry) {
82+
static::numericString($entry, $message);
83+
}
84+
}
85+
5286
/**
5387
* @psalm-pure
5488
* @psalm-assert non-empty-string|null $value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Webmozart\Assert\Tests\StaticAnalysis;
4+
5+
use Webmozart\Assert\Assert;
6+
7+
/**
8+
* @psalm-pure
9+
*
10+
* @param mixed $value
11+
*
12+
* @return numeric-string
13+
*/
14+
function numericString($value): string
15+
{
16+
Assert::numericString($value);
17+
18+
return $value;
19+
}
20+
21+
/**
22+
* @psalm-pure
23+
*
24+
* @param mixed $value
25+
*
26+
* @return numeric-string
27+
*/
28+
function nullOrNumericString($value): ?string
29+
{
30+
Assert::nullOrNumericString($value);
31+
32+
return $value;
33+
}
34+
35+
/**
36+
* @psalm-pure
37+
*
38+
* @param mixed $value
39+
*
40+
* @return iterable<numeric-string>
41+
*/
42+
function allNumericString($value): iterable
43+
{
44+
Assert::allNumericString($value);
45+
46+
return $value;
47+
}

0 commit comments

Comments
 (0)