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

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