-
-
Notifications
You must be signed in to change notification settings - Fork 503
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 #43 from aztech-forks/comb
Add generation of COMB UUID's
- Loading branch information
Showing
4 changed files
with
159 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Rhumsaa\Uuid\Generator; | ||
|
||
use Rhumsaa\Uuid\RandomGeneratorInterface; | ||
use Rhumsaa\Uuid\Converter\NumberConverterInterface; | ||
|
||
/** | ||
* Generator to be used for COMB sequential UUID's. | ||
* | ||
* @author thibaud | ||
* | ||
*/ | ||
class CombGenerator implements RandomGeneratorInterface | ||
{ | ||
|
||
private $randomGenerator; | ||
|
||
private $converter; | ||
|
||
private $timestampBytes; | ||
|
||
/** | ||
* @param RandomGeneratorInterface $generator RNG for the non-time part. | ||
* @param NumberConverterInterface $numberConverter Instance of number converter. | ||
*/ | ||
public function __construct(RandomGeneratorInterface $generator, NumberConverterInterface $numberConverter) | ||
{ | ||
$this->converter = $numberConverter; | ||
$this->randomGenerator = $generator; | ||
$this->timestampBytes = 6; | ||
} | ||
|
||
/** | ||
* (non-PHPdoc) @see \Rhumsaa\Uuid\RandomGeneratorInterface::generate() | ||
*/ | ||
public function generate($length) | ||
{ | ||
if ($length < $this->timestampBytes || $length < 0) { | ||
throw new \InvalidArgumentException('Length must be a positive integer.'); | ||
} | ||
|
||
$hash = ''; | ||
|
||
if ($this->timestampBytes > 0 && $length > $this->timestampBytes) { | ||
$hash = $this->randomGenerator->generate($length - $this->timestampBytes); | ||
} | ||
|
||
$lsbTime = str_pad($this->converter->toHex($this->timestamp()), $this->timestampBytes * 2, '0', STR_PAD_LEFT); | ||
|
||
if ($this->timestampBytes > 0 && strlen($lsbTime) > $this->timestampBytes * 2) { | ||
$lsbTime = substr($lsbTime, 0 - ($this->timestampBytes * 2)); | ||
} | ||
|
||
return hex2bin(str_pad(bin2hex($hash), $length - $this->timestampBytes, '0')) . hex2bin($lsbTime); | ||
} | ||
|
||
/** | ||
* Returns current timestamp as integer, precise to 0.00001 seconds | ||
* @return number | ||
*/ | ||
private function timestamp() | ||
{ | ||
$time = explode(' ', microtime(false)); | ||
|
||
return $time[1] . substr($time[0], 2, 5); | ||
} | ||
} |
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
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