Skip to content

Commit 32ffc7b

Browse files
authored
Fix wrong data type in codec tutorial (#1194)
* Fix wrong data type in codec tutorial * Add note about data types in DateTimeCodec
1 parent 9d9c917 commit 32ffc7b

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

docs/examples/codecs/handling-data-types/Person.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class Person
66
{
77
public function __construct(
88
public string $name,
9-
public readonly DateTime $createdAt = new DateTime(),
9+
public readonly DateTimeImmutable $createdAt = new DateTimeImmutable(),
1010
public readonly ObjectId $id = new ObjectId(),
1111
) {
1212
}

docs/tutorial/codecs.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ BSON date and accompanying timezone string. Those same embedded documents can th
7474
.. literalinclude:: /examples/codecs/handling-data-types/DateTimeCodec.php
7575
:language: php
7676

77-
This codec can now be leveraged by other codecs handle date fields.
77+
.. note::
78+
When writing a codec, you should be as lenient as possible when it comes to handling data. In this case, the codec
79+
handles any ``DateTimeInterface`` when encoding to BSON, as a ``UTCDateTime`` instance can be created from any such
80+
object. When decoding data from BSON, it will always decode to a ``DateTimeImmutable`` instance.
81+
82+
This codec can now be leveraged by other codecs that handle date fields.
7883

7984
First, we add a ``createdAt`` field to the ``Person`` class:
8085

0 commit comments

Comments
 (0)