mailpoet
/
vendor-prefixed
/
doctrine
/
persistence
/
src
/
Persistence
/
Reflection
/
EnumReflectionProperty.php
mailpoet
/
vendor-prefixed
/
doctrine
/
persistence
/
src
/
Persistence
/
Reflection
Last commit date
EnumReflectionProperty.php
9 months ago
RuntimePublicReflectionProperty.php
9 months ago
RuntimeReflectionProperty.php
9 months ago
TypedNoDefaultReflectionProperty.php
9 months ago
TypedNoDefaultReflectionPropertyBase.php
9 months ago
TypedNoDefaultRuntimePublicReflectionProperty.php
9 months ago
index.php
3 years ago
EnumReflectionProperty.php
93 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\Persistence\Reflection; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use BackedEnum; |
| 6 | use ReflectionClass; |
| 7 | use ReflectionProperty; |
| 8 | use ReflectionType; |
| 9 | use ReturnTypeWillChange; |
| 10 | use function array_map; |
| 11 | use function is_array; |
| 12 | use function reset; |
| 13 | class EnumReflectionProperty extends ReflectionProperty |
| 14 | { |
| 15 | private $originalReflectionProperty; |
| 16 | private $enumType; |
| 17 | public function __construct(ReflectionProperty $originalReflectionProperty, string $enumType) |
| 18 | { |
| 19 | $this->originalReflectionProperty = $originalReflectionProperty; |
| 20 | $this->enumType = $enumType; |
| 21 | } |
| 22 | public function getDeclaringClass() : ReflectionClass |
| 23 | { |
| 24 | return $this->originalReflectionProperty->getDeclaringClass(); |
| 25 | } |
| 26 | public function getName() : string |
| 27 | { |
| 28 | return $this->originalReflectionProperty->getName(); |
| 29 | } |
| 30 | public function getType() : ?ReflectionType |
| 31 | { |
| 32 | return $this->originalReflectionProperty->getType(); |
| 33 | } |
| 34 | public function getAttributes(?string $name = null, int $flags = 0) : array |
| 35 | { |
| 36 | return $this->originalReflectionProperty->getAttributes($name, $flags); |
| 37 | } |
| 38 | #[\ReturnTypeWillChange] |
| 39 | public function getValue($object = null) |
| 40 | { |
| 41 | if ($object === null) { |
| 42 | return null; |
| 43 | } |
| 44 | $enum = $this->originalReflectionProperty->getValue($object); |
| 45 | if ($enum === null) { |
| 46 | return null; |
| 47 | } |
| 48 | return $this->fromEnum($enum); |
| 49 | } |
| 50 | public function setValue($object, $value = null) : void |
| 51 | { |
| 52 | if ($value !== null) { |
| 53 | $value = $this->toEnum($value); |
| 54 | } |
| 55 | $this->originalReflectionProperty->setValue($object, $value); |
| 56 | } |
| 57 | private function fromEnum($enum) |
| 58 | { |
| 59 | if (is_array($enum)) { |
| 60 | return array_map(static function (BackedEnum $enum) { |
| 61 | return $enum->value; |
| 62 | }, $enum); |
| 63 | } |
| 64 | return $enum->value; |
| 65 | } |
| 66 | private function toEnum($value) |
| 67 | { |
| 68 | if ($value instanceof BackedEnum) { |
| 69 | return $value; |
| 70 | } |
| 71 | if (is_array($value)) { |
| 72 | $v = reset($value); |
| 73 | if ($v instanceof BackedEnum) { |
| 74 | return $value; |
| 75 | } |
| 76 | return array_map([$this->enumType, 'from'], $value); |
| 77 | } |
| 78 | return $this->enumType::from($value); |
| 79 | } |
| 80 | public function getModifiers() : int |
| 81 | { |
| 82 | return $this->originalReflectionProperty->getModifiers(); |
| 83 | } |
| 84 | public function getDocComment() : string|false |
| 85 | { |
| 86 | return $this->originalReflectionProperty->getDocComment(); |
| 87 | } |
| 88 | public function isPrivate() : bool |
| 89 | { |
| 90 | return $this->originalReflectionProperty->isPrivate(); |
| 91 | } |
| 92 | } |
| 93 |