8000 [Uid] Handle predefined namespaces keywords "dns", "url", "oid" and "… · symfony/symfony@ede46ac · GitHub
[go: up one dir, main page]

Skip to content

Commit ede46ac

Browse files
committed
[Uid] Handle predefined namespaces keywords "dns", "url", "oid" and "x500"
1 parent 1b93740 commit ede46ac

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

src/Symfony/Component/Uid/Command/GenerateUuidCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function configure(): void
4444
new InputOption('time-based', null, InputOption::VALUE_REQUIRED, 'The timestamp, to generate a time-based UUID: a parsable date/time string'),
4545
new InputOption('node', null, InputOption::VALUE_REQUIRED, 'The UUID whose node part should be used as the node of the generated UUID'),
4646
new InputOption('name-based', null, InputOption::VALUE_REQUIRED, 'The name, to generate a name-based UUID'),
47-
new InputOption('namespace', null, InputOption::VALUE_REQUIRED, 'The UUID to use at the namespace for named-based UUIDs'),
47+
new InputOption('namespace', null, InputOption::VALUE_REQUIRED, 'The UUID to use at the namespace for named-based UUIDs, predefined namespaces keywords "dns", "url", "oid" and "x500" are accepted'),
4848
new InputOption('random-based', null, InputOption::VALUE_NONE, 'To generate a random-based UUID'),
4949
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of UUID to generate', 1),
5050
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The UUID output format: rfc4122, base58 or base32', 'rfc4122'),
@@ -144,7 +144,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
144144
break;
145145

146146
case null !== $name:
147-
if ($namespace) {
147+
if ($namespace && !\in_array($namespace, ['dns', 'url', 'oid', 'x500'], true)) {
148148
try {
149149
$namespace = Uuid::fromString($namespace);
150150
} catch (\InvalidArgumentException $e) {

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function __construct($defaultClass = UuidV6::class, $timeBasedClass = Uui
4040
$timeBasedNode = Uuid::fromString($timeBasedNode);
4141
}
4242

43-
if (null !== $nameBasedNamespace && !$nameBasedNamespace instanceof Uuid) {
44-
$nameBasedNamespace = Uuid::fromString($nameBasedNamespace);
43+
if (null !== $nameBasedNamespace) {
44+
$nameBasedNamespace = $this->getNamespace($nameBasedNamespace);
4545
}
4646

4747
$this->defaultClass = is_numeric($defaultClass) ? Uuid::class.'V'.$defaultClass : $defaultClass;
@@ -95,10 +95,24 @@ public function nameBased($namespace = null): NameBasedUuidFactory
9595
throw new \LogicException(sprintf('A namespace should be defined when using "%s()".', __METHOD__));
9696
}
9797

98-
if (!$namespace instanceof Uuid) {
99-
$namespace = Uuid::fromString($namespace);
98+
return new NameBasedUuidFactory($this->nameBasedClass, $this->getNamespace($namespace));
99+
}
100+
101+
/**
102+
* @param Uuid|string $namespace
103+
*/
104+
private function getNamespace($namespace): Uuid
105+
{
106+
if ($namespace instanceof Uuid) {
107+
return $namespace;
100108
}
101109

102-
return new NameBasedUuidFactory($this->nameBasedClass, $namespace);
110+
switch ($namespace) {
111+
case 'dns': return new UuidV1(Uuid::NAMESPACE_DNS);
112+
case 'url': return new UuidV1(Uuid::NAMESPACE_URL);
113+
case 'oid': return new UuidV1(Uuid::NAMESPACE_OID);
114+
case 'x500': return new UuidV1(Uuid::NAMESPACE_X500);
115+
default: return Uuid::fromString($namespace);
116+
}
103117
}
104118
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function testFormat()
9090
Ulid::fromRfc4122(trim($commandTester->getDisplay()));
9191
}
9292

93-
public function testTimestampIncrementWhenGeneratingSeveralUlids()
93+
public function testUlidsAreDifferentWhenGeneratingSeveralNow()
9494
{
9595
$commandTester = new CommandTester(new GenerateUlidCommand());
9696

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,13 @@ public function testTimestampIncrementWhenGeneratingSeveralTimeBasedUuids()
220220

221221
$this->assertNotSame($uuids[0], $uuids[1]);
222222
}
223+
224+
public function testNamespacePredefinedKeyword()
225+
{
226+
$commandTester = new CommandTester(new GenerateUuidCommand());
227+
228+
$this->assertSame(0, $commandTester->execute(['--name-based' => 'https://symfony.com', '--namespace' => 'url']));
229+
230+
$this->assertSame('9c7d0eda-982d-5708-b4bd-79b3b179725d', (string) Uuid::fromRfc4122(trim($commandTester->getDisplay())));
231+
}
223232
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,9 @@ public function testCreateRandom()
9090
{
9191
$this->assertInstanceOf(UuidV4::class, (new UuidFactory())->randomBased()->create());
9292
}
93+
94+
public function testCreateNamedWithNamespacePredefinedKeyword()
95+
{
96+
$this->assertSame('1002657d-3019-59b1-96dc-afc2a3e57c61', (string) (new UuidFactory())->nameBased('dns')->create('symfony.com'));
97+
}
9398
}

0 commit comments

Comments
 (0)
0