diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index b86dbcab..e293bb1e 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -457,6 +457,10 @@ public function formatValue(mixed $value, ?string $modifier): string } } + // object-to-scalar procession + if ($value instanceof \BackedEnum && is_scalar($value->value)) { + $value = $value->value; + } // without modifier procession if (is_string($value)) { diff --git a/tests/dibi/Translator.enums.phpt b/tests/dibi/Translator.enums.phpt new file mode 100644 index 00000000..1a67b00f --- /dev/null +++ b/tests/dibi/Translator.enums.phpt @@ -0,0 +1,41 @@ += 8.1 + * @dataProvider ../databases.ini + */ + +declare(strict_types=1); + +use Tester\Assert; + +require __DIR__ . '/bootstrap.php'; + +$conn = new Dibi\Connection($config); +$translator = new Dibi\Translator($conn); + + +enum EnumInt: int +{ + case One = 1; +} + +enum EnumString: string +{ + case One = 'one'; +} + +enum PureEnum +{ + case One; +} + + +Assert::equal('1', $translator->formatValue(EnumInt::One, null)); + +Assert::equal(match ($config['driver']) { + 'sqlsrv' => "N'one'", + default => "'one'", +}, $translator->formatValue(EnumString::One, null)); + +Assert::equal('**Unexpected PureEnum**', $translator->formatValue(PureEnum::One, null));