Skip to content

Commit 2cc4f5f

Browse files
[Uid] Default to UuidV7 when using UuidFactory
1 parent b8eaa4b commit 2cc4f5f

File tree

7 files changed

+26
-19
lines changed

7 files changed

+26
-19
lines changed

UPGRADE-7.4.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ Translation
108108

109109
* Deprecate `TranslatableMessage::__toString`
110110

111+
Uid
112+
---
113+
114+
* Default to `UuidV7` when using `UuidFactory`
115+
111116
Validator
112117
---------
113118

src/Symfony/Bridge/Doctrine/Tests/IdGenerator/UuidGeneratorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
1717
use Symfony\Component\Uid\Factory\UuidFactory;
18+
use Symfony\Component\Uid\TimeBasedUidInterface;
1819
use Symfony\Component\Uid\Uuid;
1920
use Symfony\Component\Uid\UuidV4;
20-
use Symfony\Component\Uid\UuidV6;
2121

2222
class UuidGeneratorTest extends TestCase
2323
{
@@ -47,13 +47,13 @@ public function testUuidfactory()
4747
{
4848
$em = (new \ReflectionClass(EntityManager::class))->newInstanceWithoutConstructor();
4949
$generator = new UuidGenerator();
50-
$this->assertInstanceOf(UuidV6::class, $generator->generate($em, new Entity()));
50+
$this->assertInstanceOf(TimeBasedUidInterface::class, $generator->generate($em, new Entity()));
5151

5252
$generator = $generator->randomBased();
5353
$this->assertInstanceOf(UuidV4::class, $generator->generate($em, new Entity()));
5454

5555
$generator = $generator->timeBased();
56-
$this->assertInstanceOf(UuidV6::class, $generator->generate($em, new Entity()));
56+
$this->assertInstanceOf(TimeBasedUidInterface::class, $generator->generate($em, new Entity()));
5757

5858
$generator = $generator->nameBased('prop1', Uuid::NAMESPACE_OID);
5959
$this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '3'), $generator->generate($em, new Entity()));

src/Symfony/Component/Uid/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add microsecond precision to UUIDv7
8+
* Default to `UuidV7` when using `UuidFactory`
89

910
7.3
1011
---

src/Symfony/Component/Uid/Factory/UuidFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Component\Uid\UuidV1;
1717
use Symfony\Component\Uid\UuidV4;
1818
use Symfony\Component\Uid\UuidV5;
19-
use Symfony\Component\Uid\UuidV6;
19+
use Symfony\Component\Uid\UuidV7;
2020

