forked from fre5h/DoctrineEnumBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractEnumType.php
160 lines (141 loc) · 3.91 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
155
156
157
158
159
160
<?php
/*
* This file is part of the FreshDoctrineEnumBundle
*
* (c) Artem Genvald <[email protected]>
*
* 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\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Types\Type;
use Fresh\DoctrineEnumBundle\Util\LegacyFormHelper;
/**
* AbstractEnumType.
*
* Provides support of MySQL ENUM type for Doctrine in Symfony applications.
*
* @author Artem Genvald <[email protected]>
* @author Ben Davies <[email protected]>
* @author Jaik Dean <[email protected]>
*/
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 (!isset(static::$choices[$value])) {
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}'";
},
static::getValues()
)
);
if ($platform instanceof SqlitePlatform) {
return sprintf('TEXT CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
}
if ($platform instanceof PostgreSqlPlatform || $platform instanceof SQLServerPlatform) {
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()
{
// Compatibility with Symfony <3.0
if (LegacyFormHelper::isLegacy()) {
return static::$choices;
}
return array_flip(static::$choices);
}
/**
* Get values for the ENUM field.
*
* @static
*
* @return array Values for the ENUM field
*/
public static function getValues()
{
return array_keys(static::$choices);
}
/**
* 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::$choices[$value])) {
throw new \InvalidArgumentException(sprintf('Invalid value "%s" for ENUM type "%s".', $value, get_called_class()));
}
return static::$choices[$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 isset(static::$choices[$value]);
}
}