From df7217f0f6c07a8e50de172490b28728b5abf233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Mon, 6 Dec 2021 15:45:00 +0100 Subject: [PATCH] Translator: convert BackedEnum to scalar --- src/Dibi/Translator.php | 4 ++++ tests/dibi/Translator.enums.phpt | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 tests/dibi/Translator.enums.phpt 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));