Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
178 additions
and
1 deletion.
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,94 @@ | ||
<?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_float; | ||
use function is_int; | ||
use function is_string; | ||
use function ltrim; | ||
|
||
/** | ||
* @ara-extends Type\Type<uint> | ||
* | ||
* @extends Type\Type<int<0, max>> | ||
* | ||
* @internal | ||
*/ | ||
final class UIntType extends Type\Type | ||
{ | ||
/** | ||
* @ara-assert-if-true unit $value | ||
* | ||
* @psalm-assert-if-true int<0, max> $value | ||
*/ | ||
public function matches(mixed $value): bool | ||
{ | ||
return is_int($value) && $value >= 0; | ||
} | ||
|
||
/** | ||
* @throws CoercionException | ||
* | ||
* @ara-return uint | ||
* | ||
* @return int<0, max> | ||
*/ | ||
public function coerce(mixed $value): int | ||
{ | ||
if (is_int($value) && $value >= 0) { | ||
return $value; | ||
} elseif (is_float($value)) { | ||
$integer_value = (int) $value; | ||
if (((float) $integer_value) === $value && $integer_value >= 0) { | ||
return $integer_value; | ||
} | ||
} elseif (is_string($value) || $value instanceof Stringable) { | ||
$str = (string)$value; | ||
$int = (int)$str; | ||
if ($str === (string) $int && $int >= 0) { | ||
return $int; | ||
} | ||
|
||
$trimmed = ltrim($str, '0'); | ||
$int = (int) $trimmed; | ||
if ($trimmed === (string) $int && $int >= 0) { | ||
return $int; | ||
} | ||
|
||
// Exceptional case "000" -(trim)-> "", but we want to return 0 | ||
if ('' === $trimmed && '' !== $str) { | ||
return 0; | ||
} | ||
} | ||
|
||
throw CoercionException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
/** | ||
* @ara-assert uint $value | ||
* | ||
* @psalm-assert int<0, max> $value | ||
* | ||
* @throws AssertException | ||
*/ | ||
public function assert(mixed $value): int | ||
{ | ||
if (is_int($value) && $value >= 0) { | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'uint'; | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type; | ||
|
||
/** | ||
* @ara-return TypeInterface<uint> | ||
* | ||
* @return TypeInterface<int<0, max>> | ||
*/ | ||
function uint(): TypeInterface | ||
{ | ||
return new Internal\UIntType(); | ||
} |
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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Type; | ||
|
||
use Psl\Math; | ||
use Psl\Type; | ||
|
||
final class UIntTypeTest extends TypeTest | ||
{ | ||
public function getType(): Type\TypeInterface | ||
{ | ||
return Type\uint(); | ||
} | ||
|
||
public function getValidCoercions(): iterable | ||
{ | ||
yield [123, 123]; | ||
yield [0, 0]; | ||
yield ['0', 0]; | ||
yield ['123', 123]; | ||
yield [$this->stringable('123'), 123]; | ||
yield [$this->stringable((string) Math\INT16_MAX), Math\INT16_MAX]; | ||
yield [$this->stringable((string) Math\INT64_MAX), Math\INT64_MAX]; | ||
yield [(string) Math\INT64_MAX, Math\INT64_MAX]; | ||
yield [Math\INT64_MAX, Math\INT64_MAX]; | ||
yield ['7', 7]; | ||
yield ['07', 7]; | ||
yield ['007', 7]; | ||
yield ['000', 0]; | ||
yield [1.0, 1]; | ||
} | ||
|
||
public function getInvalidCoercions(): iterable | ||
{ | ||
yield [1.23]; | ||
yield ['1.23']; | ||
yield ['1e123']; | ||
yield ['']; | ||
yield [[]]; | ||
yield [[123]]; | ||
yield [null]; | ||
yield [false]; | ||
yield [$this->stringable('1.23')]; | ||
yield [$this->stringable('-007')]; | ||
yield ['-007']; | ||
yield ['9223372036854775808']; | ||
yield [$this->stringable('9223372036854775808')]; | ||
yield ['-9223372036854775809']; | ||
yield [$this->stringable('-9223372036854775809')]; | ||
yield ['0xFF']; | ||
yield ['']; | ||
yield [$this->stringable('-321')]; | ||
yield ['-321']; | ||
yield [-321]; | ||
} | ||
|
||
public function getToStringExamples(): iterable | ||
{ | ||
yield [$this->getType(), 'uint']; | ||
} | ||
} |