| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the ramsey/uuid library |
| 4 |
* |
| 5 |
* For the full copyright and license information, please view the LICENSE |
| 6 |
* file that was distributed with this source code. |
| 7 |
* |
| 8 |
* @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 9 |
* @license http://opensource.org/licenses/MIT MIT |
| 10 |
* @link https://benramsey.com/projects/ramsey-uuid/ Documentation |
| 11 |
* @link https://packagist.org/packages/ramsey/uuid Packagist |
| 12 |
* @link https://github.com/ramsey/uuid GitHub |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace Ramsey\Uuid\Generator; |
| 16 |
|
| 17 |
use Exception; |
| 18 |
use InvalidArgumentException; |
| 19 |
use Ramsey\Uuid\Converter\NumberConverterInterface; |
| 20 |
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; |
| 21 |
|
| 22 |
/** |
| 23 |
* CombGenerator provides functionality to generate COMB (combined GUID/timestamp) |
| 24 |
* sequential UUIDs |
| 25 |
* |
| 26 |
* @link https://en.wikipedia.org/wiki/Globally_unique_identifier#Sequential_algorithms |
| 27 |
*/ |
| 28 |
class CombGenerator implements RandomGeneratorInterface |
| 29 |
{ |
| 30 |
const TIMESTAMP_BYTES = 6; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var RandomGeneratorInterface |
| 34 |
*/ |
| 35 |
private $randomGenerator; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var NumberConverterInterface |
| 39 |
*/ |
| 40 |
private $converter; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructs a `CombGenerator` using a random-number generator and a number converter |
| 44 |
* |
| 45 |
* @param RandomGeneratorInterface $generator Random-number generator for the non-time part. |
| 46 |
* @param NumberConverterInterface $numberConverter Instance of number converter. |
| 47 |
*/ |
| 48 |
public function __construct(RandomGeneratorInterface $generator, NumberConverterInterface $numberConverter) |
| 49 |
{ |
| 50 |
$this->converter = $numberConverter; |
| 51 |
$this->randomGenerator = $generator; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Generates a string of binary data of the specified length |
| 56 |
* |
| 57 |
* @param integer $length The number of bytes of random binary data to generate |
| 58 |
* @return string A binary string |
| 59 |
* @throws UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present |
| 60 |
* @throws InvalidArgumentException if length is not a positive integer |
| 61 |
* @throws Exception |
| 62 |
*/ |
| 63 |
public function generate($length) |
| 64 |
{ |
| 65 |
if ($length < self::TIMESTAMP_BYTES || $length < 0) { |
| 66 |
throw new InvalidArgumentException('Length must be a positive integer.'); |
| 67 |
} |
| 68 |
|
| 69 |
$hash = ''; |
| 70 |
|
| 71 |
if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) { |
| 72 |
$hash = $this->randomGenerator->generate($length - self::TIMESTAMP_BYTES); |
| 73 |
} |
| 74 |
|
| 75 |
$lsbTime = str_pad($this->converter->toHex($this->timestamp()), self::TIMESTAMP_BYTES * 2, '0', STR_PAD_LEFT); |
| 76 |
|
| 77 |
return hex2bin(str_pad(bin2hex($hash), $length - self::TIMESTAMP_BYTES, '0') . $lsbTime); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns current timestamp as integer, precise to 0.00001 seconds |
| 82 |
* |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
private function timestamp() |
| 86 |
{ |
| 87 |
$time = explode(' ', microtime(false)); |
| 88 |
|
| 89 |
return $time[1] . substr($time[0], 2, 5); |
| 90 |
} |
| 91 |
} |