-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from geekwright/uuidextend
Convert UUID to/from 16 byte binary strings
- Loading branch information
Showing
2 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,14 @@ | |
* @category Xmf\Uuid | ||
* @package Xmf | ||
* @author Richard Griffith <[email protected]> | ||
* @copyright 2017 XOOPS Project (https://xoops.org) | ||
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) | ||
* @link https://xoops.org | ||
* @copyright 2017-2019 XOOPS Project (https://xoops.org) | ||
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) | ||
*/ | ||
class Uuid | ||
{ | ||
// match spec for version 4 UUID as per rfc4122 | ||
protected const UUID_REGEX = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/'; | ||
|
||
/** | ||
* generate - generate a version 4 (random) UUID | ||
* | ||
|
@@ -41,4 +43,48 @@ public static function generate() | |
|
||
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); | ||
} | ||
|
||
/** | ||
* Pack a UUID into a binary string | ||
* | ||
* @param string $uuid a valid UUID | ||
* | ||
* @return string packed UUID as a binary string | ||
* | ||
* @throws \InvalidArgumentException | ||
* @throws \UnexpectedValueException | ||
*/ | ||
public static function packAsBinary($uuid) | ||
{ | ||
if (!preg_match(static::UUID_REGEX, $uuid)) { | ||
throw new \InvalidArgumentException('Invalid UUID'); | ||
} | ||
$return = pack("H*", str_replace('-', '', $uuid)); | ||
if (false === $return) { | ||
throw new \UnexpectedValueException('Packing UUID Failed'); | ||
} | ||
return $return; | ||
} | ||
|
||
/** | ||
* Unpack a UUID stored as a binary string | ||
* | ||
* @param string $packedUuid a packed UUID as returned by packAsBinary() | ||
* | ||
* @return string unpacked UUID | ||
* | ||
* @throws \InvalidArgumentException | ||
* @throws \UnexpectedValueException | ||
*/ | ||
public static function unpackBinary($packedUuid) | ||
{ | ||
if (16 !== strlen($packedUuid)) { | ||
throw new \InvalidArgumentException('Invalid packed UUID'); | ||
} | ||
$return = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($packedUuid), 4)); | ||
if (!preg_match(static::UUID_REGEX, $return)) { | ||
throw new \UnexpectedValueException('Unpacking UUID Failed'); | ||
} | ||
return $return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters