UuidV4.php 932 B

123456789101112131415161718192021222324252627282930313233343536
  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;
  11. /**
  12. * A v4 UUID contains a 122-bit random number.
  13. *
  14. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  15. */
  16. class UuidV4 extends Uuid
  17. {
  18. protected const TYPE = 4;
  19. public function __construct(?string $uuid = null)
  20. {
  21. if (null === $uuid) {
  22. $uuid = random_bytes(16);
  23. $uuid[6] = $uuid[6] & "\x0F" | "\x40";
  24. $uuid[8] = $uuid[8] & "\x3F" | "\x80";
  25. $uuid = bin2hex($uuid);
  26. $this->uid = substr($uuid, 0, 8).'-'.substr($uuid, 8, 4).'-'.substr($uuid, 12, 4).'-'.substr($uuid, 16, 4).'-'.substr($uuid, 20, 12);
  27. } else {
  28. parent::__construct($uuid, true);
  29. }
  30. }
  31. }