Skip to content

Commit

Permalink
Allow null to be stored as enum value
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel.Batanov committed Jul 12, 2018
1 parent 6d8c361 commit 9c57bed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/DBAL/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ private function getStrategy(): NamingStrategyInterface
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if (null === $value) {
return null;
}

/** @var Enum $fqcn */
$fqcn = $this->fqcn;

Expand All @@ -63,6 +67,10 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (null === $value) {
return null;
}

if (!is_a($value, $this->fqcn)) {
throw ConversionException::conversionFailed($value, $this->getName());
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/DBAL/EnumTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class EnumTypeTest extends TestCase
* @param string $value
* @param TestEnum $expectedStatus
*/
public function testConvertToPHPValue(string $value, TestEnum $expectedStatus): void
public function testConvertToPHPValue(?string $value, ?TestEnum $expectedStatus): void
{
$actualValue = self::createType()->convertToPHPValue($value, $this->createPlatformMock());

Expand All @@ -38,7 +38,7 @@ public function testConvertToPHPValue(string $value, TestEnum $expectedStatus):
* @param string $expectedStatus
* @param TestEnum $status
*/
public function testConvertToDatabaseValue(string $expectedStatus, TestEnum $status): void
public function testConvertToDatabaseValue(?string $expectedStatus, ?TestEnum $status): void
{
$actualValue = self::createType()->convertToDatabaseValue($status, $this->createPlatformMock());

Expand All @@ -50,6 +50,7 @@ public function getObjectToValueMap(): array
return [
['ONE', TestEnum::ONE()],
['TWO', TestEnum::TWO()],
[null, null],
];
}

Expand Down

0 comments on commit 9c57bed

Please sign in to comment.