Skip to content

Commit f0ecc10

Browse files
committed
Simplify type class code to the essential
1 parent ff5cd77 commit f0ecc10

File tree

2 files changed

+24
-49
lines changed

2 files changed

+24
-49
lines changed

docs/en/reference/custom-mapping-types.rst

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -135,89 +135,58 @@ Example Implementation (using ``Ramsey\Uuid\Uuid``)::
135135
136136
<?php
137137
138-
namespace My\Project\Types;
138+
namespace App\MongoDB\Types;
139139
140140
use Doctrine\ODM\MongoDB\Types\ClosureToPHP;
141141
use Doctrine\ODM\MongoDB\Types\Type;
142142
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;
146145
147-
final class UuidType extends Type
146+
final class RamseyUuidType extends Type
148147
{
149148
// This trait provides default closureToPHP used during data hydration
150149
use ClosureToPHP;
151150
152-
public function convertToPHPValue(mixed $value): ?Uuid
151+
public function convertToPHPValue(mixed $value): ?RamseyUuid
153152
{
154153
if (null === $value) {
155154
return null;
156155
}
157156
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());
164159
}
165160
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));
178162
}
179163
180-
public function convertToDatabaseValue(mixed $value): ?Binary
164+
public function convertToDatabaseValue(mixed $value): ?BsonBinary
181165
{
182-
if (null === $value || [] === $value) {
166+
if (null === $value) {
183167
return null;
184168
}
185169
186-
if ($value instanceof Binary) {
187-
return $value;
170+
if ($value instanceof RamseyUuid) {
171+
return new BsonBinary($value->getBytes(), BsonBinary::TYPE_UUID);
188172
}
189173
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));
206175
}
207176
}
208177
209178
Register the type in your bootstrap code::
210179

211180
.. code-block:: php
212181
213-
Type::addType(Ramsey\Uuid\Uuid::class, My\Project\Types\UuidType::class);
182+
Type::addType(Ramsey\Uuid\Uuid::class, App\MongoDB\Types\RamseyUuidType::class);
214183
215184
Usage Example::
216185

217186
.. code-block:: php
218187
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;
221190
222191
By using the |FQCN| of the value object class as the type name, the type is
223192
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::
226195
.. code-block:: php
227196
228197
#[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.
230205

231206
.. _`ramsey/uuid library`: https://github.com/ramsey/uuid
232207
.. |FQCN| raw:: html

tests/Doctrine/ODM/MongoDB/Tests/Functional/CustomTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class Country
163163

164164
/** The field type is detected from the property type */
165165
#[ODM\Field(/* type: Language::class */)]
166-
public Language $lang;
166+
public ?Language $lang;
167167
}
168168

169169
class Language

0 commit comments

Comments
 (0)