forked from fre5h/DoctrineEnumBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractEnumType.php
154 lines (135 loc) · 3.63 KB
/
AbstractEnumType.php
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
<?php
/*
* This file is part of the FreshDoctrineEnumBundle
*
* (c) Artem Genvald <genvaldartem@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Fresh\DoctrineEnumBundle\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Types\Type;
/**
* AbstractEnumType
*
* Provides support of MySQL ENUM type for Doctrine in Symfony applications
*
* @author Artem Genvald <genvaldartem@gmail.com>
* @author Ben Davies <ben.davies@gmail.com>
*/
abstract class AbstractEnumType extends Type
{
/**
* @var string $name Name of this type
*/
protected $name = '';
/**
* @var array $choices Array of ENUM Values, where ENUM values are keys and their readable versions are values
* @static
*/
protected static $choices = [];
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (null === $value) {
return null;
}
if (!in_array($value, $this->getValues())) {
throw new \InvalidArgumentException(sprintf('Invalid value "%s" for ENUM %s.', $value, $this->getName()));
}
return $value;
}
/**
* {@inheritdoc}
*/
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$values = implode(
', ',
array_map(
function ($value) {
return "'{$value}'";
},
$this->getValues()
)
);
if ($platform instanceof SqlitePlatform) {
return sprintf('TEXT CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
}
if ($platform instanceof PostgreSqlPlatform) {
return sprintf('VARCHAR(255) CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
}
return sprintf('ENUM(%s)', $values);
}
/**
* {@inheritdoc}
*/
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name ?: (new \ReflectionClass(get_class($this)))->getShortName();
}
/**
* Get readable choices for the ENUM field
*
* @static
*
* @return array Values for the ENUM field
*/
public static function getChoices()
{
return static::$choices;
}
/**
* Get values for the ENUM field
*
* @static
*
* @return array Values for the ENUM field
*/
public static function getValues()
{
return array_keys(static::getChoices());
}
/**
* Get value in readable format
*
* @param string $value ENUM value
*
* @static
*
* @return string|null $value Value in readable format
*
* @throws \InvalidArgumentException
*/
public static function getReadableValue($value)
{
if (!isset(static::getChoices()[$value])) {
$message = sprintf('Invalid value "%s" for ENUM type "%s".', $value, get_called_class());
throw new \InvalidArgumentException($message);
}
return static::getChoices()[$value];
}
/**
* Check if some string value exists in the array of ENUM values
*
* @param string $value ENUM value
*
* @return bool
*/
public static function isValueExist($value)
{
return in_array($value, static::getValues());
}
}