UuidFactory.php 3.2 KB

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