NameBasedUuidFactory.php 1017 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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\UuidV3;
  13. use Symfony\Component\Uid\UuidV5;
  14. class NameBasedUuidFactory
  15. {
  16. public function __construct(
  17. private string $class,
  18. private Uuid $namespace,
  19. ) {
  20. }
  21. public function create(string $name): UuidV5|UuidV3
  22. {
  23. switch ($class = $this->class) {
  24. case UuidV5::class: return Uuid::v5($this->namespace, $name);
  25. case UuidV3::class: return Uuid::v3($this->namespace, $name);
  26. }
  27. if (is_subclass_of($class, UuidV5::class)) {
  28. $uuid = Uuid::v5($this->namespace, $name);
  29. } else {
  30. $uuid = Uuid::v3($this->namespace, $name);
  31. }
  32. return new $class($uuid);
  33. }
  34. }