Skip to content

Commit

Permalink
Return null in JsonType (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryuk authored Sep 1, 2022
1 parent 96731c9 commit 45a6e9e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Types/JsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ class JsonType extends Type

public function convertToPHPValue($value, AbstractPlatform $platform)
{
if (null === $value) {
return null;
}

return json_decode((string) $value, true, 512, \JSON_THROW_ON_ERROR);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (null === $value) {
return null;
}

return json_encode($value, \JSON_THROW_ON_ERROR);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Types/JsonTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function testConvertToDatabaseValue(): void
'{"foo":"bar"}',
Type::getType('json')->convertToDatabaseValue(['foo' => 'bar'], $platform)
);

static::assertNull(
Type::getType('json')->convertToDatabaseValue(null, $platform)
);
}

public function testConvertToPHPValue(): void
Expand All @@ -53,6 +57,10 @@ public function testConvertToPHPValue(): void
['foo' => 'bar'],
Type::getType('json')->convertToPHPValue('{"foo":"bar"}', $platform)
);

static::assertNull(
Type::getType('json')->convertToPHPValue(null, $platform)
);
}
}

Expand Down

0 comments on commit 45a6e9e

Please sign in to comment.