diff options
Diffstat (limited to 'vendor/doctrine/persistence/src/Persistence/Reflection/EnumReflectionProperty.php')
| -rw-r--r-- | vendor/doctrine/persistence/src/Persistence/Reflection/EnumReflectionProperty.php | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/vendor/doctrine/persistence/src/Persistence/Reflection/EnumReflectionProperty.php b/vendor/doctrine/persistence/src/Persistence/Reflection/EnumReflectionProperty.php new file mode 100644 index 0000000..114b9db --- /dev/null +++ b/vendor/doctrine/persistence/src/Persistence/Reflection/EnumReflectionProperty.php | |||
| @@ -0,0 +1,181 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\Persistence\Reflection; | ||
| 6 | |||
| 7 | use BackedEnum; | ||
| 8 | use ReflectionClass; | ||
| 9 | use ReflectionProperty; | ||
| 10 | use ReflectionType; | ||
| 11 | use ReturnTypeWillChange; | ||
| 12 | |||
| 13 | use function array_map; | ||
| 14 | use function is_array; | ||
| 15 | use function reset; | ||
| 16 | |||
| 17 | /** | ||
| 18 | * PHP Enum Reflection Property - special override for backed enums. | ||
| 19 | */ | ||
| 20 | class EnumReflectionProperty extends ReflectionProperty | ||
| 21 | { | ||
| 22 | /** @var ReflectionProperty */ | ||
| 23 | private $originalReflectionProperty; | ||
| 24 | |||
| 25 | /** @var class-string<BackedEnum> */ | ||
| 26 | private $enumType; | ||
| 27 | |||
| 28 | /** @param class-string<BackedEnum> $enumType */ | ||
| 29 | public function __construct(ReflectionProperty $originalReflectionProperty, string $enumType) | ||
| 30 | { | ||
| 31 | $this->originalReflectionProperty = $originalReflectionProperty; | ||
| 32 | $this->enumType = $enumType; | ||
| 33 | } | ||
| 34 | |||
| 35 | /** | ||
| 36 | * {@inheritDoc} | ||
| 37 | * | ||
| 38 | * @psalm-external-mutation-free | ||
| 39 | */ | ||
| 40 | public function getDeclaringClass(): ReflectionClass | ||
| 41 | { | ||
| 42 | return $this->originalReflectionProperty->getDeclaringClass(); | ||
| 43 | } | ||
| 44 | |||
| 45 | /** | ||
| 46 | * {@inheritDoc} | ||
| 47 | * | ||
| 48 | * @psalm-external-mutation-free | ||
| 49 | */ | ||
| 50 | public function getName(): string | ||
| 51 | { | ||
| 52 | return $this->originalReflectionProperty->getName(); | ||
| 53 | } | ||
| 54 | |||
| 55 | /** | ||
| 56 | * {@inheritDoc} | ||
| 57 | * | ||
| 58 | * @psalm-external-mutation-free | ||
| 59 | */ | ||
| 60 | public function getType(): ?ReflectionType | ||
| 61 | { | ||
| 62 | return $this->originalReflectionProperty->getType(); | ||
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 | * {@inheritDoc} | ||
| 67 | */ | ||
| 68 | public function getAttributes(?string $name = null, int $flags = 0): array | ||
| 69 | { | ||
| 70 | return $this->originalReflectionProperty->getAttributes($name, $flags); | ||
| 71 | } | ||
| 72 | |||
| 73 | /** | ||
| 74 | * {@inheritDoc} | ||
| 75 | * | ||
| 76 | * Converts enum instance to its value. | ||
| 77 | * | ||
| 78 | * @param object|null $object | ||
| 79 | * | ||
| 80 | * @return int|string|int[]|string[]|null | ||
| 81 | */ | ||
| 82 | #[ReturnTypeWillChange] | ||
| 83 | public function getValue($object = null) | ||
| 84 | { | ||
| 85 | if ($object === null) { | ||
| 86 | return null; | ||
| 87 | } | ||
| 88 | |||
| 89 | $enum = $this->originalReflectionProperty->getValue($object); | ||
| 90 | |||
| 91 | if ($enum === null) { | ||
| 92 | return null; | ||
| 93 | } | ||
| 94 | |||
| 95 | return $this->fromEnum($enum); | ||
| 96 | } | ||
| 97 | |||
| 98 | /** | ||
| 99 | * Converts enum value to enum instance. | ||
| 100 | * | ||
| 101 | * @param object $object | ||
| 102 | * @param mixed $value | ||
| 103 | */ | ||
| 104 | public function setValue($object, $value = null): void | ||
| 105 | { | ||
| 106 | if ($value !== null) { | ||
| 107 | $value = $this->toEnum($value); | ||
| 108 | } | ||
| 109 | |||
| 110 | $this->originalReflectionProperty->setValue($object, $value); | ||
| 111 | } | ||
| 112 | |||
| 113 | /** | ||
| 114 | * @param BackedEnum|BackedEnum[] $enum | ||
| 115 | * | ||
| 116 | * @return ($enum is BackedEnum ? (string|int) : (string[]|int[])) | ||
| 117 | */ | ||
| 118 | private function fromEnum($enum) | ||
| 119 | { | ||
| 120 | if (is_array($enum)) { | ||
| 121 | return array_map(static function (BackedEnum $enum) { | ||
| 122 | return $enum->value; | ||
| 123 | }, $enum); | ||
| 124 | } | ||
| 125 | |||
| 126 | return $enum->value; | ||
| 127 | } | ||
| 128 | |||
| 129 | /** | ||
| 130 | * @param int|string|int[]|string[]|BackedEnum|BackedEnum[] $value | ||
| 131 | * | ||
| 132 | * @return ($value is int|string|BackedEnum ? BackedEnum : BackedEnum[]) | ||
| 133 | */ | ||
| 134 | private function toEnum($value) | ||
| 135 | { | ||
| 136 | if ($value instanceof BackedEnum) { | ||
| 137 | return $value; | ||
| 138 | } | ||
| 139 | |||
| 140 | if (is_array($value)) { | ||
| 141 | $v = reset($value); | ||
| 142 | if ($v instanceof BackedEnum) { | ||
| 143 | return $value; | ||
| 144 | } | ||
| 145 | |||
| 146 | return array_map([$this->enumType, 'from'], $value); | ||
| 147 | } | ||
| 148 | |||
| 149 | return $this->enumType::from($value); | ||
| 150 | } | ||
| 151 | |||
| 152 | /** | ||
| 153 | * {@inheritDoc} | ||
| 154 | * | ||
| 155 | * @psalm-external-mutation-free | ||
| 156 | */ | ||
| 157 | public function getModifiers(): int | ||
| 158 | { | ||
| 159 | return $this->originalReflectionProperty->getModifiers(); | ||
| 160 | } | ||
| 161 | |||
| 162 | /** | ||
| 163 | * {@inheritDoc} | ||
| 164 | * | ||
| 165 | * @psalm-external-mutation-free | ||
| 166 | */ | ||
| 167 | public function getDocComment(): string|false | ||
| 168 | { | ||
| 169 | return $this->originalReflectionProperty->getDocComment(); | ||
| 170 | } | ||
| 171 | |||
| 172 | /** | ||
| 173 | * {@inheritDoc} | ||
| 174 | * | ||
| 175 | * @psalm-external-mutation-free | ||
| 176 | */ | ||
| 177 | public function isPrivate(): bool | ||
| 178 | { | ||
| 179 | return $this->originalReflectionProperty->isPrivate(); | ||
| 180 | } | ||
| 181 | } | ||
