UuidFactory.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Factory;
  11. use Symfony\Component\Uid\Exception\LogicException;
  12. use Symfony\Component\Uid\Uuid;
  13. use Symfony\Component\Uid\UuidV1;
  14. use Symfony\Component\Uid\UuidV4;
  15. use Symfony\Component\Uid\UuidV5;
  16. use Symfony\Component\Uid\UuidV7;
  17. class UuidFactory
  18. {
  19. private string $defaultClass;
  20. private string $timeBasedClass;
  21. private string $nameBasedClass;
  22. private string $randomBasedClass;
  23. private ?Uuid $timeBasedNode;
  24. private ?Uuid $nameBasedNamespace;
  25. public function __construct(string|int $defaultClass = UuidV7::class, string|int $timeBasedClass = UuidV7::class, string|int $nameBasedClass = UuidV5::class, string|int $randomBasedClass = UuidV4::class, Uuid|string|null $timeBasedNode = null, Uuid|string|null $nameBasedNamespace = null)
  26. {
  27. if (null !== $timeBasedNode && !$timeBasedNode instanceof Uuid) {
  28. $timeBasedNode = Uuid::fromString($timeBasedNode);
  29. }
  30. if (null !== $nameBasedNamespace) {
  31. $nameBasedNamespace = $this->getNamespace($nameBasedNamespace);
  32. }
  33. $this->defaultClass = is_numeric($defaultClass) ? Uuid::class.'V'.$defaultClass : $defaultClass;
  34. $this->timeBasedClass = is_numeric($timeBasedClass) ? Uuid::class.'V'.$timeBasedClass : $timeBasedClass;
  35. $this->nameBasedClass = is_numeric($nameBasedClass) ? Uuid::class.'V'.$nameBasedClass : $nameBasedClass;
  36. $this->randomBasedClass = is_numeric($randomBasedClass) ? Uuid::class.'V'.$randomBasedClass : $randomBasedClass;
  37. $this->timeBasedNode = $timeBasedNode;
  38. $this->nameBasedNamespace = $nameBasedNamespace;
  39. }
  40. public function create(): Uuid
  41. {
  42. $class = $this->defaultClass;
  43. return new $class();
  44. }
  45. public function randomBased(): RandomBasedUuidFactory
  46. {
  47. return new RandomBasedUuidFactory($this->randomBasedClass);
  48. }
  49. public function timeBased(Uuid|string|null $node = null): TimeBasedUuidFactory
  50. {
  51. $node ??= $this->timeBasedNode;
  52. if (null !== $node && !$node instanceof Uuid) {
  53. $node = Uuid::fromString($node);
  54. }
  55. return new TimeBasedUuidFactory($this->timeBasedClass, $node);
  56. }
  57. /**
  58. * @throws LogicException When no namespace is defined
  59. */
  60. public function nameBased(Uuid|string|null $namespace = null): NameBasedUuidFactory
  61. {
  62. $namespace ??= $this->nameBasedNamespace;
  63. if (null === $namespace) {
  64. throw new LogicException(\sprintf('A namespace should be defined when using "%s()".', __METHOD__));
  65. }
  66. return new NameBasedUuidFactory($this->nameBasedClass, $this->getNamespace($namespace));
  67. }
  68. private function getNamespace(Uuid|string $namespace): Uuid
  69. {
  70. if ($namespace instanceof Uuid) {
  71. return $namespace;
  72. }
  73. return match ($namespace) {
  74. 'dns' => new UuidV1(Uuid::NAMESPACE_DNS),
  75. 'url' => new UuidV1(Uuid::NAMESPACE_URL),
  76. 'oid' => new UuidV1(Uuid::NAMESPACE_OID),
  77. 'x500' => new UuidV1(Uuid::NAMESPACE_X500),
  78. default => Uuid::fromString($namespace),
  79. };
  80. }
  81. }