-
-
Notifications
You must be signed in to change notification settings - Fork 513
Support symfony UUID #2826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Support symfony UUID #2826
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
022641c
Add new type for Symfony UUIDs
alcaeus 7c2ccf9
Add UUID type to type class
alcaeus 3278765
Don't transform PHP UUIDs again
alcaeus 570bfff
Support generating Symfony UUID objects in AUTO generator
alcaeus e15ac97
Deprecate string-based UUID generator
alcaeus 0d752d5
Add documentation around UUIDs
alcaeus 87d05d6
Apply Copilot review feedback
alcaeus 4e658da
Unify logic in binary uuid type
alcaeus e5ba6bf
Apply code review feedback
alcaeus 02e4966
Undo type conditional change to fix errors
alcaeus 96b98b8
Add documentation around UUID generator deprecation
alcaeus 97d2774
Link to Symfony UID component
alcaeus 0cf5290
Explain deprecation of UUID generator in docs
alcaeus e4c07d6
Simplify ID generator docblocks
alcaeus 26d71ea
Fix ObjectId check when using auto generation
alcaeus 8940f86
Use InvalidArgumentException in type
alcaeus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ODM\MongoDB\Id; | ||
|
||
use Doctrine\ODM\MongoDB\DocumentManager; | ||
use MongoDB\BSON\ObjectId; | ||
|
||
/** @internal */ | ||
final class ObjectIdGenerator extends AbstractIdGenerator | ||
{ | ||
public function generate(DocumentManager $dm, object $document): ObjectId | ||
{ | ||
return new ObjectId(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ODM\MongoDB\Id; | ||
|
||
use Doctrine\ODM\MongoDB\DocumentManager; | ||
use InvalidArgumentException; | ||
use Symfony\Component\Uid\Uuid; | ||
use Symfony\Component\Uid\UuidV1; | ||
use Symfony\Component\Uid\UuidV4; | ||
use Symfony\Component\Uid\UuidV7; | ||
|
||
use function array_values; | ||
use function implode; | ||
use function in_array; | ||
use function sprintf; | ||
|
||
/** @internal */ | ||
final class SymfonyUuidGenerator extends AbstractIdGenerator | ||
{ | ||
private const SUPPORTED_TYPES = [ | ||
1 => UuidV1::class, | ||
4 => UuidV4::class, | ||
7 => UuidV7::class, | ||
]; | ||
|
||
public function __construct(private readonly string $class) | ||
{ | ||
if (! in_array($this->class, self::SUPPORTED_TYPES, true)) { | ||
throw new InvalidArgumentException(sprintf('Invalid UUID type "%s". Expected one of: %s.', $this->class, implode(', ', array_values(self::SUPPORTED_TYPES)))); | ||
} | ||
} | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function generate(DocumentManager $dm, object $document): Uuid | ||
{ | ||
return new $this->class(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ODM\MongoDB\Types; | ||
|
||
use InvalidArgumentException; | ||
use MongoDB\BSON\Binary; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
use function get_debug_type; | ||
use function is_string; | ||
use function sprintf; | ||
|
||
class BinaryUuidType extends Type | ||
{ | ||
public function convertToDatabaseValue(mixed $value): ?Binary | ||
{ | ||
return match (true) { | ||
$value === null => null, | ||
$value instanceof Binary => $value, | ||
$value instanceof Uuid => new Binary($value->toBinary(), Binary::TYPE_UUID), | ||
is_string($value) => new Binary(Uuid::fromString($value)->toBinary(), Binary::TYPE_UUID), | ||
default => throw new InvalidArgumentException(sprintf('Invalid data type %s received for UUID', get_debug_type($value))), | ||
}; | ||
} | ||
|
||
public function convertToPHPValue(mixed $value): Uuid | ||
{ | ||
if ($value instanceof Uuid) { | ||
return $value; | ||
} | ||
|
||
if (! $value instanceof Binary) { | ||
throw new InvalidArgumentException(sprintf('Invalid data of type "%s" received for Uuid', get_debug_type($value))); | ||
} | ||
|
||
if ($value->getType() !== Binary::TYPE_UUID) { | ||
throw new InvalidArgumentException(sprintf('Invalid binary data of type %d received for Uuid', $value->getType())); | ||
} | ||
|
||
return Uuid::fromBinary($value->getData()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked that this returns instances of |
||
} | ||
|
||
public function closureToMongo(): string | ||
{ | ||
return <<<'PHP' | ||
$return = match (true) { | ||
$value === null => null, | ||
$value instanceof \MongoDB\BSON\Binary => $value, | ||
$value instanceof \Symfony\Component\Uid\Uuid => new \MongoDB\BSON\Binary($value->toBinary(), \MongoDB\BSON\Binary::TYPE_UUID), | ||
is_string($value) => new \MongoDB\BSON\Binary(\Symfony\Component\Uid\Uuid::fromString($value)->toBinary(), \MongoDB\BSON\Binary::TYPE_UUID), | ||
default => throw new \InvalidArgumentException(sprintf('Invalid data type %s received for UUID', get_debug_type($value))), | ||
}; | ||
PHP; | ||
} | ||
|
||
public function closureToPHP(): string | ||
{ | ||
return <<<'PHP' | ||
if ($value instanceof \Symfony\Component\Uid\Uuid) { | ||
$return = $value; | ||
return; | ||
} | ||
|
||
if (! $value instanceof \MongoDB\BSON\Binary) { | ||
throw new \InvalidArgumentException(sprintf('Invalid data of type "%s" received for Uuid', get_debug_type($value))); | ||
} | ||
|
||
if ($value->getType() !== \MongoDB\BSON\Binary::TYPE_UUID) { | ||
throw new \InvalidArgumentException(sprintf('Invalid binary data of type %d received for Uuid', $value->getType())); | ||
} | ||
|
||
$return = \Symfony\Component\Uid\Uuid::fromBinary($value->getData()); | ||
PHP; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.