forked from CuyZ/Valinor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle non-negative integer type
Non-negative integer can be used as below. It will accept any value equal to or greater than zero. ```php final class SomeClass { /** @var non-negative-int */ public int $nonNegativeInteger; } ```
- Loading branch information
Showing
9 changed files
with
256 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Types; | ||
|
||
use CuyZ\Valinor\Mapper\Tree\Message\ErrorMessage; | ||
use CuyZ\Valinor\Mapper\Tree\Message\MessageBuilder; | ||
use CuyZ\Valinor\Type\IntegerType; | ||
use CuyZ\Valinor\Type\Type; | ||
use CuyZ\Valinor\Utility\IsSingleton; | ||
|
||
/** @internal */ | ||
final class NonNegativeIntegerType implements IntegerType | ||
{ | ||
use IsSingleton; | ||
|
||
public function accepts(mixed $value): bool | ||
{ | ||
return is_int($value) && $value >= 0; | ||
} | ||
|
||
public function matches(Type $other): bool | ||
{ | ||
if ($other instanceof UnionType) { | ||
return $other->isMatchedBy($this); | ||
} | ||
|
||
return $other instanceof self | ||
|| $other instanceof NativeIntegerType | ||
|| $other instanceof MixedType; | ||
} | ||
|
||
public function canCast(mixed $value): bool | ||
{ | ||
return ! is_bool($value) | ||
&& filter_var($value, FILTER_VALIDATE_INT) !== false | ||
&& $value >= 0; | ||
} | ||
|
||
public function cast(mixed $value): int | ||
{ | ||
assert($this->canCast($value)); | ||
|
||
return (int)$value; // @phpstan-ignore-line | ||
} | ||
|
||
public function errorMessage(): ErrorMessage | ||
{ | ||
return MessageBuilder::newError('Value {source_value} is not a valid non-negative integer.')->build(); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'non-negative-int'; | ||
} | ||
} |
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
Oops, something went wrong.