vendor/symfony/doctrine-bridge/IdGenerator/UuidGenerator.php line 26

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\Bridge\Doctrine\IdGenerator;
  11. use Doctrine\ORM\EntityManager;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\ORM\Id\AbstractIdGenerator;
  14. use Symfony\Component\Uid\Factory\UuidFactory;
  15. use Symfony\Component\Uid\Uuid;
  16. final class UuidGenerator extends AbstractIdGenerator
  17. {
  18.     private $protoFactory;
  19.     private $factory;
  20.     private $entityGetter;
  21.     public function __construct(UuidFactory $factory null)
  22.     {
  23.         $this->protoFactory $this->factory $factory ?? new UuidFactory();
  24.     }
  25.     /**
  26.      * doctrine/orm < 2.11 BC layer.
  27.      */
  28.     public function generate(EntityManager $em$entity): Uuid
  29.     {
  30.         return $this->generateId($em$entity);
  31.     }
  32.     public function generateId(EntityManagerInterface $em$entity): Uuid
  33.     {
  34.         if (null !== $this->entityGetter) {
  35.             if (\is_callable([$entity$this->entityGetter])) {
  36.                 return $this->factory->create($entity->{$this->entityGetter}());
  37.             }
  38.             return $this->factory->create($entity->{$this->entityGetter});
  39.         }
  40.         return $this->factory->create();
  41.     }
  42.     /**
  43.      * @param Uuid|string|null $namespace
  44.      *
  45.      * @return static
  46.      */
  47.     public function nameBased(string $entityGetter$namespace null): self
  48.     {
  49.         $clone = clone $this;
  50.         $clone->factory $clone->protoFactory->nameBased($namespace);
  51.         $clone->entityGetter $entityGetter;
  52.         return $clone;
  53.     }
  54.     /**
  55.      * @return static
  56.      */
  57.     public function randomBased(): self
  58.     {
  59.         $clone = clone $this;
  60.         $clone->factory $clone->protoFactory->randomBased();
  61.         $clone->entityGetter null;
  62.         return $clone;
  63.     }
  64.     /**
  65.      * @param Uuid|string|null $node
  66.      *
  67.      * @return static
  68.      */
  69.     public function timeBased($node null): self
  70.     {
  71.         $clone = clone $this;
  72.         $clone->factory $clone->protoFactory->timeBased($node);
  73.         $clone->entityGetter null;
  74.         return $clone;
  75.     }
  76. }