-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement PHP 8.1 Enum support [closes #585]
- Loading branch information
Showing
15 changed files
with
93 additions
and
78 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,50 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nextras\Orm\Entity\Wrapper; | ||
|
||
|
||
use BackedEnum; | ||
use Nextras\Orm\Entity\ImmutableValuePropertyWrapper; | ||
use Nextras\Orm\Exception\NullValueException; | ||
use function array_key_first; | ||
use function assert; | ||
use function is_int; | ||
use function is_string; | ||
use function is_subclass_of; | ||
|
||
|
||
final class BackedEnumWrapper extends ImmutableValuePropertyWrapper | ||
{ | ||
public function setInjectedValue($value): bool | ||
{ | ||
if ($value === null && !$this->propertyMetadata->isNullable) { | ||
throw new NullValueException($this->propertyMetadata); | ||
} | ||
|
||
return parent::setInjectedValue($value); | ||
} | ||
|
||
|
||
public function convertToRawValue(mixed $value): mixed | ||
{ | ||
if ($value === null) return null; | ||
$type = array_key_first($this->propertyMetadata->types); | ||
assert($value instanceof $type); | ||
assert($value instanceof BackedEnum); | ||
return $value->value; | ||
} | ||
|
||
|
||
public function convertFromRawValue(mixed $value): ?BackedEnum | ||
{ | ||
if ($value === null) { | ||
if ($this->propertyMetadata->isNullable) return null; | ||
throw new NullValueException($this->propertyMetadata); | ||
} | ||
|
||
assert(is_int($value) || is_string($value)); | ||
$type = array_key_first($this->propertyMetadata->types); | ||
assert(is_subclass_of($type, BackedEnum::class)); | ||
return $type::from($value); | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.
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