@@ -46,17 +46,18 @@ entities:
46
46
#[Entity]
47
47
class Article
48
48
{
49
- const STATUS_VISIBLE = 'visible';
50
- const STATUS_INVISIBLE = 'invisible';
49
+ public const STATUS_VISIBLE = 'visible';
50
+ public const STATUS_INVISIBLE = 'invisible';
51
51
52
52
#[Column(type: "string")]
53
53
private $status;
54
54
55
- public function setStatus($status)
55
+ public function setStatus(string $status): void
56
56
{
57
- if (!in_array($status, array( self::STATUS_VISIBLE, self::STATUS_INVISIBLE) )) {
57
+ if (!in_array($status, [ self::STATUS_VISIBLE, self::STATUS_INVISIBLE], true )) {
58
58
throw new \InvalidArgumentException("Invalid status");
59
59
}
60
+
60
61
$this->status = $status;
61
62
}
62
63
}
@@ -92,37 +93,33 @@ For example for the previous enum type:
92
93
93
94
class EnumVisibilityType extends Type
94
95
{
95
- const ENUM_VISIBILITY = 'enumvisibility';
96
- const STATUS_VISIBLE = 'visible';
97
- const STATUS_INVISIBLE = 'invisible';
96
+ private const ENUM_VISIBILITY = 'enumvisibility';
97
+ private const STATUS_VISIBLE = 'visible';
98
+ private const STATUS_INVISIBLE = 'invisible';
98
99
99
- public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
100
+ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
100
101
{
101
102
return "ENUM('visible', 'invisible')";
102
103
}
103
104
104
- public function convertToPHPValue($value, AbstractPlatform $platform)
105
+ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed
105
106
{
106
107
return $value;
107
108
}
108
109
109
- public function convertToDatabaseValue($value, AbstractPlatform $platform)
110
+ public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): string
110
111
{
111
- if (!in_array($value, array( self::STATUS_VISIBLE, self::STATUS_INVISIBLE) )) {
112
+ if (!in_array($value, [ self::STATUS_VISIBLE, self::STATUS_INVISIBLE], true )) {
112
113
throw new \InvalidArgumentException("Invalid status");
113
114
}
115
+
114
116
return $value;
115
117
}
116
118
117
- public function getName()
119
+ public function getName(): string
118
120
{
119
121
return self::ENUM_VISIBILITY;
120
122
}
121
-
122
- public function requiresSQLCommentHint(AbstractPlatform $platform)
123
- {
124
- return true;
125
- }
126
123
}
127
124
128
125
You can register this type with ``Type::addType('enumvisibility', 'MyProject\DBAL\EnumVisibilityType'); ``.
@@ -151,37 +148,33 @@ You can generalize this approach easily to create a base class for enums:
151
148
abstract class EnumType extends Type
152
149
{
153
150
protected $name;
154
- protected $values = array() ;
151
+ protected $values = [] ;
155
152
156
- public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
153
+ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
157
154
{
158
- $values = array_map(function ($val) { return "'".$val."'"; } , $this->values);
155
+ $values = array_map(fn ($val) => "'".$val."'", $this->values);
159
156
160
157
return "ENUM(".implode(", ", $values).")";
161
158
}
162
159
163
- public function convertToPHPValue($value, AbstractPlatform $platform)
160
+ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed
164
161
{
165
162
return $value;
166
163
}
167
164
168
- public function convertToDatabaseValue($value, AbstractPlatform $platform)
165
+ public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed
169
166
{
170
- if (!in_array($value, $this->values)) {
167
+ if (!in_array($value, $this->values, true )) {
171
168
throw new \InvalidArgumentException("Invalid '".$this->name."' value.");
172
169
}
170
+
173
171
return $value;
174
172
}
175
173
176
- public function getName()
174
+ public function getName(): string
177
175
{
178
176
return $this->name;
179
177
}
180
-
181
- public function requiresSQLCommentHint(AbstractPlatform $platform)
182
- {
183
- return true;
184
- }
185
178
}
186
179
187
180
With this base class you can define an enum as easily as:
@@ -194,5 +187,5 @@ With this base class you can define an enum as easily as:
194
187
class EnumVisibilityType extends EnumType
195
188
{
196
189
protected $name = 'enumvisibility';
197
- protected $values = array( 'visible', 'invisible') ;
190
+ protected $values = [ 'visible', 'invisible'] ;
198
191
}
0 commit comments