Uuid.php 533 B

123456789101112131415161718192021
  1. <?php
  2. namespace App\Service\Utils;
  3. class Uuid
  4. {
  5. /**
  6. * Generates an UUID v4.
  7. *
  8. * @param ?int $length To limit the length of the uuid (standard uuid count 36 cars). Warning: reducing
  9. * the number of characters breaks the warranty of global unity.
  10. *
  11. * @throws \Exception
  12. */
  13. public static function uuid(?int $length = null): string
  14. {
  15. $uuid = \Ramsey\Uuid\Uuid::uuid4()->toString();
  16. return $length !== null ? substr($uuid, 0, $length) : $uuid;
  17. }
  18. }