2121
class UuidFactory
2222
{
@@ -27,7 +27,7 @@ class UuidFactory
2727
private ?Uuid $timeBasedNode;
2828
private ?Uuid $nameBasedNamespace;
2929

30-
public function __construct(string|int $defaultClass = UuidV6::class, string|int $timeBasedClass = UuidV6::class, string|int $nameBasedClass = UuidV5::class, string|int $randomBasedClass = UuidV4::class, Uuid|string|null $timeBasedNode = null, Uuid|string|null $nameBasedNamespace = null)
30+
public function __construct(string|int $defaultClass = UuidV7::class, string|int $timeBasedClass = UuidV7::class, string|int $nameBasedClass = UuidV5::class, string|int $randomBasedClass = UuidV4::class, Uuid|string|null $timeBasedNode = null, Uuid|string|null $nameBasedNamespace = null)
3131
{
3232
if (null !== $timeBasedNode && !$timeBasedNode instanceof Uuid) {
3333
$timeBasedNode = Uuid::fromString($timeBasedNode);

src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
use Symfony\Component\Uid\UuidV3;
2424
use Symfony\Component\Uid\UuidV4;
2525
use Symfony\Component\Uid\UuidV5;
26-
use Symfony\Component\Uid\UuidV6;
26+
use Symfony\Component\Uid\UuidV7;
2727

2828
final class GenerateUuidCommandTest extends TestCase
2929
{
3030
public function testDefaults()
3131
{
3232
$commandTester = new CommandTester(new GenerateUuidCommand());
3333
$this->assertSame(0, $commandTester->execute([]));
34-
$this->assertInstanceOf(UuidV6::class, Uuid::fromRfc4122(trim($commandTester->getDisplay())));
34+
$this->assertInstanceOf(UuidV7::class, Uuid::fromRfc4122(trim($commandTester->getDisplay())));
3535

3636
$commandTester = new CommandTester(new GenerateUuidCommand(new UuidFactory(UuidV4::class)));
3737
$this->assertSame(0, $commandTester->execute([]));
@@ -59,17 +59,17 @@ public function testTimeBasedWithTimestampBeforeUUIDEpoch()
5959
$commandTester = new CommandTester(new GenerateUuidCommand());
6060

6161
$this->assertSame(1, $commandTester->execute(['--time-based' => '@-16807797990']));
62-
$this->assertStringContainsString('The given UUID date cannot be earlier than 1582-10-15.', $commandTester->getDisplay());
62+
$this->assertStringContainsString('The timestamp must be positive.', $commandTester->getDisplay());
6363
}
6464

6565
public function testTimeBased()
6666
{
6767
$commandTester = new CommandTester(new GenerateUuidCommand());
6868
$this->assertSame(0, $commandTester->execute(['--time-based' => 'now']));
69-
$this->assertInstanceOf(UuidV6::class, Uuid::fromRfc4122(trim($commandTester->getDisplay())));
69+
$this->assertInstanceOf(UuidV7::class, Uuid::fromRfc4122(trim($commandTester->getDisplay())));
7070

7171
$commandTester = new CommandTester(new GenerateUuidCommand(new UuidFactory(
72-
UuidV6::class,
72+
UuidV7::class,
7373
UuidV1::class,
7474
UuidV5::class,
7575
UuidV4::class,
@@ -104,7 +104,7 @@ public function testNameBased()
104104
$this->assertInstanceOf(UuidV5::class, Uuid::fromRfc4122(trim($commandTester->getDisplay())));
105105

106106
$commandTester = new CommandTester(new GenerateUuidCommand(new UuidFactory(
107-
UuidV6::class,
107+
UuidV7::class,
108108
UuidV1::class,
109109
UuidV3::class,
110110
UuidV4::class,

src/Symfony/Component/Uid/Tests/Factory/UuidFactoryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Uid\UuidV4;
2222
use Symfony\Component\Uid\UuidV5;
2323
use Symfony\Component\Uid\UuidV6;
24+
use Symfony\Component\Uid\UuidV7;
2425

2526
final class UuidFactoryTest extends TestCase
2627
{
@@ -51,7 +52,7 @@ public function testCreateNamed()
5152

5253
public function testCreateTimedDefaultVersion()
5354
{
54-
$this->assertInstanceOf(UuidV6::class, (new UuidFactory())->timeBased()->create());
55+
$this->assertInstanceOf(UuidV7::class, (new UuidFactory())->timeBased()->create());
5556
$this->assertInstanceOf(UuidV1::class, (new UuidFactory(6, 1))->timeBased()->create());
5657
}
5758

@@ -83,7 +84,7 @@ public function testCreateTimed()
8384
public function testInvalidCreateTimed()
8485
{
8586
$this->expectException(InvalidArgumentException::class);
86-
$this->expectExceptionMessage('The given UUID date cannot be earlier than 1582-10-15.');
87+
$this->expectExceptionMessage('The timestamp must be positive.');
8788

8889
(new UuidFactory())->timeBased()->create(new \DateTimeImmutable('@-12219292800.001000'));
8990
}

src/Symfony/Component/Uid/Ulid.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public function __construct(?string $ulid = null)
3333
if (null === $ulid) {
3434
$this->uid = static::generate();
3535
} elseif (self::NIL === $ulid) {
36-
$this->uid = $ulid;
37-
} elseif (self::MAX === strtr($ulid, 'z', 'Z')) {
38-
$this->uid = $ulid;
36+
$this->uid = self::NIL;
3937
} else {
40-
if (!self::isValid($ulid)) {
38+
$this->uid = strtoupper($ulid);
39+
40+
if (self::MAX === $this->uid) {
41+
$this->uid = self::MAX;
42+
} elseif (!self::isValid($ulid)) {
4143
throw new InvalidArgumentException(\sprintf('Invalid ULID: "%s".', $ulid));
4244
}
43-
44-
$this->uid = strtoupper($ulid);
4545
}
4646
}
4747

0 commit comments

Comments
 (0)