1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FreshDoctrineEnumBundle |
4
|
|
|
* |
5
|
|
|
* (c) Artem Genvald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fresh\DoctrineEnumBundle\DBAL\Types; |
12
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
14
|
|
|
use Doctrine\DBAL\Platforms\PostgreSqlPlatform; |
15
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform; |
16
|
|
|
use Doctrine\DBAL\Platforms\SQLServerPlatform; |
17
|
|
|
use Doctrine\DBAL\Types\Type; |
18
|
|
|
use Fresh\DoctrineEnumBundle\Util\LegacyFormHelper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AbstractEnumType |
22
|
|
|
* |
23
|
|
|
* Provides support of MySQL ENUM type for Doctrine in Symfony applications |
24
|
|
|
* |
25
|
|
|
* @author Artem Genvald <[email protected]> |
26
|
|
|
* @author Ben Davies <[email protected]> |
27
|
|
|
* @author Jaik Dean <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
abstract class AbstractEnumType extends Type |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string $name Name of this type |
33
|
|
|
*/ |
34
|
|
|
protected $name = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array $choices Array of ENUM Values, where ENUM values are keys and their readable versions are values |
38
|
|
|
* @static |
39
|
|
|
*/ |
40
|
|
|
protected static $choices = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
46
|
|
|
{ |
47
|
|
|
if (null === $value) { |
48
|
|
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!isset(static::$choices[$value])) { |
52
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid value "%s" for ENUM "%s".', $value, $this->getName())); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
62
|
|
|
{ |
63
|
|
|
$values = implode( |
64
|
|
|
', ', |
65
|
|
|
array_map( |
66
|
|
|
function ($value) { |
67
|
|
|
return "'{$value}'"; |
68
|
|
|
}, |
69
|
|
|
static::getValues() |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
if ($platform instanceof SqlitePlatform) { |
74
|
|
|
return sprintf('TEXT CHECK(%s IN (%s))', $fieldDeclaration['name'], $values); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($platform instanceof PostgreSqlPlatform || $platform instanceof SQLServerPlatform) { |
78
|
|
|
return sprintf('VARCHAR(255) CHECK(%s IN (%s))', $fieldDeclaration['name'], $values); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return sprintf('ENUM(%s)', $values); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function requiresSQLCommentHint(AbstractPlatform $platform) |
88
|
|
|
{ |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function getName() |
96
|
|
|
{ |
97
|
|
|
return $this->name ?: (new \ReflectionClass(get_class($this)))->getShortName(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get readable choices for the ENUM field |
102
|
|
|
* |
103
|
|
|
* @static |
104
|
|
|
* |
105
|
|
|
* @return array Values for the ENUM field |
106
|
|
|
*/ |
107
|
|
|
public static function getChoices() |
108
|
|
|
{ |
109
|
|
|
// Compatibility with Symfony <3.0 |
110
|
|
|
if (LegacyFormHelper::isLegacy()) { |
111
|
|
|
return static::$choices; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return array_flip(static::$choices); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get values for the ENUM field |
119
|
|
|
* |
120
|
|
|
* @static |
121
|
|
|
* |
122
|
|
|
* @return array Values for the ENUM field |
123
|
|
|
*/ |
124
|
|
|
public static function getValues() |
125
|
|
|
{ |
126
|
|
|
return array_keys(static::$choices); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get value in readable format |
131
|
|
|
* |
132
|
|
|
* @param string $value ENUM value |
133
|
|
|
* |
134
|
|
|
* @static |
135
|
|
|
* |
136
|
|
|
* @return string|null $value Value in readable format |
137
|
|
|
* |
138
|
|
|
* @throws \InvalidArgumentException |
139
|
|
|
*/ |
140
|
|
|
public static function getReadableValue($value) |
141
|
|
|
{ |
142
|
|
|
if (!isset(static::$choices[$value])) { |
143
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid value "%s" for ENUM type "%s".', $value, get_called_class())); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return static::$choices[$value]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Check if some string value exists in the array of ENUM values |
151
|
|
|
* |
152
|
|
|
* @param string $value ENUM value |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public static function isValueExist($value) |
157
|
|
|
{ |
158
|
|
|
return isset(static::$choices[$value]); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|