-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
441 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
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,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ORM\Mapping; | ||
|
||
use BackedEnum; | ||
use ReflectionProperty; | ||
use ReturnTypeWillChange; | ||
use ValueError; | ||
|
||
use function assert; | ||
use function get_class; | ||
use function is_int; | ||
use function is_string; | ||
|
||
class ReflectionEnumProperty extends ReflectionProperty | ||
{ | ||
/** @var ReflectionProperty */ | ||
private $originalReflectionProperty; | ||
|
||
/** @var class-string<BackedEnum> */ | ||
private $enumType; | ||
|
||
/** | ||
* @param class-string<BackedEnum> $enumType | ||
*/ | ||
public function __construct(ReflectionProperty $originalReflectionProperty, string $enumType) | ||
{ | ||
$this->originalReflectionProperty = $originalReflectionProperty; | ||
$this->enumType = $enumType; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param object|null $object | ||
* | ||
* @return int|string|null | ||
*/ | ||
#[ReturnTypeWillChange] | ||
public function getValue($object = null) | ||
{ | ||
if ($object === null) { | ||
return null; | ||
} | ||
|
||
$enum = $this->originalReflectionProperty->getValue($object); | ||
|
||
if ($enum === null) { | ||
return null; | ||
} | ||
|
||
return $enum->value; | ||
} | ||
|
||
/** | ||
* @param object $object | ||
* @param mixed $value | ||
*/ | ||
public function setValue($object, $value = null): void | ||
{ | ||
if ($value !== null) { | ||
$enumType = $this->enumType; | ||
try { | ||
$value = $enumType::from($value); | ||
} catch (ValueError $e) { | ||
assert(is_string($value) || is_int($value)); | ||
|
||
throw MappingException::invalidEnumValue( | ||
get_class($object), | ||
$this->originalReflectionProperty->getName(), | ||
(string) $value, | ||
$enumType, | ||
$e | ||
); | ||
} | ||
} | ||
|
||
$this->originalReflectionProperty->setValue($object, $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
Oops, something went wrong.