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

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