@@ -94,7 +94,10 @@ To then use this codec with a collection, specify the ``codec`` option when sele
94
94
95
95
<?php
96
96
97
- $collection = (new MongoDB\Client())->selectCollection('test', 'person', ['codec' => new PersonCodec()]);
97
+ $client = new MongoDB\Client();
98
+ $collection = $client->selectCollection('test', 'person', [
99
+ 'codec' => new PersonCodec(),
100
+ ]);
98
101
99
102
$person = new Person('Jane Doe');
100
103
$collection->insertOne($person);
@@ -114,7 +117,7 @@ encoded will result in an exception. The ``aggregate``, ``find``, ``findOne``, `
114
117
``findOneAndReplace``, and ``findOneAndUpdate`` operations will attempt to decode returned documents using the provided
115
118
codec. If the codec does not support the data returned, an exception will be thrown. You can disable codec usage for a
116
119
specific operation or use a different codec (e.g. to decode the result of an aggregation pipeline) by specifying the
117
- ``codec`` option for the operation. This will override the collection-level codec.
120
+ nullable ``codec`` option for the operation. This will override the collection-level codec.
118
121
119
122
Generic Codecs
120
123
--------------
@@ -193,12 +196,9 @@ code omitted for brevity):
193
196
194
197
final class PersonCodec implements MongoDB\Codec\DocumentCodec
195
198
{
196
- private DateTimeCodec $dateTimeCodec;
197
-
198
- public function __construct()
199
- {
200
- $this->dateTimeCodec = new DateTimeCodec();
201
- }
199
+ public function __construct(
200
+ private readonly DateTimeCodec $dateTimeCodec = new DateTimeCodec(),
201
+ ) {}
202
202
203
203
// Other code omitted for brevity
204
204
public function decode($value): Person
@@ -299,12 +299,9 @@ the example below excludes some code we've already shown in previous examples.
299
299
300
300
final class PersonCodec implements MongoDB\Codec\DocumentCodec
301
301
{
302
- private AddressCodec $addressCodec;
303
-
304
- public function __construct()
305
- {
306
- $this->addressCodec = new AddressCodec();
307
- }
302
+ public function __construct(
303
+ private readonly AddressCodec $addressCodec = new AddressCodec(),
304
+ ) {}
308
305
309
306
// Other code omitted for brevity
310
307
public function decode($value): Person
@@ -356,11 +353,9 @@ hard-coded ``DateTimeCodec``:
356
353
357
354
final class PersonCodec implements MongoDB\Codec\DocumentCodec
358
355
{
359
- private MongoDB\Codec\CodecLibrary $library;
360
-
361
- public function __construct(private MongoDB\Codec\CodecLibrary $codecLibrary)
362
- {
363
- }
356
+ public function __construct(
357
+ private readonly MongoDB\Codec\CodecLibrary $codecLibrary,
358
+ ) {}
364
359
365
360
// Other code omitted for brevity
366
361
public function decode($value): Person
0 commit comments