diff --git a/docs/en/reference/custom-mapping-types.rst b/docs/en/reference/custom-mapping-types.rst index 42367ae77..6f1ffbd3e 100644 --- a/docs/en/reference/custom-mapping-types.rst +++ b/docs/en/reference/custom-mapping-types.rst @@ -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 [ diff --git a/tests/Documentation/CustomMapping/DateTimeWithTimezoneType.php b/tests/Documentation/CustomMapping/DateTimeWithTimezoneType.php index 429b837d8..39e2395a0 100644 --- a/tests/Documentation/CustomMapping/DateTimeWithTimezoneType.php +++ b/tests/Documentation/CustomMapping/DateTimeWithTimezoneType.php @@ -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 { @@ -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()