Adding static fromInteger method to create UUIDs from string integer or BigNumber, along with tests.

This commit is contained in:
Terry Corley
2014-07-14 18:44:11 -05:00
parent 1aa26b0143
commit 7fa0c32f8b
2 changed files with 56 additions and 0 deletions
+27
View File
@@ -894,6 +894,33 @@ final class Uuid
return new self($fields);
}
/**
* Creates a UUID from either the UUID as a 128-bit integer string or a Moontoast\Math\BigNumber object.
*
* @param string|\Moontoast\Math\BigNumber $integer String/BigNumber representation of UUID integer
* @throws Exception\UnsatisfiedDependencyException If Moontoast\Math\BigNumber is not present
* @return \Rhumsaa\Uuid\Uuid
*/
public static function fromInteger($integer)
{
if (!self::hasBigNumber()) {
throw new Exception\UnsatisfiedDependencyException(
'Cannot call ' . __METHOD__ . ' without support for large '
. 'integers, since integer is an unsigned '
. '128-bit integer; Moontoast\Math\BigNumber is required. '
);
}
if (!$integer instanceof \Moontoast\Math\BigNumber) {
$integer = new \Moontoast\Math\BigNumber($integer);
}
$hex = \Moontoast\Math\BigNumber::baseConvert($integer, 10, 16);
$hex = str_pad($hex, 32, '0', STR_PAD_LEFT);
return self::fromString($hex);
}
/**
* Check if a string is a valid uuid
*