Builder
9 months ago
Driver
9 months ago
Exception
9 months ago
Reflection
9 months ago
Annotation.php
9 months ago
AnsiQuoteStrategy.php
9 months ago
AssociationOverride.php
9 months ago
AssociationOverrides.php
9 months ago
AttributeOverride.php
9 months ago
AttributeOverrides.php
9 months ago
Cache.php
9 months ago
ChainTypedFieldMapper.php
9 months ago
ChangeTrackingPolicy.php
9 months ago
ClassMetadata.php
9 months ago
ClassMetadataFactory.php
9 months ago
ClassMetadataInfo.php
9 months ago
Column.php
9 months ago
ColumnResult.php
9 months ago
CustomIdGenerator.php
9 months ago
DefaultEntityListenerResolver.php
9 months ago
DefaultNamingStrategy.php
9 months ago
DefaultQuoteStrategy.php
9 months ago
DefaultTypedFieldMapper.php
9 months ago
DiscriminatorColumn.php
9 months ago
DiscriminatorMap.php
9 months ago
Embeddable.php
9 months ago
Embedded.php
9 months ago
Entity.php
9 months ago
EntityListenerResolver.php
9 months ago
EntityListeners.php
9 months ago
EntityResult.php
9 months ago
FieldResult.php
9 months ago
GeneratedValue.php
9 months ago
HasLifecycleCallbacks.php
9 months ago
Id.php
9 months ago
Index.php
9 months ago
InheritanceType.php
9 months ago
InverseJoinColumn.php
9 months ago
JoinColumn.php
9 months ago
JoinColumnProperties.php
9 months ago
JoinColumns.php
9 months ago
JoinTable.php
9 months ago
ManyToMany.php
9 months ago
ManyToOne.php
9 months ago
MappedSuperclass.php
9 months ago
MappingAttribute.php
9 months ago
MappingException.php
9 months ago
NamedNativeQueries.php
9 months ago
NamedNativeQuery.php
9 months ago
NamedQueries.php
9 months ago
NamedQuery.php
9 months ago
NamingStrategy.php
9 months ago
OneToMany.php
9 months ago
OneToOne.php
9 months ago
OrderBy.php
9 months ago
PostLoad.php
9 months ago
PostPersist.php
9 months ago
PostRemove.php
9 months ago
PostUpdate.php
9 months ago
PreFlush.php
9 months ago
PrePersist.php
9 months ago
PreRemove.php
9 months ago
PreUpdate.php
9 months ago
QuoteStrategy.php
9 months ago
ReflectionEmbeddedProperty.php
9 months ago
ReflectionEnumProperty.php
9 months ago
ReflectionReadonlyProperty.php
9 months ago
SequenceGenerator.php
9 months ago
SqlResultSetMapping.php
9 months ago
SqlResultSetMappings.php
9 months ago
Table.php
9 months ago
TypedFieldMapper.php
9 months ago
UnderscoreNamingStrategy.php
9 months ago
UniqueConstraint.php
9 months ago
Version.php
9 months ago
ReflectionEnumProperty.php
65 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Mapping; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use BackedEnum; |
| 6 | use ReflectionProperty; |
| 7 | use ReturnTypeWillChange; |
| 8 | use ValueError; |
| 9 | use function array_map; |
| 10 | use function get_class; |
| 11 | use function is_array; |
| 12 | class ReflectionEnumProperty extends ReflectionProperty |
| 13 | { |
| 14 | private $originalReflectionProperty; |
| 15 | private $enumType; |
| 16 | public function __construct(ReflectionProperty $originalReflectionProperty, string $enumType) |
| 17 | { |
| 18 | $this->originalReflectionProperty = $originalReflectionProperty; |
| 19 | $this->enumType = $enumType; |
| 20 | parent::__construct($originalReflectionProperty->class, $originalReflectionProperty->name); |
| 21 | } |
| 22 | #[\ReturnTypeWillChange] |
| 23 | public function getValue($object = null) |
| 24 | { |
| 25 | if ($object === null) { |
| 26 | return null; |
| 27 | } |
| 28 | $enum = $this->originalReflectionProperty->getValue($object); |
| 29 | if ($enum === null) { |
| 30 | return null; |
| 31 | } |
| 32 | if (is_array($enum)) { |
| 33 | return array_map(static function (BackedEnum $item) : mixed { |
| 34 | return $item->value; |
| 35 | }, $enum); |
| 36 | } |
| 37 | return $enum->value; |
| 38 | } |
| 39 | public function setValue($object, $value = null) : void |
| 40 | { |
| 41 | if ($value !== null) { |
| 42 | if (is_array($value)) { |
| 43 | $value = array_map(function ($item) use($object) : BackedEnum { |
| 44 | return $this->initializeEnumValue($object, $item); |
| 45 | }, $value); |
| 46 | } else { |
| 47 | $value = $this->initializeEnumValue($object, $value); |
| 48 | } |
| 49 | } |
| 50 | $this->originalReflectionProperty->setValue($object, $value); |
| 51 | } |
| 52 | private function initializeEnumValue($object, $value) : BackedEnum |
| 53 | { |
| 54 | if ($value instanceof BackedEnum) { |
| 55 | return $value; |
| 56 | } |
| 57 | $enumType = $this->enumType; |
| 58 | try { |
| 59 | return $enumType::from($value); |
| 60 | } catch (ValueError $e) { |
| 61 | throw MappingException::invalidEnumValue(get_class($object), $this->originalReflectionProperty->name, (string) $value, $enumType, $e); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 |