-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PHP 8.1: new enum type #4930
Closed
Closed
PHP 8.1: new enum type #4930
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use UnitEnum; | ||
|
||
use function restore_error_handler; | ||
use function serialize; | ||
use function set_error_handler; | ||
use function unserialize; | ||
|
||
final class EnumType extends Type | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSQLDeclaration(array $column, AbstractPlatform $platform) | ||
{ | ||
return $platform->getVarcharTypeDeclarationSQL($column); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertToDatabaseValue($value, AbstractPlatform $platform) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if ($value instanceof UnitEnum) { | ||
return serialize($value); | ||
} | ||
|
||
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'UnitEnum']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertToPHPValue($value, AbstractPlatform $platform) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
set_error_handler(function (int $code, string $message): bool { | ||
throw ConversionException::conversionFailedUnserialization($this->getName(), $message); | ||
}); | ||
|
||
try { | ||
return unserialize($value); | ||
} finally { | ||
restore_error_handler(); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return Types::ENUM; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function requiresSQLCommentHint(AbstractPlatform $platform) | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Types; | ||
|
||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Tests\Tools\TestAsset\SimpleEnum; | ||
use Doctrine\DBAL\Types\Types; | ||
|
||
/** | ||
* @requires PHP >= 8.1 | ||
*/ | ||
class EnumTest extends FunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$table = new Table('enum_table'); | ||
$table->addColumn('enum', 'enum'); | ||
|
||
$this->connection->createSchemaManager()->dropAndCreateTable($table); | ||
} | ||
|
||
public function testInsertAndSelect(): void | ||
{ | ||
$draft = SimpleEnum::DRAFT; | ||
|
||
$result = $this->connection->insert('enum_table', ['enum' => $draft], [Types::ENUM]); | ||
self::assertSame(1, $result); | ||
|
||
$value = $this->connection->fetchOne('SELECT enum FROM enum_table'); | ||
|
||
self::assertSame($draft, $this->connection->convertToPHPValue($value, Types::ENUM)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Tests\Tools\TestAsset; | ||
|
||
enum SimpleEnum | ||
{ | ||
case DRAFT; | ||
case PUBLISHED; | ||
case UNPUBLISHED; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Tests\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Tests\Tools\TestAsset\SimpleEnum; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Doctrine\DBAL\Types\EnumType; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function is_a; | ||
use function serialize; | ||
|
||
/** | ||
* @requires PHP >= 8.1 | ||
*/ | ||
class EnumTest extends TestCase | ||
{ | ||
/** @var AbstractPlatform&MockObject */ | ||
private $platform; | ||
|
||
/** @var EnumType */ | ||
private $type; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->platform = $this->createMock(AbstractPlatform::class); | ||
$this->type = new EnumType(); | ||
} | ||
|
||
public function testObjectConvertsToDatabaseValue(): void | ||
{ | ||
self::assertIsString($this->type->convertToDatabaseValue(SimpleEnum::DRAFT, $this->platform)); | ||
} | ||
|
||
public function testObjectConvertsToPHPValue(): void | ||
{ | ||
$value = $this->type->convertToPHPValue(serialize(SimpleEnum::DRAFT), $this->platform); | ||
self::assertTrue(is_a($value, SimpleEnum::class)); | ||
} | ||
|
||
public function testConversionToDatabaseValueFailure(): void | ||
{ | ||
$this->expectException(ConversionException::class); | ||
$this->expectExceptionMessage( | ||
"Could not convert PHP value 'abcdefg' to type enum. Expected one of the following types: null, UnitEnum" | ||
); | ||
$this->type->convertToDatabaseValue('abcdefg', $this->platform); | ||
} | ||
|
||
public function testConversionToPHPValueFailure(): void | ||
{ | ||
$this->expectException(ConversionException::class); | ||
$this->expectExceptionMessage( | ||
"Could not convert database value to 'enum' as an error was triggered by the unserialization:" | ||
. " 'unserialize(): Error at offset 0 of 7 bytes'" | ||
); | ||
$this->type->convertToPHPValue('abcdefg', $this->platform); | ||
} | ||
|
||
public function testNullConversion(): void | ||
{ | ||
self::assertNull($this->type->convertToPHPValue(null, $this->platform)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm very much against writing the output of PHP's
serialize
function to the database. This makes this value pretty much unusable for human beings and non-PHP applications and writing queries against that field is not much fun.PHP allows us to specify backed values for enums and that is what I would expect to find in the database. For instance, if we look at the example from the RFC:
In that case,
H
is a value I'd expect to see the in the database, and notE:11:"Suit:Hearts";
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understandable. I will try to work on something better! You're absolutely right about keeping the benefit of the value only.