| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the ramsey/uuid library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\Ramsey\Uuid\Generator; |
| 14 |
|
| 15 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Converter\NumberConverterInterface; |
| 16 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\InvalidArgumentException; |
| 17 |
use function bin2hex; |
| 18 |
use function explode; |
| 19 |
use function hex2bin; |
| 20 |
use function microtime; |
| 21 |
use function str_pad; |
| 22 |
use function substr; |
| 23 |
use const STR_PAD_LEFT; |
| 24 |
/** |
| 25 |
* CombGenerator generates COMBs (combined UUID/timestamp) |
| 26 |
* |
| 27 |
* The CombGenerator, when used with the StringCodec (and, by proxy, the |
| 28 |
* TimestampLastCombCodec) or the TimestampFirstCombCodec, combines the current |
| 29 |
* timestamp with a UUID (hence the name "COMB"). The timestamp either appears |
| 30 |
* as the first or last 48 bits of the COMB, depending on the codec used. |
| 31 |
* |
| 32 |
* By default, COMBs will have the timestamp set as the last 48 bits of the |
| 33 |
* identifier. |
| 34 |
* |
| 35 |
* ``` php |
| 36 |
* $factory = new UuidFactory(); |
| 37 |
* |
| 38 |
* $factory->setRandomGenerator(new CombGenerator( |
| 39 |
* $factory->getRandomGenerator(), |
| 40 |
* $factory->getNumberConverter() |
| 41 |
* )); |
| 42 |
* |
| 43 |
* $comb = $factory->uuid4(); |
| 44 |
* ``` |
| 45 |
* |
| 46 |
* To generate a COMB with the timestamp as the first 48 bits, set the |
| 47 |
* TimestampFirstCombCodec as the codec. |
| 48 |
* |
| 49 |
* ``` php |
| 50 |
* $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder())); |
| 51 |
* ``` |
| 52 |
* |
| 53 |
* @link https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys |
| 54 |
*/ |
| 55 |
class CombGenerator implements RandomGeneratorInterface |
| 56 |
{ |
| 57 |
public const TIMESTAMP_BYTES = 6; |
| 58 |
/** |
| 59 |
* @var RandomGeneratorInterface |
| 60 |
*/ |
| 61 |
private $randomGenerator; |
| 62 |
/** |
| 63 |
* @var NumberConverterInterface |
| 64 |
*/ |
| 65 |
private $converter; |
| 66 |
public function __construct(RandomGeneratorInterface $generator, NumberConverterInterface $numberConverter) |
| 67 |
{ |
| 68 |
$this->converter = $numberConverter; |
| 69 |
$this->randomGenerator = $generator; |
| 70 |
} |
| 71 |
/** |
| 72 |
* @throws InvalidArgumentException if $length is not a positive integer |
| 73 |
* greater than or equal to CombGenerator::TIMESTAMP_BYTES |
| 74 |
* |
| 75 |
* @inheritDoc |
| 76 |
*/ |
| 77 |
public function generate(int $length): string |
| 78 |
{ |
| 79 |
if ($length < self::TIMESTAMP_BYTES || $length < 0) { |
| 80 |
throw new InvalidArgumentException('Length must be a positive integer greater than or equal to ' . self::TIMESTAMP_BYTES); |
| 81 |
} |
| 82 |
$hash = ''; |
| 83 |
if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) { |
| 84 |
$hash = $this->randomGenerator->generate($length - self::TIMESTAMP_BYTES); |
| 85 |
} |
| 86 |
$lsbTime = str_pad($this->converter->toHex($this->timestamp()), self::TIMESTAMP_BYTES * 2, '0', STR_PAD_LEFT); |
| 87 |
return (string) hex2bin(str_pad(bin2hex($hash), $length - self::TIMESTAMP_BYTES, '0') . $lsbTime); |
| 88 |
} |
| 89 |
/** |
| 90 |
* Returns current timestamp a string integer, precise to 0.00001 seconds |
| 91 |
*/ |
| 92 |
private function timestamp(): string |
| 93 |
{ |
| 94 |
$time = explode(' ', microtime(\false)); |
| 95 |
return $time[1] . substr($time[0], 2, 5); |
| 96 |
} |
| 97 |
} |
| 98 |
|