Skip to content

Commit 9c81ccc

Browse files
committed
add enum label string type check
1 parent 860fdea commit 9c81ccc

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/DBAL/Contract/EnumInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
interface EnumInterface
88
{
99
/**
10-
* @return array<int|string>
10+
* @return string[]
1111
*/
1212
public static function cases(): array;
1313
}

src/Tools/EnumTool.php

+21-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class EnumTool
1919
*/
2020
public static function getEnumTypeNameFromClassName(string $className): string
2121
{
22-
self::checkEnumExists($className);
22+
self::checkEnumExistsAndValid($className);
2323

2424
$classNameParts = \explode('\\', $className);
2525

@@ -33,7 +33,7 @@ public static function getEnumTypeNameFromClassName(string $className): string
3333
*/
3434
public static function getEnumLabelsByClassName(string $className): array
3535
{
36-
self::checkEnumExists($className);
36+
self::checkEnumExistsAndValid($className);
3737

3838
return \array_map(
3939
static function (mixed $case) {
@@ -50,18 +50,35 @@ static function (mixed $case) {
5050
);
5151
}
5252

53-
private static function checkEnumExists(string $className): void
53+
private static function checkEnumExistsAndValid(string $className): void
5454
{
5555
if (self::$checkMap[$className] ?? false) {
5656
return;
5757
}
5858

59-
if (!\enum_exists($className) && (!\class_exists($className) || is_subclass_of($className, EnumInterface::class))) {
59+
if (!\enum_exists($className) && (!\class_exists($className) || !is_subclass_of($className, EnumInterface::class))) {
6060
throw new InvalidArgumentException(
6161
sprintf('Invalid enum className specified: %s. Enum class has to be a php8 enum or implements %s', $className, EnumInterface::class)
6262
);
6363
}
6464

65+
if (enum_exists($className) && self::isIntBackedEnum($className)) {
66+
throw new InvalidArgumentException(
67+
sprintf('Invalid enum className specified: %s. PostgreSQL supports only string values for enums', $className)
68+
);
69+
}
70+
6571
self::$checkMap[$className] = true;
6672
}
73+
74+
/**
75+
* @param class-string<\UnitEnum> $className
76+
* @return bool
77+
*/
78+
private static function isIntBackedEnum(string $className): bool
79+
{
80+
$fistCase = ($className::cases()[0] ?? null);
81+
82+
return $fistCase instanceof \BackedEnum && is_int($fistCase->value);
83+
}
6784
}

0 commit comments

Comments
 (0)