-
-
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.
- Loading branch information
Showing
20 changed files
with
127 additions
and
121 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
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
Oops, something went wrong.