Skip to content

Commit

Permalink
Translator: convert BackedEnum to scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
milo authored and dg committed Dec 12, 2021
1 parent 7c890c2 commit 0bdf956
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Dibi/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ 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)) {
return $this->driver->escapeText($value);
Expand Down
41 changes: 41 additions & 0 deletions tests/dibi/Translator.enums.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @phpVersion 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));

0 comments on commit 0bdf956

Please sign in to comment.