From 04bb5ede3df2403112b88733fd7ce70e74584e81 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 also closes (#412) --- src/Dibi/Translator.php | 5 ++++ tests/dibi/Translator.enums.phpt | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/dibi/Translator.enums.phpt diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index 2188e896..3d2a4831 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -326,6 +326,11 @@ public function formatValue($value, ?string $modifier): string } } + // object-to-scalar procession + if ($value instanceof \BackedEnum && is_scalar($value->value)) { + $value = $value->value; + } + // with modifier procession if ($modifier) { if ($value !== null && !is_scalar($value)) { // array is already processed diff --git a/tests/dibi/Translator.enums.phpt b/tests/dibi/Translator.enums.phpt new file mode 100644 index 00000000..270a8f81 --- /dev/null +++ b/tests/dibi/Translator.enums.phpt @@ -0,0 +1,41 @@ +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));