|
13 | 13 | namespace Elao\Enum\Bridge\Doctrine\DBAL\Types;
|
14 | 14 |
|
15 | 15 | use Doctrine\DBAL\ParameterType;
|
16 |
| -use Doctrine\DBAL\Platforms\AbstractPlatform; |
17 |
| -use Doctrine\DBAL\Types\Type; |
18 |
| -use Elao\Enum\Exception\InvalidArgumentException; |
19 | 16 |
|
20 |
| -abstract class AbstractEnumType extends Type |
21 |
| -{ |
22 |
| - private bool $isIntBackedEnum; |
23 |
| - |
24 |
| - /** |
25 |
| - * The enum FQCN for which we should make the DBAL conversion. |
26 |
| - * |
27 |
| - * @psalm-return class-string<\BackedEnum> |
28 |
| - */ |
29 |
| - abstract protected function getEnumClass(): string; |
30 |
| - |
31 |
| - /** |
32 |
| - * What should be returned on null value from the database. |
33 |
| - */ |
34 |
| - protected function onNullFromDatabase(): ?\BackedEnum |
35 |
| - { |
36 |
| - return null; |
37 |
| - } |
38 |
| - |
39 |
| - /** |
40 |
| - * What should be returned on null value from PHP. |
41 |
| - */ |
42 |
| - protected function onNullFromPhp(): int|string|null |
43 |
| - { |
44 |
| - return null; |
45 |
| - } |
46 |
| - |
47 |
| - /** |
48 |
| - * {@inheritdoc} |
49 |
| - * |
50 |
| - * @param \BackedEnum|int|string|null $value |
51 |
| - */ |
52 |
| - public function convertToDatabaseValue($value, AbstractPlatform $platform): string|int|null |
53 |
| - { |
54 |
| - if ($value !== null && !is_a($value, $this->getEnumClass())) { |
55 |
| - $throwException = true; |
56 |
| - if ($this->checkIfValueMatchesBackedEnumType($value)) { |
57 |
| - $value = $this->getEnumClass()::tryFrom($this->cast($value)); |
58 |
| - if ($value !== null) { |
59 |
| - $throwException = false; |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - if ($throwException) { |
64 |
| - throw new InvalidArgumentException(sprintf( |
65 |
| - 'Expected an instance of a %s. %s given.', |
66 |
| - $this->getEnumClass(), |
67 |
| - get_debug_type($value), |
68 |
| - )); |
69 |
| - } |
70 |
| - } |
71 |
| - |
72 |
| - if (null === $value) { |
73 |
| - return $this->onNullFromPhp(); |
74 |
| - } |
75 |
| - |
76 |
| - return $value->value; |
77 |
| - } |
78 |
| - |
79 |
| - /** |
80 |
| - * {@inheritdoc} |
81 |
| - * |
82 |
| - * @param int|string|null $value The value to convert. |
83 |
| - */ |
84 |
| - public function convertToPHPValue($value, AbstractPlatform $platform): ?\BackedEnum |
85 |
| - { |
86 |
| - if (null === $value) { |
87 |
| - return $this->onNullFromDatabase(); |
88 |
| - } |
89 |
| - |
90 |
| - return $this->getEnumClass()::from($this->cast($value)); |
91 |
| - } |
92 |
| - |
93 |
| - /** |
94 |
| - * {@inheritdoc} |
95 |
| - */ |
96 |
| - public function getSQLDeclaration(array $column, AbstractPlatform $platform): string |
| 17 | +if (enum_exists(ParameterType::class)) { |
| 18 | + abstract class AbstractEnumType extends AbstractEnumTypeCommon |
97 | 19 | {
|
98 |
| - if ($this->isIntBackedEnum()) { |
99 |
| - return $platform->getIntegerTypeDeclarationSQL($column); |
| 20 | + /** |
| 21 | + * {@inheritdoc} |
| 22 | + */ |
| 23 | + public function getBindingType(): ParameterType |
| 24 | + { |
| 25 | + return $this->isIntBackedEnum() ? ParameterType::INTEGER : ParameterType::STRING; |
100 | 26 | }
|
101 |
| - |
102 |
| - if (empty($column['length'])) { |
103 |
| - $column['length'] = 255; |
104 |
| - } |
105 |
| - |
106 |
| - return method_exists($platform, 'getStringTypeDeclarationSQL') ? |
107 |
| - $platform->getStringTypeDeclarationSQL($column) : |
108 |
| - $platform->getVarcharTypeDeclarationSQL($column); |
109 | 27 | }
|
110 |
| - |
111 |
| - /** |
112 |
| - * {@inheritdoc} |
113 |
| - */ |
114 |
| - public function requiresSQLCommentHint(AbstractPlatform $platform): bool |
| 28 | +} else { |
| 29 | + abstract class AbstractEnumType extends AbstractEnumTypeCommon |
115 | 30 | {
|
116 |
| - return true; |
117 |
| - } |
118 |
| - |
119 |
| - /** |
120 |
| - * {@inheritdoc} |
121 |
| - */ |
122 |
| - public function getBindingType(): int |
123 |
| - { |
124 |
| - return $this->isIntBackedEnum() ? ParameterType::INTEGER : ParameterType::STRING; |
125 |
| - } |
126 |
| - |
127 |
| - /** |
128 |
| - * Cast the value from database to proper enumeration internal type. |
129 |
| - */ |
130 |
| - private function cast(int|string $value): int|string |
131 |
| - { |
132 |
| - return $this->isIntBackedEnum() ? (int) $value : (string) $value; |
133 |
| - } |
134 |
| - |
135 |
| - private function checkIfValueMatchesBackedEnumType(mixed $value): bool |
136 |
| - { |
137 |
| - return ($this->isIntBackedEnum() && \is_int($value)) || (!$this->isIntBackedEnum() && \is_string($value)); |
138 |
| - } |
139 |
| - |
140 |
| - private function isIntBackedEnum(): bool |
141 |
| - { |
142 |
| - if (!isset($this->isIntBackedEnum)) { |
143 |
| - $r = new \ReflectionEnum($this->getEnumClass()); |
144 |
| - |
145 |
| - $this->isIntBackedEnum = 'int' === (string) $r->getBackingType(); |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + public function getBindingType(): int |
| 35 | + { |
| 36 | + return $this->isIntBackedEnum() ? ParameterType::INTEGER : ParameterType::STRING; |
146 | 37 | }
|
147 |
| - |
148 |
| - return $this->isIntBackedEnum; |
149 |
| - } |
150 |
| - |
151 |
| - public function getName(): string |
152 |
| - { |
153 |
| - return $this->getEnumClass(); |
154 | 38 | }
|
155 | 39 | }
|
0 commit comments