@@ -135,89 +135,58 @@ Example Implementation (using ``Ramsey\Uuid\Uuid``)::
135
135
136
136
<?php
137
137
138
- namespace My\Project \Types;
138
+ namespace App\MongoDB \Types;
139
139
140
140
use Doctrine\ODM\MongoDB\Types\ClosureToPHP;
141
141
use Doctrine\ODM\MongoDB\Types\Type;
142
142
use InvalidArgumentException;
143
- use MongoDB\BSON\Binary;
144
- use Ramsey\Uuid\Uuid;
145
- use Ramsey\Uuid\UuidInterface;
143
+ use MongoDB\BSON\Binary as BsonBinary;
144
+ use Ramsey\Uuid\Uuid as RamseyUuid;
146
145
147
- final class UuidType extends Type
146
+ final class RamseyUuidType extends Type
148
147
{
149
148
// This trait provides default closureToPHP used during data hydration
150
149
use ClosureToPHP;
151
150
152
- public function convertToPHPValue(mixed $value): ?Uuid
151
+ public function convertToPHPValue(mixed $value): ?RamseyUuid
153
152
{
154
153
if (null === $value) {
155
154
return null;
156
155
}
157
156
158
- if ($value instanceof Uuid) {
159
- return $value;
160
- }
161
-
162
- if ($value instanceof Binary) {
163
- return Uuid::fromBytes($value->getData());
157
+ if ($value instanceof BsonBinary && $value->getType() === BsonBinary::TYPE_UUID) {
158
+ return RamseyUuid::fromBytes($value->getData());
164
159
}
165
160
166
- if (is_string($value) && Uuid::isValid($value)) {
167
- return Uuid::fromString($value);
168
- }
169
-
170
- throw new InvalidArgumentException(
171
- sprintf(
172
- 'Could not convert database value "%s" from "%s" to %s',
173
- $value,
174
- get_debug_type($value),
175
- UuidInterface::class
176
- )
177
- );
161
+ throw new \InvalidArgumentException(\sprintf('Could not convert database value from "%s" to %s',get_debug_type($value),RamseyUuid::class));
178
162
}
179
163
180
- public function convertToDatabaseValue(mixed $value): ?Binary
164
+ public function convertToDatabaseValue(mixed $value): ?BsonBinary
181
165
{
182
- if (null === $value || [] === $value ) {
166
+ if (null === $value) {
183
167
return null;
184
168
}
185
169
186
- if ($value instanceof Binary ) {
187
- return $value;
170
+ if ($value instanceof RamseyUuid ) {
171
+ return new BsonBinary( $value->getBytes(), BsonBinary::TYPE_UUID) ;
188
172
}
189
173
190
- if (is_string($value) && Uuid::isValid($value)) {
191
- $value = Uuid::fromString($value)->getBytes();
192
- }
193
-
194
- if ($value instanceof Uuid) {
195
- return new Binary($value->getBytes(), Binary::TYPE_UUID);
196
- }
197
-
198
- throw new InvalidArgumentException(
199
- sprintf(
200
- 'Could not convert database value "%s" from "%s" to %s',
201
- $value,
202
- get_debug_type($value),
203
- Binary::class
204
- )
205
- );
174
+ throw new \InvalidArgumentException(\sprintf('Could not convert database value from "%s" to %s', get_debug_type($value), Binary::class));
206
175
}
207
176
}
208
177
209
178
Register the type in your bootstrap code::
210
179
211
180
.. code-block :: php
212
181
213
- Type::addType(Ramsey\Uuid\Uuid::class, My\Project \Types\UuidType ::class);
182
+ Type::addType(Ramsey\Uuid\Uuid::class, App\MongoDB \Types\RamseyUuidType ::class);
214
183
215
184
Usage Example::
216
185
217
186
.. code-block :: php
218
187
219
- #[Field(type: Ramsey\Uuid\Uuid::class)]
220
- public Ramsey\Uuid\Uuid $id;
188
+ #[Field(type: \ Ramsey\Uuid\Uuid::class)]
189
+ public ?\ Ramsey\Uuid\Uuid $id;
221
190
222
191
By using the |FQCN | of the value object class as the type name, the type is
223
192
automatically used when encountering a property of that class. This means you
@@ -226,7 +195,13 @@ can omit the ``type`` option when defining the field mapping::
226
195
.. code-block :: php
227
196
228
197
#[Field]
229
- public Ramsey\Uuid\Uuid $id;
198
+ public ?\Ramsey\Uuid\Uuid $id;
199
+
200
+ .. note ::
201
+
202
+ This implementation of ``RamseyUuidType `` is volontary simple and does not
203
+ handle all edge cases, but it should give you a good starting point for
204
+ implementing your own custom types.
230
205
231
206
.. _`ramsey/uuid library` : https://github.com/ramsey/uuid
232
207
.. |FQCN | raw :: html
0 commit comments