-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractEnumType.php
More file actions
78 lines (62 loc) · 1.73 KB
/
Copy pathAbstractEnumType.php
File metadata and controls
78 lines (62 loc) · 1.73 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
<?php
declare(strict_types=1);
namespace Light\App\DBAL\Types;
use BackedEnum;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Types\Type;
use function array_map;
use function implode;
use function sprintf;
abstract class AbstractEnumType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
if ($platform instanceof PostgreSQLPlatform) {
return $this->getName();
}
if ($platform instanceof SQLitePlatform) {
return 'TEXT';
}
$values = array_map(fn($case) => "'$case->value'", $this->getEnumCases());
return sprintf('ENUM(%s)', implode(', ', $values));
}
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed
{
return $this->getValue($value);
}
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed
{
return $this->getValue($value);
}
/**
* @return class-string
*/
abstract public function getEnumClass(): string;
/**
* @return non-empty-string
*/
abstract public function getName(): string;
/**
* @return BackedEnum[]
*/
public function getEnumCases(): array
{
return $this->getEnumClass()::cases();
}
/**
* @return list<non-empty-string>
*/
public function getEnumValues(): array
{
return $this->getEnumClass()::values();
}
public function getValue(mixed $value): mixed
{
if (! $value instanceof BackedEnum) {
return $value;
}
return $value->value;
}
}