InspectUlidCommand.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Uid\Command;
  11. use Symfony\Component\Console\Attribute\AsCommand;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\TableSeparator;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Style\SymfonyStyle;
  19. use Symfony\Component\Uid\Ulid;
  20. #[AsCommand(name: 'ulid:inspect', description: 'Inspect a ULID')]
  21. class InspectUlidCommand extends Command
  22. {
  23. protected function configure(): void
  24. {
  25. $this
  26. ->setDefinition([
  27. new InputArgument('ulid', InputArgument::REQUIRED, 'The ULID to inspect'),
  28. ])
  29. ->setHelp(<<<'EOF'
  30. The <info>%command.name%</info> displays information about a ULID.
  31. <info>php %command.full_name% 01EWAKBCMWQ2C94EXNN60ZBS0Q</info>
  32. <info>php %command.full_name% 1BVdfLn3ERmbjYBLCdaaLW</info>
  33. <info>php %command.full_name% 01771535-b29c-b898-923b-b5a981f5e417</info>
  34. EOF
  35. )
  36. ;
  37. }
  38. protected function execute(InputInterface $input, OutputInterface $output): int
  39. {
  40. $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  41. try {
  42. $ulid = Ulid::fromString($input->getArgument('ulid'));
  43. } catch (\InvalidArgumentException $e) {
  44. $io->error($e->getMessage());
  45. return 1;
  46. }
  47. $io->table(['Label', 'Value'], [
  48. ['toBase32 (canonical)', (string) $ulid],
  49. ['toBase58', $ulid->toBase58()],
  50. ['toRfc4122', $ulid->toRfc4122()],
  51. ['toHex', $ulid->toHex()],
  52. new TableSeparator(),
  53. ['Time', $ulid->getDateTime()->format('Y-m-d H:i:s.v \U\T\C')],
  54. ]);
  55. return 0;
  56. }
  57. }