|
| 1 | +# Converting BSON data through codecs |
| 2 | + |
| 3 | +The codec system is a more advanced way to convert BSON data to native types and back, designed for libraries with more |
| 4 | +advanced use cases, e.g. object mappers. It is designed to decouple the serialisation logic from the data model, |
| 5 | +allowing for more flexible implementations. |
| 6 | + |
| 7 | +## Encoders and Decoders |
| 8 | + |
| 9 | +The codec interface is split into two smaller interfaces: encoders and decoders. Both interfaces are marked as internal, |
| 10 | +as users are only expected to interact with the Codec interface. The interfaces are typed using Psalm generics, allowing |
| 11 | +for better type checking when they are used. Without type annotations, the interfaces are equivalent to the following: |
| 12 | + |
| 13 | +```php |
| 14 | +namespace MongoDB\Codec; |
| 15 | + |
| 16 | +interface Decoder |
| 17 | +{ |
| 18 | + public function canDecode(mixed $value): bool; |
| 19 | + |
| 20 | + public function decode(mixed $value): mixed; |
| 21 | + |
| 22 | + public function decodeIfSupported(mixed $value): mixed; |
| 23 | +} |
| 24 | + |
| 25 | +interface Encoder |
| 26 | +{ |
| 27 | + public function canEncode(mixed $value): bool; |
| 28 | + |
| 29 | + public function encode(mixed $value): mixed; |
| 30 | + |
| 31 | + public function encodeIfSupported(mixed $value): mixed; |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +## Codec interface |
| 36 | + |
| 37 | +The `Codec` interface combines decoding and encoding into a single interface. This will be used for most values except |
| 38 | +for documents where a more specific `DocumentCodec` is provided. |
| 39 | + |
| 40 | +The base interface supports encoding from a `NativeType` to a `BSONType` and back. Helper methods to determine whether a |
| 41 | +value is supported are provided. The `decodeIfSupported` and `encodeIfSupported` methods are useful to have a codec |
| 42 | +encode or decode a value only if it is supported. If it is not supported, the original value is returned. |
| 43 | + |
| 44 | +```php |
| 45 | +namespace MongoDB\Codec; |
| 46 | + |
| 47 | +/** |
| 48 | + * @psalm-template BSONType |
| 49 | + * @psalm-template NativeType |
| 50 | + * @template-extends Decoder<BSONType, NativeType> |
| 51 | + * @template-extends Encoder<BSONType, NativeType> |
| 52 | + */ |
| 53 | +interface Codec extends Decoder, Encoder |
| 54 | +{ |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Document codec |
| 59 | + |
| 60 | +The document codec is special as it is guaranteed to always encode to a BSON document instance and decode to a PHP |
| 61 | +object. Document codecs can be provided to a `MongoDB\Collection` instance to have it automatically decode data read |
| 62 | +from the database. Likewise, any supported value is encoded before writing to the database in `insert` and `replace` |
| 63 | +operations. |
| 64 | + |
| 65 | +```php |
| 66 | +namespace MongoDB\Codec; |
| 67 | + |
| 68 | +use MongoDB\BSON\Document; |
| 69 | + |
| 70 | +/** |
| 71 | + * @template ObjectType of object |
| 72 | + * @extends Codec<ObjectType, Document> |
| 73 | + */ |
| 74 | +interface DocumentCodec extends Codec |
| 75 | +{ |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## Using codecs |
| 80 | + |
| 81 | +The `MongoDB\Collection` class and all operations that work with documents now take a `codec` option. This can be |
| 82 | +an instance of a `DocumentCodec` that will be used to encode documents (for insert and replace operations) and decode |
| 83 | +them into PHP objects when reading data. |
| 84 | + |
| 85 | +### Codecs and type maps |
| 86 | + |
| 87 | +When providing a value for the `codec` option, it takes precedence over the `typeMap` option. An exception is made |
| 88 | +when the `codec` option was specified on the collection level, but an operation is given a `typeMap` option. In |
| 89 | +this case, the type map is used. The precedence order is as follows: |
| 90 | + |
| 91 | +* operation-level `codec` option |
| 92 | +* operation-level `typeMap` option |
| 93 | +* collection-level `codec` option |
| 94 | +* collection-level `typeMap` option |
| 95 | + |
| 96 | +Codecs are not inherited from the client or the database object, as they are purely used for operations that return |
| 97 | +documents. However, database- or client-level aggregation commands will take an operation-level codec option to |
| 98 | +decode the resulting documents. |
| 99 | + |
0 commit comments