8000 [Uid] Handle predefined namespaces keywords "dns", "url", "oid" and "x500" by fancyweb · Pull Request #40530 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Uid] Handle predefined namespaces keywords "dns", "url", "oid" and "x500" # 8000 40530

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Symfony/Component/Uid/Command/GenerateUuidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function configure(): void
new InputOption('time-based', null, InputOption::VALUE_REQUIRED, 'The timestamp, to generate a time-based UUID: a parsable date/time string'),
new InputOption('node', null, InputOption::VALUE_REQUIRED, 'The UUID whose node part should be used as the node of the generated UUID'),
new InputOption('name-based', null, InputOption::VALUE_REQUIRED, 'The name, to generate a name-based UUID'),
new InputOption('namespace', null, InputOption::VALUE_REQUIRED, 'The UUID to use at the namespace for named-based UUIDs'),
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'),
new InputOption('random-based', null, InputOption::VALUE_NONE, 'To generate a random-based UUID'),
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of UUID to generate', 1),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The UUID output format: rfc4122, base58 or base32', 'rfc4122'),
Expand Down Expand Up @@ -144,7 +144,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
break;

case null !== $name:
if ($namespace) {
if ($namespace && !\in_array($namespace, ['dns', 'url', 'oid', 'x500'], true)) {
try {
$namespace = Uuid::fromString($namespace);
} catch (\InvalidArgumentException $e) {
Expand Down
24 changes: 19 additions & 5 deletions src/Symfony/Component/Uid/Factory/UuidFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public function __construct($defaultClass = UuidV6::class, $timeBasedClass = Uui
$timeBasedNode = Uuid::fromString($timeBasedNode);
}

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

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

if (!$namespace instanceof Uuid) {
$namespace = Uuid::fromString($namespace);
return new NameBasedUuidFactory($this->nameBasedClass, $this->getNamespace($namespace));
}

/**
* @param Uuid|string $namespace
*/
private function getNamespace($namespace): Uuid
{
if ($namespace instanceof Uuid) {
return $namespace;
}

return new NameBasedUuidFactory($this->nameBasedClass, $namespace);
switch ($namespace) {
case 'dns': return new UuidV1(Uuid::NAMESPACE_DNS);
case 'url': return new UuidV1(Uuid::NAMESPACE_URL);
case 'oid': return new UuidV1(Uuid::NAMESPACE_OID);
case 'x500': return new UuidV1(Uuid::NAMESPACE_X500);
default: return Uuid::fromString($namespace);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function testFormat()
Ulid::fromRfc4122(trim($commandTester->getDisplay()));
}

public function testTimestampIncrementWhenGeneratingSeveralUlids()
public function testUlidsAreDifferentWhenGeneratingSeveralNow()
{
$commandTester = new CommandTester(new GenerateUlidCommand());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,13 @@ public function testTimestampIncrementWhenGeneratingSeveralTimeBasedUuids()

$this->assertNotSame($uuids[0], $uuids[1]);
}

public function testNamespacePredefinedKeyword()
{
$commandTester = new CommandTester(new GenerateUuidCommand());

$this->assertSame(0, $commandTester->execute(['--name-based' => 'https://symfony.com', '--namespace' => 'url']));

$this->assertSame('9c7d0eda-982d-5708-b4bd-79b3b179725d', (string) Uuid::fromRfc4122(trim($commandTester->getDisplay())));
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Uid/Tests/Factory/UuidFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ public function testCreateRandom()
{
$this->assertInstanceOf(UuidV4::class, (new UuidFactory())->randomBased()->create());
}

public function testCreateNamedWithNamespacePredefinedKeyword()
{
$this->assertSame('1002657d-3019-59b1-96dc-afc2a3e57c61', (string) (new UuidFactory())->nameBased('dns')->create('symfony.com'));
}
}
0