Skip to content

Commit d19a89c

Browse files
committed
Update "Custom Mapping Types" documentation for a fully working example
1 parent 6f4f0fb commit d19a89c

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ a ``DateTimeImmutable`` when the data is read from the database.
2020
2121
namespace My\Project\Types;
2222
23-
use DateTimeImmutable;
24-
use DateTimeZone;
2523
use Doctrine\ODM\MongoDB\Types\ClosureToPHP;
2624
use Doctrine\ODM\MongoDB\Types\Type;
2725
use MongoDB\BSON\UTCDateTime;
@@ -31,20 +29,25 @@ a ``DateTimeImmutable`` when the data is read from the database.
3129
// This trait provides default closureToPHP used during data hydration
3230
use ClosureToPHP;
3331
34-
public function convertToPHPValue($value): DateTimeImmutable
32+
public function convertToPHPValue($value): \DateTimeImmutable
3533
{
36-
$timeZone = new DateTimeZone($value['tz']);
34+
$timeZone = new \DateTimeZone($value['tz']);
3735
$dateTime = $value['utc']
3836
->toDateTime()
3937
->setTimeZone($timeZone);
4038
41-
return DateTimeImmutable::createFromMutable($dateTime);
39+
return \DateTimeImmutable::createFromMutable($dateTime);
4240
}
4341
4442
public function convertToDatabaseValue($value): array
4543
{
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.');
44+
if (!$value instanceof \DateTimeImmutable) {
45+
throw new \RuntimeException(
46+
sprintf(
47+
'Expected instance of \DateTimeImmutable, got %s',
48+
gettype($value)
49+
)
50+
);
4851
}
4952
5053
return [

0 commit comments

Comments
 (0)