Skip to content

Commit aba7b30

Browse files
authored
Update "Custom Mapping Types" documentation for a fully working example (#2778)
1 parent a69de4c commit aba7b30

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

docs/en/reference/custom-mapping-types.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ a ``DateTimeImmutable`` when the data is read from the database.
2525
use Doctrine\ODM\MongoDB\Types\ClosureToPHP;
2626
use Doctrine\ODM\MongoDB\Types\Type;
2727
use MongoDB\BSON\UTCDateTime;
28+
use RuntimeException;
2829
2930
class DateTimeWithTimezoneType extends Type
3031
{
@@ -33,6 +34,10 @@ a ``DateTimeImmutable`` when the data is read from the database.
3334
3435
public function convertToPHPValue($value): DateTimeImmutable
3536
{
37+
if (!isset($value['utc'], $value['tz'])) {
38+
throw new RuntimeException('Database value cannot be converted to date with timezone. Expected array with "utc" and "tz" keys.');
39+
}
40+
3641
$timeZone = new DateTimeZone($value['tz']);
3742
$dateTime = $value['utc']
3843
->toDateTime()
@@ -43,8 +48,13 @@ a ``DateTimeImmutable`` when the data is read from the database.
4348
4449
public function convertToDatabaseValue($value): array
4550
{
46-
if (! isset($value['utc'], $value['tz'])) {
47-
throw new RuntimeException('Database value cannot be converted to date with timezone. Expected array with "utc" and "tz" keys.');
51+
if (!$value instanceof DateTimeImmutable) {
52+
throw new \RuntimeException(
53+
sprintf(
54+
'Expected instance of \DateTimeImmutable, got %s',
55+
gettype($value)
56+
)
57+
);
4858
}
4959
5060
return [

0 commit comments

Comments
 (0)