File tree Expand file tree Collapse file tree 2 files changed +7
-2
lines changed
examples/codecs/handling-data-types Expand file tree Collapse file tree 2 files changed +7
-2
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ final class Person
6
6
{
7
7
public function __construct (
8
8
public string $ name ,
9
- public readonly DateTime $ createdAt = new DateTime (),
9
+ public readonly DateTimeImmutable $ createdAt = new DateTimeImmutable (),
10
10
public readonly ObjectId $ id = new ObjectId (),
11
11
) {
12
12
}
Original file line number Diff line number Diff line change @@ -74,7 +74,12 @@ BSON date and accompanying timezone string. Those same embedded documents can th
74
74
.. literalinclude:: /examples/codecs/handling-data-types/DateTimeCodec.php
75
75
:language: php
76
76
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.
78
83
79
84
First, we add a ``createdAt`` field to the ``Person`` class:
80
85
You can’t perform that action at this time.
0 commit comments