Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5f2bc27

Browse filesBrowse files
committed
[Uid] Handle predefined namespaces keywords "dns", "url", "oid" and "x500"
1 parent 1b93740 commit 5f2bc27
Copy full SHA for 5f2bc27

File tree

5 files changed

+46
-8
lines changed
Filter options

5 files changed

+46
-8
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Command/GenerateUuidCommand.php
+2-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Factory/UuidFactory.php
+29-5Lines changed: 29 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,34 @@ 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;
108+
}
109+
110+
if ('dns' === $namespace) {
111+
return new UuidV1(Uuid::NAMESPACE_DNS);
112+
}
113+
114+
if ('url' === $namespace) {
115+
return new UuidV1(Uuid::NAMESPACE_URL);
116+
}
117+
118+
if ('oid' === $namespace) {
119+
return new UuidV1(Uuid::NAMESPACE_OID);
120+
}
121+
122+
if ('x500' === $namespace) {
123+
return new UuidV1(Uuid::NAMESPACE_X500);
100124
}
101125

102-
return new NameBasedUuidFactory($this->nameBasedClass, $namespace);
126+
return Uuid::fromString($namespace);
103127
}
104128
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php
+9Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/Factory/UuidFactoryTest.php
+5Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.