TimeBasedUuidFactory.php 882 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. * @param class-string<Uuid&TimeBasedUidInterface> $class
  17. */
  18. public function __construct(
  19. private string $class,
  20. private ?Uuid $node = null,
  21. ) {
  22. }
  23. public function create(?\DateTimeInterface $time = null): Uuid&TimeBasedUidInterface
  24. {
  25. $class = $this->class;
  26. if (null === $time && null === $this->node) {
  27. return new $class();
  28. }
  29. return new $class($class::generate($time, $this->node));
  30. }
  31. }