mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-15 16:07:55 +03:00
Abstract time generation into family of TimeGeneratorInterface classes
This commit is contained in:
+32
-44
@@ -20,6 +20,7 @@ use Ramsey\Uuid\Converter\TimeConverterInterface;
|
||||
use Ramsey\Uuid\Provider\NodeProviderInterface;
|
||||
use Ramsey\Uuid\Provider\TimeProviderInterface;
|
||||
use Ramsey\Uuid\Generator\RandomGeneratorInterface;
|
||||
use Ramsey\Uuid\Generator\TimeGeneratorInterface;
|
||||
use Ramsey\Uuid\Codec\CodecInterface;
|
||||
use Ramsey\Uuid\Builder\UuidBuilderInterface;
|
||||
|
||||
@@ -49,6 +50,11 @@ class UuidFactory implements UuidFactoryInterface
|
||||
*/
|
||||
private $randomGenerator = null;
|
||||
|
||||
/**
|
||||
* @var TimeGeneratorInterface
|
||||
*/
|
||||
private $timeGenerator = null;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TimeConverterInterface
|
||||
@@ -79,6 +85,7 @@ class UuidFactory implements UuidFactoryInterface
|
||||
$this->nodeProvider = $features->getNodeProvider();
|
||||
$this->numberConverter = $features->getNumberConverter();
|
||||
$this->randomGenerator = $features->getRandomGenerator();
|
||||
$this->timeGenerator = $features->getTimeGenerator();
|
||||
$this->timeConverter = $features->getTimeConverter();
|
||||
$this->timeProvider = $features->getTimeProvider();
|
||||
$this->uuidBuilder = $features->getBuilder();
|
||||
@@ -89,11 +96,21 @@ class UuidFactory implements UuidFactoryInterface
|
||||
return $this->codec;
|
||||
}
|
||||
|
||||
public function getNodeProvider()
|
||||
{
|
||||
return $this->nodeProvider;
|
||||
}
|
||||
|
||||
public function getRandomGenerator()
|
||||
{
|
||||
return $this->randomGenerator;
|
||||
}
|
||||
|
||||
public function getTimeGenerator()
|
||||
{
|
||||
return $this->timeGenerator;
|
||||
}
|
||||
|
||||
public function getNumberConverter()
|
||||
{
|
||||
return $this->numberConverter;
|
||||
@@ -109,6 +126,11 @@ class UuidFactory implements UuidFactoryInterface
|
||||
$this->timeConverter = $converter;
|
||||
}
|
||||
|
||||
public function getTimeProvider()
|
||||
{
|
||||
return $this->timeProvider;
|
||||
}
|
||||
|
||||
public function setTimeProvider(TimeProviderInterface $provider)
|
||||
{
|
||||
$this->timeProvider = $provider;
|
||||
@@ -119,6 +141,11 @@ class UuidFactory implements UuidFactoryInterface
|
||||
$this->randomGenerator = $generator;
|
||||
}
|
||||
|
||||
public function setTimeGenerator(TimeGeneratorInterface $generator)
|
||||
{
|
||||
$this->timeGenerator = $generator;
|
||||
}
|
||||
|
||||
public function setNodeProvider(NodeProviderInterface $provider)
|
||||
{
|
||||
$this->nodeProvider = $provider;
|
||||
@@ -184,31 +211,10 @@ class UuidFactory implements UuidFactoryInterface
|
||||
*/
|
||||
public function uuid1($node = null, $clockSeq = null)
|
||||
{
|
||||
$node = $this->getValidNode($node);
|
||||
$bytes = $this->timeGenerator->generate($this, $node, $clockSeq);
|
||||
$hex = bin2hex($bytes);
|
||||
|
||||
if ($clockSeq === null) {
|
||||
// Not using "stable storage"; see RFC 4122, Section 4.2.1.1
|
||||
$clockSeq = mt_rand(0, 1 << 14);
|
||||
}
|
||||
|
||||
// Create a 60-bit time value as a count of 100-nanosecond intervals
|
||||
// since 00:00:00.00, 15 October 1582
|
||||
$timeOfDay = $this->timeProvider->currentTime();
|
||||
$uuidTime = $this->timeConverter->calculateTime($timeOfDay['sec'], $timeOfDay['usec']);
|
||||
|
||||
$timeHi = $this->applyVersion($uuidTime['hi'], 1);
|
||||
$clockSeqHi = $this->applyVariant($clockSeq >> 8);
|
||||
|
||||
$fields = array(
|
||||
'time_low' => $uuidTime['low'],
|
||||
'time_mid' => $uuidTime['mid'],
|
||||
'time_hi_and_version' => sprintf('%04x', $timeHi),
|
||||
'clock_seq_hi_and_reserved' => sprintf('%02x', $clockSeqHi),
|
||||
'clock_seq_low' => sprintf('%02x', $clockSeq & 0xff),
|
||||
'node' => $node,
|
||||
);
|
||||
|
||||
return $this->uuid($fields);
|
||||
return $this->uuidFromHashedName($hex, 1);
|
||||
}
|
||||
|
||||
|
||||
@@ -260,7 +266,7 @@ class UuidFactory implements UuidFactoryInterface
|
||||
return $this->uuidBuilder->build($this->codec, $fields);
|
||||
}
|
||||
|
||||
protected function applyVariant($clockSeqHi)
|
||||
public function applyVariant($clockSeqHi)
|
||||
{
|
||||
// Set the variant to RFC 4122
|
||||
$clockSeqHi = $clockSeqHi & 0x3f;
|
||||
@@ -274,7 +280,7 @@ class UuidFactory implements UuidFactoryInterface
|
||||
* @param string $timeHi
|
||||
* @param integer $version
|
||||
*/
|
||||
protected function applyVersion($timeHi, $version)
|
||||
public function applyVersion($timeHi, $version)
|
||||
{
|
||||
$timeHi = hexdec($timeHi) & 0x0fff;
|
||||
$timeHi &= ~(0xf000);
|
||||
@@ -323,22 +329,4 @@ class UuidFactory implements UuidFactoryInterface
|
||||
|
||||
return $this->uuid($fields);
|
||||
}
|
||||
|
||||
protected function getValidNode($node)
|
||||
{
|
||||
if ($node === null) {
|
||||
$node = $this->nodeProvider->getNode();
|
||||
}
|
||||
|
||||
// Convert the node to hex, if it is still an integer
|
||||
if (is_int($node)) {
|
||||
$node = sprintf('%012x', $node);
|
||||
}
|
||||
|
||||
if (! ctype_xdigit($node) || strlen($node) > 12) {
|
||||
throw new \InvalidArgumentException('Invalid node value');
|
||||
}
|
||||
|
||||
return strtolower(sprintf('%012s', $node));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user