Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(type): add unsigned integer types (#395)
- Loading branch information
1 parent
f53a5a5
commit 8dabaa0
Showing
11 changed files
with
491 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,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Internal; | ||
|
||
use Psl\Math; | ||
use Psl\Type; | ||
use Psl\Type\Exception\AssertException; | ||
use Psl\Type\Exception\CoercionException; | ||
|
||
use function is_int; | ||
use function Psl\Type; | ||
|
||
/** | ||
* @ara-extends Type\Type<u16> | ||
* | ||
* @extends Type\Type<int<0, 65535>> | ||
* | ||
* @internal | ||
*/ | ||
final class U16Type extends Type\Type | ||
{ | ||
/** | ||
* @ara-assert-if-true u16 $value | ||
* | ||
* @psalm-assert-if-true int<0, 65535> $value | ||
*/ | ||
public function matches(mixed $value): bool | ||
{ | ||
return is_int($value) && $value >= 0 && $value <= MATH\UINT16_MAX; | ||
} | ||
|
||
/** | ||
* @throws CoercionException | ||
* | ||
* @ara-return u16 | ||
* | ||
* @return int<0, 65535> | ||
*/ | ||
public function coerce(mixed $value): int | ||
{ | ||
$integer = Type\int() | ||
->withTrace($this->getTrace()->withFrame($this->toString())) | ||
->coerce($value); | ||
|
||
if ($integer >= 0 && $integer <= MATH\UINT16_MAX) { | ||
return $integer; | ||
} | ||
|
||
throw CoercionException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
/** | ||
* @ara-assert u16 $value | ||
* | ||
* @psalm-assert int<0, 65535> $value | ||
* | ||
* @throws AssertException | ||
* | ||
* @ara-return u16 | ||
* | ||
* @return int<0, 65535> | ||
*/ | ||
public function assert(mixed $value): int | ||
{ | ||
if (is_int($value) && $value >= 0 && $value <= MATH\UINT16_MAX) { | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'u16'; | ||
} | ||
} |
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,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Internal; | ||
|
||
use Psl\Math; | ||
use Psl\Type; | ||
use Psl\Type\Exception\AssertException; | ||
use Psl\Type\Exception\CoercionException; | ||
|
||
use function is_int; | ||
use function Psl\Type; | ||
|
||
/** | ||
* @ara-extends Type\Type<u32> | ||
* | ||
* @extends Type\Type<int<0, 4294967295>> | ||
* | ||
* @internal | ||
*/ | ||
final class U32Type extends Type\Type | ||
{ | ||
/** | ||
* @ara-assert-if-true u32 $value | ||
* | ||
* @psalm-assert-if-true int<0, 4294967295> $value | ||
*/ | ||
public function matches(mixed $value): bool | ||
{ | ||
return is_int($value) && $value >= 0 && $value <= MATH\UINT32_MAX; | ||
} | ||
|
||
/** | ||
* @throws CoercionException | ||
* | ||
* @ara-return u32 | ||
* | ||
* @return int<0, 4294967295> | ||
*/ | ||
public function coerce(mixed $value): int | ||
{ | ||
$integer = Type\int() | ||
->withTrace($this->getTrace()->withFrame($this->toString())) | ||
->coerce($value); | ||
|
||
if ($integer >= 0 && $integer <= MATH\UINT32_MAX) { | ||
return $integer; | ||
} | ||
|
||
throw CoercionException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
/** | ||
* @ara-assert u32 $value | ||
* | ||
* @psalm-assert int<0, 4294967295> $value | ||
* | ||
* @throws AssertException | ||
* | ||
* @ara-return u32 | ||
* | ||
* @return int<0, 4294967295> | ||
*/ | ||
public function assert(mixed $value): int | ||
{ | ||
if (is_int($value) && $value >= 0 && $value <= MATH\UINT32_MAX) { | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'u32'; | ||
} | ||
} |
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,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Internal; | ||
|
||
use Psl\Math; | ||
use Psl\Type; | ||
use Psl\Type\Exception\AssertException; | ||
use Psl\Type\Exception\CoercionException; | ||
|
||
use function is_int; | ||
use function Psl\Type; | ||
|
||
/** | ||
* @ara-extends Type\Type<u8> | ||
* | ||
* @extends Type\Type<int<0, 255>> | ||
* | ||
* @internal | ||
*/ | ||
final class U8Type extends Type\Type | ||
{ | ||
/** | ||
* @ara-assert-if-true u8 $value | ||
* | ||
* @psalm-assert-if-true int<0, 255> $value | ||
*/ | ||
public function matches(mixed $value): bool | ||
{ | ||
return is_int($value) && $value >= 0 && $value <= MATH\UINT8_MAX; | ||
} | ||
|
||
/** | ||
* @throws CoercionException | ||
* | ||
* @ara-return u8 | ||
* | ||
* @return int<0, 255> | ||
*/ | ||
public function coerce(mixed $value): int | ||
{ | ||
$integer = Type\int() | ||
->withTrace($this->getTrace()->withFrame($this->toString())) | ||
->coerce($value); | ||
|
||
if ($integer >= 0 && $integer <= MATH\UINT8_MAX) { | ||
return $integer; | ||
} | ||
|
||
throw CoercionException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
/** | ||
* @ara-assert u8 $value | ||
* | ||
* @psalm-assert int<0, 255> $value | ||
* | ||
* @throws AssertException | ||
* | ||
* @ara-return u8 | ||
* | ||
* @return int<0, 255> | ||
*/ | ||
public function assert(mixed $value): int | ||
{ | ||
if (is_int($value) && $value >= 0 && $value <= MATH\UINT8_MAX) { | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'u8'; | ||
} | ||
} |
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<u16> | ||
* | ||
* @return TypeInterface<int<0, 65535>> | ||
*/ | ||
function u16(): TypeInterface | ||
{ | ||
return new Internal\U16Type(); | ||
} |
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<u32> | ||
* | ||
* @return TypeInterface<int<0, 4294967295>> | ||
*/ | ||
function u32(): TypeInterface | ||
{ | ||
return new Internal\U32Type(); | ||
} |
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<u8> | ||
* | ||
* @return TypeInterface<int<0, 255>> | ||
*/ | ||
function u8(): TypeInterface | ||
{ | ||
return new Internal\U8Type(); | ||
} |
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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Type; | ||
|
||
use Psl\Math; | ||
use Psl\Type; | ||
|
||
final class U16TypeTest extends TypeTest | ||
{ | ||
public function getType(): Type\TypeInterface | ||
{ | ||
return Type\u16(); | ||
} | ||
|
||
public function getValidCoercions(): iterable | ||
{ | ||
yield [65535, 65535]; | ||
yield [0, 0]; | ||
yield ['0', 0]; | ||
yield ['123', 123]; | ||
yield [$this->stringable('123'), 123]; | ||
yield ['7', 7]; | ||
yield ['07', 7]; | ||
yield ['007', 7]; | ||
yield ['000', 0]; | ||
yield [1.0, 1]; | ||
yield [$this->stringable((string) Math\INT16_MAX), Math\INT16_MAX]; | ||
yield [$this->stringable((string) Math\INT8_MAX), Math\INT8_MAX]; | ||
yield [$this->stringable((string) Math\UINT16_MAX), Math\UINT16_MAX]; | ||
yield [$this->stringable((string) Math\UINT8_MAX), Math\UINT8_MAX]; | ||
} | ||
|
||
public function getInvalidCoercions(): iterable | ||
{ | ||
yield [-123]; | ||
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 ['4294967296']; | ||
yield [$this->stringable('4294967296')]; | ||
yield ['-4294967295']; | ||
yield ['0xFF']; | ||
yield ['']; | ||
yield [$this->stringable((string) Math\INT8_MIN)]; | ||
yield [$this->stringable((string) Math\INT16_MIN)]; | ||
yield [$this->stringable((string) Math\INT32_MIN)]; | ||
yield [$this->stringable((string) Math\INT32_MAX)]; | ||
yield [$this->stringable((string) Math\INT64_MAX)]; | ||
yield [(string) Math\INT64_MAX]; | ||
yield [Math\INT64_MAX]; | ||
} | ||
|
||
public function getToStringExamples(): iterable | ||
{ | ||
yield [$this->getType(), 'u16']; | ||
} | ||
} |
Oops, something went wrong.