@@ -123,14 +123,14 @@ Custom Type Example: Mapping a UUID Class
123
123
124
124
You can create a custom mapping type for your own value objects or classes. For
125
125
example, to map a UUID value object using the `ramsey/uuid library `_, you can
126
- implement a type that converts between your class and the MongoDB binary format.
126
+ implement a type that converts between your class and the BSON Binary UUID format.
127
127
128
- This approach works for any custom class—just adapt the conversion logic to your needs.
129
-
130
- Example Implementation (using Ramsey\U uid\U uidInterface)::
128
+ This approach works for any custom class by adapting the conversion logic to your needs.
131
129
130
+ Example Implementation (using ``Ramsey\Uuid\Uuid ``)::
132
131
133
132
.. code-block :: php
133
+
134
134
<?php
135
135
136
136
namespace My\Project\Types;
@@ -144,6 +144,7 @@ Example Implementation (using Ramsey\Uuid\UuidInterface)::
144
144
145
145
final class UuidType extends Type
146
146
{
147
+ // This trait provides default closureToPHP used during data hydration
147
148
use ClosureToPHP;
148
149
149
150
public function convertToPHPValue(mixed $value): ?Uuid
@@ -203,23 +204,24 @@ Example Implementation (using Ramsey\Uuid\UuidInterface)::
203
204
}
204
205
}
205
206
206
- Usage Example ::
207
+ Register the type in your bootstrap code ::
207
208
208
209
.. code-block :: php
209
210
210
- #[Field(type: Uuid::class)]
211
- public Uuid $id;
211
+ Type::addType(Ramsey\Uuid\Uuid::class, My\Project\Types\UuidType::class);
212
212
213
- Register the type in your bootstrap code ::
213
+ Usage Example ::
214
214
215
215
.. code-block :: php
216
216
217
- Type::addType(Ramsey\Uuid\Uuid::class, My\Project\Types\UuidType::class);
217
+ #[Field(type: Ramsey\Uuid\Uuid::class)]
218
+ public Ramsey\Uuid\Uuid $id;
218
219
219
220
.. note ::
220
- The type name should match the support class name (e.g., ``uuid `` for `` UuidType ``) to enable automatic association with the field type.
221
-
222
- This pattern can be adapted for any custom class—just implement the conversion logic for your value object .
221
+ The type name should be the name of the value object class (e.g., ``Ramsey\Uuid\Uuid ``
222
+ for our custom `` UuidType ``) to enable automatic transformation of the values
223
+ of this class in the aggregation builder .
223
224
224
- .. |FQCN | raw :: html <abbr title="Fully-Qualified Class Name">FQCN</abbr>
225
225
.. _`ramsey/uuid library` : https://github.com/ramsey/uuid
226
+ .. |FQCN | raw :: html
227
+ <abbr title="Fully-Qualified Class Name">FQCN</abbr>
0 commit comments