Skip to content

Commit

Permalink
Translator: convert BackedEnum to scalar
Browse files Browse the repository at this point in the history
also closes (#412)
  • Loading branch information
milo authored and dg committed Jan 18, 2022
1 parent 7d82ce2 commit 04bb5ed
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 @@ -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
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 04bb5ed

Please sign in to comment.