Skip to content

Commit

Permalink
Add exception for invalid value from the database
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jun 26, 2024
1 parent 94a6fc5 commit 827f613
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/en/reference/custom-mapping-types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ a ``DateTimeImmutable`` when the data is read from the database.
public function convertToDatabaseValue($value): array
{
if (! is_array($value) || ! isset($value['utc'], $value['tz'])) {
throw new Exception(); // TODO: throw correct exception class
if (! isset($value['utc'], $value['tz'])) {
throw new RuntimeException('Database value cannot be converted to date with timezone. Expected array with "utc" and "tz" keys.');
}
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\ODM\MongoDB\Types\ClosureToPHP;
use Doctrine\ODM\MongoDB\Types\Type;
use MongoDB\BSON\UTCDateTime;
use RuntimeException;

class DateTimeWithTimezoneType extends Type
{
Expand All @@ -19,6 +20,10 @@ class DateTimeWithTimezoneType extends Type
/** @param array{utc: UTCDateTime, tz: string} $value */
public function convertToPHPValue($value): DateTimeImmutable
{
if (! isset($value['utc'], $value['tz'])) {
throw new RuntimeException('Database value cannot be converted to date with timezone. Expected array with "utc" and "tz" keys.');
}

$timeZone = new DateTimeZone($value['tz']);
$dateTime = $value['utc']
->toDateTime()
Expand Down

0 comments on commit 827f613

Please sign in to comment.