Skip to content

Commit e714b1a

Browse files
authored
Merge pull request #12075 from andrew-demb/patch-1
📖 Actualize code block to be compatible with DBAL v4, use modern PHP
2 parents ec0bf05 + dc58aa3 commit e714b1a

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

docs/en/cookbook/mysql-enums.rst

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,18 @@ entities:
4646
#[Entity]
4747
class Article
4848
{
49-
const STATUS_VISIBLE = 'visible';
50-
const STATUS_INVISIBLE = 'invisible';
49+
public const STATUS_VISIBLE = 'visible';
50+
public const STATUS_INVISIBLE = 'invisible';
5151
5252
#[Column(type: "string")]
5353
private $status;
5454
55-
public function setStatus($status)
55+
public function setStatus(string $status): void
5656
{
57-
if (!in_array($status, array(self::STATUS_VISIBLE, self::STATUS_INVISIBLE))) {
57+
if (!in_array($status, [self::STATUS_VISIBLE, self::STATUS_INVISIBLE], true)) {
5858
throw new \InvalidArgumentException("Invalid status");
5959
}
60+
6061
$this->status = $status;
6162
}
6263
}
@@ -92,37 +93,33 @@ For example for the previous enum type:
9293
9394
class EnumVisibilityType extends Type
9495
{
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';
9899
99-
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
100+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
100101
{
101102
return "ENUM('visible', 'invisible')";
102103
}
103104
104-
public function convertToPHPValue($value, AbstractPlatform $platform)
105+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed
105106
{
106107
return $value;
107108
}
108109
109-
public function convertToDatabaseValue($value, AbstractPlatform $platform)
110+
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): string
110111
{
111-
if (!in_array($value, array(self::STATUS_VISIBLE, self::STATUS_INVISIBLE))) {
112+
if (!in_array($value, [self::STATUS_VISIBLE, self::STATUS_INVISIBLE], true)) {
112113
throw new \InvalidArgumentException("Invalid status");
113114
}
115+
114116
return $value;
115117
}
116118
117-
public function getName()
119+
public function getName(): string
118120
{
119121
return self::ENUM_VISIBILITY;
120122
}
121-
122-
public function requiresSQLCommentHint(AbstractPlatform $platform)
123-
{
124-
return true;
125-
}
126123
}
127124
128125
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:
151148
abstract class EnumType extends Type
152149
{
153150
protected $name;
154-
protected $values = array();
151+
protected $values = [];
155152
156-
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
153+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
157154
{
158-
$values = array_map(function($val) { return "'".$val."'"; }, $this->values);
155+
$values = array_map(fn($val) => "'".$val."'", $this->values);
159156
160157
return "ENUM(".implode(", ", $values).")";
161158
}
162159
163-
public function convertToPHPValue($value, AbstractPlatform $platform)
160+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed
164161
{
165162
return $value;
166163
}
167164
168-
public function convertToDatabaseValue($value, AbstractPlatform $platform)
165+
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed
169166
{
170-
if (!in_array($value, $this->values)) {
167+
if (!in_array($value, $this->values, true)) {
171168
throw new \InvalidArgumentException("Invalid '".$this->name."' value.");
172169
}
170+
173171
return $value;
174172
}
175173
176-
public function getName()
174+
public function getName(): string
177175
{
178176
return $this->name;
179177
}
180-
181-
public function requiresSQLCommentHint(AbstractPlatform $platform)
182-
{
183-
return true;
184-
}
185178
}
186179
187180
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:
194187
class EnumVisibilityType extends EnumType
195188
{
196189
protected $name = 'enumvisibility';
197-
protected $values = array('visible', 'invisible');
190+
protected $values = ['visible', 'invisible'];
198191
}

0 commit comments

Comments
 (0)