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