Skip to content

Commit 4ff5c45

Browse files
milodg
authored andcommitted
Translator: convert BackedEnum to scalar
1 parent abb949b commit 4ff5c45

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/Dibi/Translator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ public function formatValue(mixed $value, ?string $modifier): string
470470
}
471471
}
472472

473+
// object-to-scalar procession
474+
if ($value instanceof \BackedEnum && is_scalar($value->value)) {
475+
$value = $value->value;
476+
}
477+
473478
// without modifier procession
474479
if (is_string($value)) {
475480
return $this->driver->escapeText($value);

tests/dibi/Translator.enums.phpt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @phpVersion 8.1
5+
* @dataProvider ../databases.ini
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Tester\Assert;
11+
12+
require __DIR__ . '/bootstrap.php';
13+
14+
$conn = new Dibi\Connection($config);
15+
$translator = new Dibi\Translator($conn);
16+
17+
18+
enum EnumInt: int
19+
{
20+
case One = 1;
21+
}
22+
23+
enum EnumString: string
24+
{
25+
case One = 'one';
26+
}
27+
28+
enum PureEnum
29+
{
30+
case One;
31+
}
32+
33+
34+
Assert::equal('1', $translator->formatValue(EnumInt::One, null));
35+
36+
Assert::equal(match ($config['driver']) {
37+
'sqlsrv' => "N'one'",
38+
default => "'one'",
39+
}, $translator->formatValue(EnumString::One, null));
40+
41+
Assert::equal('**Unexpected PureEnum**', $translator->formatValue(PureEnum::One, null));

0 commit comments

Comments
 (0)