Skip to content

Commit 0037220

Browse files
committed
Handle code review
1 parent 5e32489 commit 0037220

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

docs/tutorial/codecs.txt

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ To then use this codec with a collection, specify the ``codec`` option when sele
9494

9595
<?php
9696

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+
]);
98101

99102
$person = new Person('Jane Doe');
100103
$collection->insertOne($person);
@@ -114,7 +117,7 @@ encoded will result in an exception. The ``aggregate``, ``find``, ``findOne``, `
114117
``findOneAndReplace``, and ``findOneAndUpdate`` operations will attempt to decode returned documents using the provided
115118
codec. If the codec does not support the data returned, an exception will be thrown. You can disable codec usage for a
116119
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.
118121

119122
Generic Codecs
120123
--------------
@@ -193,12 +196,9 @@ code omitted for brevity):
193196

194197
final class PersonCodec implements MongoDB\Codec\DocumentCodec
195198
{
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+
) {}
202202

203203
// Other code omitted for brevity
204204
public function decode($value): Person
@@ -299,12 +299,9 @@ the example below excludes some code we've already shown in previous examples.
299299

300300
final class PersonCodec implements MongoDB\Codec\DocumentCodec
301301
{
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+
) {}
308305

309306
// Other code omitted for brevity
310307
public function decode($value): Person
@@ -356,11 +353,9 @@ hard-coded ``DateTimeCodec``:
356353

357354
final class PersonCodec implements MongoDB\Codec\DocumentCodec
358355
{
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+
) {}
364359

365360
// Other code omitted for brevity
366361
public function decode($value): Person

0 commit comments

Comments
 (0)