-
-
Notifications
You must be signed in to change notification settings - Fork 9
Union types #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Union types #20
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a63921a
feat: union types
hungthai1401 7e4220b
test: union types
hungthai1401 714ad51
fix: phpstan
hungthai1401 606c583
fix: typo in function name
hungthai1401 8c6f44c
chore: `Union Types` usage
hungthai1401 869c8f5
feat: save instance types for validation
hungthai1401 5b3f87f
refactor: use `toBe()` expectation
hungthai1401 05d6224
fix: plural of instance
hungthai1401 6cbdf0b
fix: phpstan
hungthai1401 2d83328
fix: declare type of instance as `class-string` to solve PHPStan
hungthai1401 e15ae93
refactor: method parameter in the PHPDOC block
hungthai1401 b85e891
fix: lint
hungthai1401 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,17 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strictus\Enums; | ||
|
|
||
| enum Type | ||
| { | ||
| case INT; | ||
| case STRING; | ||
| case FLOAT; | ||
| case BOOLEAN; | ||
| case ARRAY; | ||
| case OBJECT; | ||
| case INSTANCE; | ||
| case ENUM; | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |
|
|
||
| /** | ||
| * @internal | ||
| * | ||
| * @property mixed $value | ||
| */ | ||
| interface StrictusTypeInterface | ||
| { | ||
|
|
||
This file contains hidden or 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 hidden or 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,220 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strictus\Types; | ||
|
|
||
| use Strictus\Concerns\StrictusTyping; | ||
| use Strictus\Enums\Type; | ||
| use Strictus\Exceptions\StrictusTypeException; | ||
| use Strictus\Interfaces\StrictusTypeInterface; | ||
| use UnitEnum; | ||
|
|
||
| /** | ||
| * @internal | ||
| * | ||
| * @property ?StrictusTypeInterface $value | ||
| */ | ||
| final class StrictusUnion implements StrictusTypeInterface | ||
| { | ||
| use StrictusTyping; | ||
|
|
||
| private string $errorMessage = 'Expected Union'; | ||
|
|
||
| private ?Type $type = null; | ||
|
|
||
| /** @var array<string, class-string>|null */ | ||
| private ?array $instances = null; | ||
|
|
||
| /** | ||
| * @param array<int, Type> $types | ||
| */ | ||
| public function __construct(private array $types, mixed $value, private bool $nullable) | ||
| { | ||
| if ($this->nullable) { | ||
| $this->errorMessage .= ' Or Null'; | ||
| } | ||
|
|
||
| $this->validateTypes($types); | ||
|
|
||
| $this->validate($value); | ||
|
|
||
| $this->value = $this->getStrictusType($value); | ||
| } | ||
|
|
||
| public function __invoke(mixed $value = new StrictusUndefined()): mixed | ||
| { | ||
| if ($value instanceof StrictusUndefined) { | ||
| return $this->value?->value; | ||
| } | ||
|
|
||
| $this->immutableValidate(); | ||
|
|
||
| $this->validate($value); | ||
|
|
||
| $this->value = $this->getStrictusType($value); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function __get(string $value): mixed | ||
| { | ||
| return $this->value?->value; | ||
| } | ||
|
|
||
| public function __set(string $name, mixed $value): void | ||
| { | ||
| if ($name !== 'value') { | ||
| return; | ||
| } | ||
|
|
||
| $this->immutableValidate(); | ||
|
|
||
| $this->validate($value); | ||
|
|
||
| $this->value = $this->getStrictusType($value); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, Type> $types $types | ||
| */ | ||
| private function validateTypes(array $types): void | ||
| { | ||
| foreach ($types as $type) { | ||
| if (! $type instanceof Type) { | ||
| throw StrictusTypeException::becauseInvalidSupportedType(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private function validate(mixed $value): void | ||
| { | ||
| if ($value === null && ! $this->nullable) { | ||
| throw new StrictusTypeException($this->errorMessage); | ||
| } | ||
|
|
||
| $this->detectType($value); | ||
| if ($value !== null && ! in_array($this->type, $this->types, true)) { | ||
| throw new StrictusTypeException($this->errorMessage); | ||
| } | ||
| } | ||
|
|
||
| private function detectType(mixed $value): void | ||
| { | ||
| if (is_null($value)) { | ||
| $this->type = null; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (is_int($value)) { | ||
| $this->type = Type::INT; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (is_string($value)) { | ||
| $this->type = Type::STRING; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (is_float($value)) { | ||
| $this->type = Type::FLOAT; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (is_bool($value)) { | ||
| $this->type = Type::BOOLEAN; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (is_array($value)) { | ||
| $this->type = Type::ARRAY; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (false === is_object($value)) { | ||
| throw StrictusTypeException::becauseNotSupportedType(gettype($value)); | ||
| } | ||
|
|
||
| $class = get_class($value); | ||
| if ('stdClass' === $class) { | ||
| $this->type = Type::OBJECT; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if ($value instanceof UnitEnum) { | ||
WendellAdriel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $this->type = Type::ENUM; | ||
| $this->setInstance($class); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (class_exists($class)) { | ||
| $this->type = Type::INSTANCE; | ||
| $this->setInstance($class); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| throw StrictusTypeException::becauseNotSupportedType(gettype($value)); | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string $instance | ||
| */ | ||
| private function setInstance(string $instance): void | ||
| { | ||
| if (null === $this->type) { | ||
| throw StrictusTypeException::becauseNullInstanceType(); | ||
| } | ||
|
|
||
| if (Type::ENUM !== $this->type && Type::INSTANCE !== $this->type) { | ||
| throw StrictusTypeException::becauseUnInstanceableType(); | ||
| } | ||
|
|
||
| if (isset($this->instances[$this->type->name]) && $this->instances[$this->type->name]) { | ||
| return; | ||
| } | ||
|
|
||
| $this->instances = [ | ||
| $this->type->name => $instance, | ||
| ]; | ||
| } | ||
|
|
||
| private function getStrictusType(mixed $value): ?StrictusTypeInterface | ||
| { | ||
| return match ($this->type) { | ||
| null => null, | ||
| Type::INT => new StrictusInteger($value, $this->nullable), | ||
| Type::STRING => new StrictusString($value, $this->nullable), | ||
| Type::FLOAT => new StrictusFloat($value, $this->nullable), | ||
| Type::BOOLEAN => new StrictusBoolean($value, $this->nullable), | ||
| Type::ARRAY => new StrictusArray($value, $this->nullable), | ||
| Type::OBJECT => new StrictusObject($value, $this->nullable), | ||
| default => $this->getInstanceableStrictusType($value), | ||
| }; | ||
| } | ||
|
|
||
| private function getInstanceableStrictusType(mixed $value): StrictusTypeInterface | ||
| { | ||
| if (null === $this->type) { | ||
| throw StrictusTypeException::becauseInvalidSupportedType(); | ||
| } | ||
|
|
||
| if (null === $this->instances || (! isset($this->instances[$this->type->name]))) { | ||
| throw StrictusTypeException::becauseNullInstanceType(); | ||
| } | ||
|
|
||
| return match ($this->type) { | ||
| Type::INSTANCE => new StrictusInstance($this->instances[$this->type->name], $value, $this->nullable), | ||
| Type::ENUM => new StrictusEnum($this->instances[$this->type->name], $value, $this->nullable), | ||
| default => throw StrictusTypeException::becauseUnInstanceableType(), | ||
| }; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.