forked from fre5h/DoctrineEnumBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractEnumType.php
More file actions
268 lines (239 loc) · 6.9 KB
/
AbstractEnumType.php
File metadata and controls
268 lines (239 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/*
* This file is part of the FreshDoctrineEnumBundle.
*
* (c) Artem Henvald <genvaldartem@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fresh\DoctrineEnumBundle\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Types\Type;
use Fresh\DoctrineEnumBundle\Exception\InvalidArgumentException;
/**
* AbstractEnumType.
*
* Provides support of ENUM type for Doctrine in Symfony applications.
*
* @author Artem Henvald <genvaldartem@gmail.com>
* @author Ben Davies <ben.davies@gmail.com>
* @author Jaik Dean <jaik@fluoresce.co>
*
* @template TValue of int|string
* @template TReadable of int|string
*/
abstract class AbstractEnumType extends Type
{
protected string $name = '';
/**
* @var array<TValue, TReadable> Array of ENUM Values, where ENUM values are keys and their readable versions are values
*
* @static
*/
protected static array $choices = [];
/**
* @param TValue $value
* @param AbstractPlatform $platform
*
* @throws InvalidArgumentException
*
* @return TValue|int|string
*/
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed
{
if (null !== $value && !isset(static::$choices[$value])) {
throw new InvalidArgumentException(\sprintf('Invalid value "%s" for ENUM "%s".', $value, $this->getName()));
}
return $value;
}
/**
* @param TValue $value
* @param AbstractPlatform $platform
*
* @return TValue
*/
public function convertToPHPValue($value, AbstractPlatform $platform): mixed
{
if (!isset(static::$choices[$value])) {
return $value;
}
// Check whether choice list is using integers as values
$choice = static::$choices[$value];
$choices = \array_flip(static::$choices);
if (\is_int($choices[$choice])) {
return (int) $value;
}
return $value;
}
/**
* Gets the SQL declaration snippet for a field of this type.
*
* @param array<string, string> $column The column definition
* @param AbstractPlatform $platform The currently used database platform
*
* @return string
*/
public function getSqlDeclaration(array $column, AbstractPlatform $platform): string
{
$values = \implode(
', ',
\array_map(
/** @var TValue $value */
static function (int|string $value) {
return "'{$value}'";
},
static::getValues()
)
);
$sqlDeclaration = match (true) {
$platform instanceof SQLitePlatform => \sprintf('TEXT CHECK(%s IN (%s))', $column['name'], $values),
$platform instanceof PostgreSQLPlatform, $platform instanceof SQLServerPlatform => \sprintf(
'VARCHAR(255) CHECK(%s IN (%s))',
$column['name'],
$values
),
default => \sprintf('ENUM(%s)', $values),
};
$defaultValue = static::getDefaultValue();
if (null !== $defaultValue) {
$sqlDeclaration .= \sprintf(' DEFAULT %s', $platform->quoteStringLiteral((string) $defaultValue));
}
return $sqlDeclaration;
}
/**
* {@inheritdoc}
*/
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return $this->name ?: (string) \array_search(static::class, self::getTypesMap(), true);
}
/**
* Get readable choices for the ENUM field.
*
* @static
*
* @return array<TReadable, TValue>
*/
public static function getChoices(): array
{
return \array_flip(static::$choices);
}
/**
* Get values for the ENUM field.
*
* @static
*
* @return array<int, TValue> Values for the ENUM field
*/
public static function getValues(): array
{
return \array_keys(static::$choices);
}
/**
* Get random value for the ENUM field.
*
* @static
*
* @throws InvalidArgumentException
*
* @return TValue
*/
public static function getRandomValue()
{
$values = self::getValues();
$count = \count($values);
if (0 === $count) {
throw new InvalidArgumentException('There is no value in Enum type');
}
return $values[\random_int(0, $count - 1)];
}
/**
* Get array of ENUM Values, where ENUM values are keys and their readable versions are values.
*
* @static
*
* @return array<TValue, TReadable> Array of values in readable format
*/
public static function getReadableValues(): array
{
return static::$choices;
}
/**
* Asserts that given choice exists in the array of ENUM values.
*
* @param int|string $value ENUM value
*
* @throws InvalidArgumentException
*/
public static function assertValidChoice(int|string $value): void
{
if (!isset(static::$choices[$value])) {
throw new InvalidArgumentException(\sprintf('Invalid value "%s" for ENUM type "%s".', (string) $value, static::class));
}
}
/**
* Get value in readable format.
*
* @param int|string $value ENUM value
*
* @static
*
* @return TReadable Value in readable format
*/
public static function getReadableValue(int|string $value)
{
static::assertValidChoice($value);
return static::$choices[$value];
}
/**
* Check if some value exists in the array of ENUM values.
*
* @param int|string $value ENUM value
*
* @static
*
* @return bool
*/
public static function isValueExist(int|string $value): bool
{
return isset(static::$choices[$value]);
}
/**
* Get default value for DDL statement.
*
* @static
*
* @return TValue|null Default value for DDL statement
*/
public static function getDefaultValue()
{
return null;
}
/**
* Gets an array of database types that map to this Doctrine type.
*
* @param AbstractPlatform $platform
*
* @return string[]
*/
public function getMappedDatabaseTypes(AbstractPlatform $platform): array
{
if ($platform instanceof MySQLPlatform) {
return \array_merge(parent::getMappedDatabaseTypes($platform), ['enum']);
}
return parent::getMappedDatabaseTypes($platform);
}
}