vendor/symfony/uid/Factory/UuidFactory.php line 29

Open in your IDE?
  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 $timeBasedNode;
  23.     private $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 $timeBasedNode = null, Uuid|string $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.     /**
  40.      * @return UuidV6|UuidV4|UuidV1
  41.      */
  42.     public function create(): UuidV6|UuidV4|UuidV1
  43.     {
  44.         $class = $this->defaultClass;
  45.         return new $class();
  46.     }
  47.     public function randomBased(): RandomBasedUuidFactory
  48.     {
  49.         return new RandomBasedUuidFactory($this->randomBasedClass);
  50.     }
  51.     public function timeBased(Uuid|string $node = null): TimeBasedUuidFactory
  52.     {
  53.         $node ?? $node = $this->timeBasedNode;
  54.         if (null !== $node && !$node instanceof Uuid) {
  55.             $node = Uuid::fromString($node);
  56.         }
  57.         return new TimeBasedUuidFactory($this->timeBasedClass, $node);
  58.     }
  59.     public function nameBased(Uuid|string $namespace = null): NameBasedUuidFactory
  60.     {
  61.         $namespace ?? $namespace = $this->nameBasedNamespace;
  62.         if (null === $namespace) {
  63.             throw new \LogicException(sprintf('A namespace should be defined when using "%s()".', __METHOD__));
  64.         }
  65.         return new NameBasedUuidFactory($this->nameBasedClass, $this->getNamespace($namespace));
  66.     }
  67.     private function getNamespace(Uuid|string $namespace): Uuid
  68.     {
  69.         if ($namespace instanceof Uuid) {
  70.             return $namespace;
  71.         }
  72.         switch ($namespace) {
  73.             case 'dns': return new UuidV1(Uuid::NAMESPACE_DNS);
  74.             case 'url': return new UuidV1(Uuid::NAMESPACE_URL);
  75.             case 'oid': return new UuidV1(Uuid::NAMESPACE_OID);
  76.             case 'x500': return new UuidV1(Uuid::NAMESPACE_X500);
  77.             default: return Uuid::fromString($namespace);
  78.         }
  79.     }
  80. }