-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce numeric-string type (#406)
- Loading branch information
Showing
5 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Internal; | ||
|
||
use Psl\Type; | ||
use Psl\Type\Exception\AssertException; | ||
use Psl\Type\Exception\CoercionException; | ||
use Stringable; | ||
|
||
use function is_numeric; | ||
use function is_string; | ||
|
||
/** | ||
* @extends Type\Type<numeric-string> | ||
* | ||
* @internal | ||
*/ | ||
final class NumericStringType extends Type\Type | ||
{ | ||
/** | ||
* @psalm-assert-if-true numeric-string $value | ||
*/ | ||
public function matches(mixed $value): bool | ||
{ | ||
return is_string($value) && is_numeric($value); | ||
} | ||
|
||
/** | ||
* @throws CoercionException | ||
* | ||
* @return numeric-string | ||
*/ | ||
public function coerce(mixed $value): string | ||
{ | ||
if (is_string($value) && is_numeric($value)) { | ||
/** @var numeric-string $value */ | ||
return $value; | ||
} | ||
|
||
if (is_numeric($value)) { | ||
return (string) $value; | ||
} | ||
|
||
if ($value instanceof Stringable) { | ||
$str = (string) $value; | ||
if (is_numeric($str)) { | ||
return $str; | ||
} | ||
} | ||
|
||
throw CoercionException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
/** | ||
* @throws AssertException | ||
* | ||
* @return numeric-string | ||
* | ||
* @psalm-assert numeric-string $value | ||
*/ | ||
public function assert(mixed $value): string | ||
{ | ||
if (is_string($value) && is_numeric($value)) { | ||
/** @var numeric-string $value */ | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'numeric-string'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type; | ||
|
||
/** | ||
* @return TypeInterface<numeric-string> | ||
*/ | ||
function numeric_string(): TypeInterface | ||
{ | ||
return new Internal\NumericStringType(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Type; | ||
|
||
use Psl\Type; | ||
|
||
/** | ||
* @extends TypeTest<numeric-string> | ||
*/ | ||
final class NumericStringTypeTest extends TypeTest | ||
{ | ||
/** | ||
* @return Type\Type<numeric-string> | ||
*/ | ||
public function getType(): Type\TypeInterface | ||
{ | ||
return Type\numeric_string(); | ||
} | ||
|
||
public function getValidCoercions(): iterable | ||
{ | ||
yield [123, '123']; | ||
yield [0, '0']; | ||
yield [1.0, '1']; | ||
yield [1.23, '1.23']; | ||
yield ['0', '0']; | ||
yield ['123', '123']; | ||
yield ['1e23', '1e23']; | ||
yield [$this->stringable('123'), '123']; | ||
} | ||
|
||
public function getInvalidCoercions(): iterable | ||
{ | ||
yield ['']; | ||
yield ['hello', 'hello']; | ||
yield [$this->stringable('hello'), 'hello']; | ||
yield [[]]; | ||
yield [[1]]; | ||
yield [Type\bool()]; | ||
yield [null]; | ||
yield [false]; | ||
yield [true]; | ||
yield [STDIN]; | ||
} | ||
|
||
public function getToStringExamples(): iterable | ||
{ | ||
yield [$this->getType(), 'numeric-string']; | ||
} | ||
} |