Final Class Yiisoft\Db\Helper\DbUuidHelper
| Inheritance | Yiisoft\Db\Helper\DbUuidHelper |
|---|
Public Methods
Method Details
| public static boolean isValidHexUuid ( string $uuidString ) | ||
| $uuidString | string | |
public static function isValidHexUuid(string $uuidString): bool
{
return (bool) preg_match('/^[0-9a-f]{32}$/i', $uuidString);
}
| public static boolean isValidUuid ( string $uuidString ) | ||
| $uuidString | string | |
public static function isValidUuid(string $uuidString): bool
{
return (bool) preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i', $uuidString);
}
| public static string toUuid ( string $blobString ) | ||
| $blobString | string | |
public static function toUuid(string $blobString): string
{
if (self::isValidUuid($blobString)) {
return $blobString;
}
if (strlen($blobString) === 16) {
$hex = bin2hex($blobString);
} elseif (strlen($blobString) === 32 && self::isValidHexUuid($blobString)) {
$hex = $blobString;
} else {
throw new InvalidArgumentException('Length of source data is should be 16 or 32 bytes.');
}
return
substr($hex, 0, 8) . '-'
. substr($hex, 8, 4) . '-'
. substr($hex, 12, 4) . '-'
. substr($hex, 16, 4) . '-'
. substr($hex, 20)
;
}
| public static string uuidToBlob ( string $uuidString ) | ||
| $uuidString | string | |
public static function uuidToBlob(string $uuidString): string
{
if (!self::isValidUuid($uuidString)) {
throw new InvalidArgumentException('Incorrect UUID.');
}
return (string) hex2bin(str_replace('-', '', $uuidString));
}
Signup or Login in order to comment.