TimeBasedUuidFactory.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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\TimeBasedUidInterface;
  12. use Symfony\Component\Uid\Uuid;
  13. class TimeBasedUuidFactory
  14. {
  15. /**
  16. * @var class-string<Uuid&TimeBasedUidInterface>
  17. */
  18. private string $class;
  19. private ?Uuid $node;
  20. /**
  21. * @param class-string<Uuid&TimeBasedUidInterface> $class
  22. */
  23. public function __construct(string $class, ?Uuid $node = null)
  24. {
  25. $this->class = $class;
  26. $this->node = $node;
  27. }
  28. public function create(?\DateTimeInterface $time = null): Uuid&TimeBasedUidInterface
  29. {
  30. $class = $this->class;
  31. if (null === $time && null === $this->node) {
  32. return new $class();
  33. }
  34. return new $class($class::generate($time, $this->node));
  35. }
  36. }