-
-
Notifications
You must be signed in to change notification settings - Fork 72
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 signed integer types (#394)
- Loading branch information
Showing
17 changed files
with
659 additions
and
2 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
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\Math; | ||
use Psl\Type; | ||
use Psl\Type\Exception\AssertException; | ||
use Psl\Type\Exception\CoercionException; | ||
|
||
use function is_int; | ||
|
||
/** | ||
* @ara-extends Type\Type<i16> | ||
* | ||
* @extends Type\Type<int<-32768, 32767>> | ||
* | ||
* @internal | ||
*/ | ||
final class I16Type extends Type\Type | ||
{ | ||
/** | ||
* @ara-assert-if-true i16 $value | ||
* | ||
* @psalm-assert-if-true int<-32768, 32767> $value | ||
*/ | ||
public function matches(mixed $value): bool | ||
{ | ||
return is_int($value) && $value >= Math\INT16_MIN && $value <= MATH\INT16_MAX; | ||
} | ||
|
||
/** | ||
* @throws CoercionException | ||
* | ||
* @ara-return i16 | ||
* | ||
* @return int<-32768, 32767> | ||
*/ | ||
public function coerce(mixed $value): int | ||
{ | ||
$integer = Type\int() | ||
->withTrace($this->getTrace()->withFrame($this->toString())) | ||
->coerce($value); | ||
|
||
if ($integer >= Math\INT16_MIN && $integer <= MATH\INT16_MAX) { | ||
return $integer; | ||
} | ||
|
||
throw CoercionException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
/** | ||
* @ara-assert i16 $value | ||
* | ||
* @psalm-assert int<-32768, 32767> $value | ||
* | ||
* @throws AssertException | ||
* | ||
* @ara-return i16 | ||
* | ||
* @return int<-32768, 32767> | ||
*/ | ||
public function assert(mixed $value): int | ||
{ | ||
if (is_int($value) && $value >= Math\INT16_MIN && $value <= MATH\INT16_MAX) { | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'i16'; | ||
} | ||
} |
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<i32> | ||
* | ||
* @extends Type\Type<int<-2147483648, 2147483647>> | ||
* | ||
* @internal | ||
*/ | ||
final class I32Type extends Type\Type | ||
{ | ||
/** | ||
* @ara-assert-if-true i32 $value | ||
* | ||
* @psalm-assert-if-true int<-2147483648, 2147483647> $value | ||
*/ | ||
public function matches(mixed $value): bool | ||
{ | ||
return is_int($value) && $value >= Math\INT32_MIN && $value <= MATH\INT32_MAX; | ||
} | ||
|
||
/** | ||
* @throws CoercionException | ||
* | ||
* @ara-return i32 | ||
* | ||
* @return int<-2147483648, 2147483647> | ||
*/ | ||
public function coerce(mixed $value): int | ||
{ | ||
$integer = Type\int() | ||
->withTrace($this->getTrace()->withFrame($this->toString())) | ||
->coerce($value); | ||
|
||
if ($integer >= Math\INT32_MIN && $integer <= MATH\INT32_MAX) { | ||
return $integer; | ||
} | ||
|
||
throw CoercionException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
/** | ||
* @ara-assert i32 $value | ||
* | ||
* @psalm-assert int<-2147483648, 2147483647> $value | ||
* | ||
* @throws AssertException | ||
* | ||
* @ara-return i32 | ||
* | ||
* @return int<-2147483648, 2147483647> | ||
*/ | ||
public function assert(mixed $value): int | ||
{ | ||
if (is_int($value) && $value >= Math\INT32_MIN && $value <= MATH\INT32_MAX) { | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'i32'; | ||
} | ||
} |
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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Internal; | ||
|
||
use Psl\Type; | ||
use Psl\Type\Exception\AssertException; | ||
use Psl\Type\Exception\CoercionException; | ||
|
||
use function is_int; | ||
|
||
/** | ||
* @ara-extends Type\Type<i64> | ||
* | ||
* @extends Type\Type<int> | ||
* | ||
* @internal | ||
*/ | ||
final class I64Type extends Type\Type | ||
{ | ||
/** | ||
* @ara-assert-if-true i64 $value | ||
* | ||
* @psalm-assert-if-true int $value | ||
*/ | ||
public function matches(mixed $value): bool | ||
{ | ||
return is_int($value); | ||
} | ||
|
||
/** | ||
* @throws CoercionException | ||
* | ||
* @ara-return i64 | ||
* | ||
* @return int | ||
*/ | ||
public function coerce(mixed $value): int | ||
{ | ||
return Type\int() | ||
->withTrace($this->getTrace()->withFrame($this->toString())) | ||
->coerce($value); | ||
} | ||
|
||
/** | ||
* @ara-assert i64 $value | ||
* | ||
* @psalm-assert int $value | ||
* | ||
* @throws AssertException | ||
* | ||
* @ara-return i64 | ||
* | ||
* @return int | ||
*/ | ||
public function assert(mixed $value): int | ||
{ | ||
if (is_int($value)) { | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'i64'; | ||
} | ||
} |
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\Math; | ||
use Psl\Type; | ||
use Psl\Type\Exception\AssertException; | ||
use Psl\Type\Exception\CoercionException; | ||
|
||
use function is_int; | ||
|
||
/** | ||
* @ara-extends Type\Type<i8> | ||
* | ||
* @extends Type\Type<int<-128, 127>> | ||
* | ||
* @internal | ||
*/ | ||
final class I8Type extends Type\Type | ||
{ | ||
/** | ||
* @ara-assert-if-true i8 $value | ||
* | ||
* @psalm-assert-if-true int<-128, 127> $value | ||
*/ | ||
public function matches(mixed $value): bool | ||
{ | ||
return is_int($value) && $value >= Math\INT8_MIN && $value <= Math\INT8_MAX; | ||
} | ||
|
||
/** | ||
* @throws CoercionException | ||
* | ||
* @ara-return i8 | ||
* | ||
* @return int<-128, 127> | ||
*/ | ||
public function coerce(mixed $value): int | ||
{ | ||
$integer = Type\int() | ||
->withTrace($this->getTrace()->withFrame($this->toString())) | ||
->coerce($value); | ||
|
||
if ($integer >= Math\INT8_MIN && $integer <= Math\INT8_MAX) { | ||
return $integer; | ||
} | ||
|
||
throw CoercionException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
/** | ||
* @ara-assert i8 $value | ||
* | ||
* @psalm-assert int<-128, 127> $value | ||
* | ||
* @throws AssertException | ||
* | ||
* @ara-return i8 | ||
* | ||
* @return int<-128, 127> | ||
*/ | ||
public function assert(mixed $value): int | ||
{ | ||
if (is_int($value) && $value >= Math\INT8_MIN && $value <= Math\INT8_MAX) { | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'i8'; | ||
} | ||
} |
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<i16> | ||
* | ||
* @return TypeInterface<int<-32768, 32767>> | ||
*/ | ||
function i16(): TypeInterface | ||
{ | ||
return new Internal\I16Type(); | ||
} |
Oops, something went wrong.