vendor/shopware/core/Framework/Uuid/Uuid.php line 86

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Uuid;
  3. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  4. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidLengthException;
  5. class Uuid
  6. {
  7.     /**
  8.      * Regular expression pattern for matching a valid UUID of any variant.
  9.      */
  10.     public const VALID_PATTERN '^[0-9a-f]{32}$';
  11.     public static function randomHex(): string
  12.     {
  13.         $hex bin2hex(random_bytes(16));
  14.         $timeHi self::applyVersion(mb_substr($hex124), 4);
  15.         $clockSeqHi self::applyVariant(hexdec(mb_substr($hex162)));
  16.         return sprintf(
  17.             '%08s%04s%04s%02s%02s%012s',
  18.             // time low
  19.             mb_substr($hex08),
  20.             // time mid
  21.             mb_substr($hex84),
  22.             // time high and version
  23.             str_pad(dechex($timeHi), 4'0', \STR_PAD_LEFT),
  24.             // clk_seq_hi_res
  25.             str_pad(dechex($clockSeqHi), 2'0', \STR_PAD_LEFT),
  26.             // clock_seq_low
  27.             mb_substr($hex182),
  28.             // node
  29.             mb_substr($hex2012)
  30.         );
  31.     }
  32.     public static function randomBytes(): string
  33.     {
  34.         return hex2bin(self::randomHex());
  35.     }
  36.     /**
  37.      * @throws InvalidUuidException
  38.      * @throws InvalidUuidLengthException
  39.      */
  40.     public static function fromBytesToHex(string $bytes): string
  41.     {
  42.         if (mb_strlen($bytes'8bit') !== 16) {
  43.             throw new InvalidUuidLengthException(mb_strlen($bytes'8bit'), bin2hex($bytes));
  44.         }
  45.         $uuid bin2hex($bytes);
  46.         if (!self::isValid($uuid)) {
  47.             throw new InvalidUuidException($uuid);
  48.         }
  49.         return $uuid;
  50.     }
  51.     public static function fromBytesToHexList(array $bytesList): array
  52.     {
  53.         $converted = [];
  54.         foreach ($bytesList as $key => $bytes) {
  55.             $converted[$key] = self::fromBytesToHex($bytes);
  56.         }
  57.         return $converted;
  58.     }
  59.     public static function fromHexToBytesList(array $uuids): array
  60.     {
  61.         $converted = [];
  62.         foreach ($uuids as $key => $uuid) {
  63.             $converted[$key] = self::fromHexToBytes($uuid);
  64.         }
  65.         return $converted;
  66.     }
  67.     /**
  68.      * @throws InvalidUuidException
  69.      */
  70.     public static function fromHexToBytes(string $uuid): string
  71.     {
  72.         if ($bin = @hex2bin($uuid)) {
  73.             return $bin;
  74.         }
  75.         throw new InvalidUuidException($uuid);
  76.     }
  77.     /**
  78.      * Generates a md5 binary, to hash the string and returns a UUID in hex
  79.      */
  80.     public static function fromStringToHex(string $string): string
  81.     {
  82.         return self::fromBytesToHex(md5($stringtrue));
  83.     }
  84.     public static function isValid(string $id): bool
  85.     {
  86.         if (!preg_match('/' self::VALID_PATTERN '/'$id)) {
  87.             return false;
  88.         }
  89.         return true;
  90.     }
  91.     private static function applyVersion(string $timeHiint $version): int
  92.     {
  93.         $timeHi hexdec($timeHi) & 0x0fff;
  94.         $timeHi &= ~0xf000;
  95.         $timeHi |= $version << 12;
  96.         return $timeHi;
  97.     }
  98.     private static function applyVariant(int $clockSeqHi): int
  99.     {
  100.         // Set the variant to RFC 4122
  101.         $clockSeqHi &= 0x3f;
  102.         $clockSeqHi &= ~0xc0;
  103.         $clockSeqHi |= 0x80;
  104.         return $clockSeqHi;
  105.     }
  106. }