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