Skip to content

Natural language join #43849

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

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 14 additions & 2 deletions src/Symfony/Component/Uid/Command/GenerateUlidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function configure(): void
->setDefinition([
new InputOption('time', null, InputOption::VALUE_REQUIRED, 'The ULID timestamp: a parsable date/time string'),
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of ULID to generate', 1),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The ULID output format: base32, base58 or rfc4122', 'base32'),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, sprintf('The ULID output format: %s', $this->getNaturalLanguageJoin($this->getAvailableFormatOptions())), 'base32'),
])
->setDescription(self::$defaultDescription)
->setHelp(<<<'EOF'
Expand Down Expand Up @@ -90,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (\in_array($formatOption, $this->getAvailableFormatOptions())) {
$format = 'to'.ucfirst($formatOption);
} else {
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format')));
$io->error(sprintf('Invalid format "%s", did you mean %s?', $input->getOption('format'), $this->getNaturalLanguageJoin($this->getAvailableFormatOptions())));

return 1;
}
Expand Down Expand Up @@ -124,4 +124,16 @@ private function getAvailableFormatOptions(): array
'rfc4122',
];
}

private function getNaturalLanguageJoin(array $list, string $conjunction = 'or')
{
$last = array_pop($list);
if ($list) {
return implode(', ', array_map(function ($item) {
return sprintf('"%s"', $item);
}, $list)).' '.$conjunction.' "'.$last.'"';
}

return $last;
}
}
16 changes: 14 additions & 2 deletions src/Symfony/Component/Uid/Command/GenerateUuidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function configure(): void
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'),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, sprintf('The UUID output format: %s', $this->getNaturalLanguageJoin($this->getAvailableFormatOptions())), 'rfc4122'),
])
->setDescription(self::$defaultDescription)
->setHelp(<<<'EOF'
Expand Down Expand Up @@ -181,7 +181,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (\in_array($formatOption, $this->getAvailableFormatOptions())) {
$format = 'to'.ucfirst($formatOption);
} else {
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $formatOption));
$io->error(sprintf('Invalid format "%s", did you mean %s?', $input->getOption('format'), $this->getNaturalLanguageJoin($this->getAvailableFormatOptions())));

return 1;
}
Expand Down Expand Up @@ -215,4 +215,16 @@ private function getAvailableFormatOptions(): array
'rfc4122',
];
}

private function getNaturalLanguageJoin(array $list, string $conjunction = 'or')
{
$last = array_pop($list);
if ($list) {
return implode(', ', array_map(function ($item) {
return sprintf('"%s"', $item);
}, $list)).' '.$conjunction.' "'.$last.'"';
}

return $last;
}
}