| 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 <[email protected]> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator; |
| 14 |
|
| 15 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\NumberConverterInterface; |
| 16 |
use Dudlewebs\WPMCS\GCP\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 TimestampLastCombCodec) or the |
| 28 |
* TimestampFirstCombCodec, combines the current timestamp with a UUID (hence the name "COMB"). The timestamp either |
| 29 |
* appears as the first or last 48 bits of the COMB, depending on the codec used. |
| 30 |
* |
| 31 |
* By default, COMBs will have the timestamp set as the last 48 bits of the identifier. |
| 32 |
* |
| 33 |
* ``` |
| 34 |
* $factory = new UuidFactory(); |
| 35 |
* |
| 36 |
* $factory->setRandomGenerator(new CombGenerator( |
| 37 |
* $factory->getRandomGenerator(), |
| 38 |
* $factory->getNumberConverter(), |
| 39 |
* )); |
| 40 |
* |
| 41 |
* $comb = $factory->uuid4(); |
| 42 |
* ``` |
| 43 |
* |
| 44 |
* To generate a COMB with the timestamp as the first 48 bits, set the TimestampFirstCombCodec as the codec. |
| 45 |
* |
| 46 |
* ``` |
| 47 |
* $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder())); |
| 48 |
* ``` |
| 49 |
* |
| 50 |
* @deprecated Please migrate to {@link https://uuid.ramsey.dev/en/stable/rfc4122/version7.html Version 7, Unix Epoch Time UUIDs}. |
| 51 |
* |
| 52 |
* @link https://web.archive.org/web/20240118030355/https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys |
| 53 |
*/ |
| 54 |
class CombGenerator implements RandomGeneratorInterface |
| 55 |
{ |
| 56 |
public const TIMESTAMP_BYTES = 6; |
| 57 |
public function __construct(private RandomGeneratorInterface $generator, private NumberConverterInterface $numberConverter) |
| 58 |
{ |
| 59 |
} |
| 60 |
/** |
| 61 |
* @throws InvalidArgumentException if $length is not a positive integer greater than or equal to CombGenerator::TIMESTAMP_BYTES |
| 62 |
* |
| 63 |
* @inheritDoc |
| 64 |
*/ |
| 65 |
public function generate(int $length) : string |
| 66 |
{ |
| 67 |
if ($length < self::TIMESTAMP_BYTES) { |
| 68 |
throw new InvalidArgumentException('Length must be a positive integer greater than or equal to ' . self::TIMESTAMP_BYTES); |
| 69 |
} |
| 70 |
if ($length % 2 !== 0) { |
| 71 |
throw new InvalidArgumentException('Length must be an even number'); |
| 72 |
} |
| 73 |
$hash = ''; |
| 74 |
/** @phpstan-ignore greater.alwaysTrue (TIMESTAMP_BYTES constant could change in child classes) */ |
| 75 |
if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) { |
| 76 |
$hash = $this->generator->generate($length - self::TIMESTAMP_BYTES); |
| 77 |
} |
| 78 |
$lsbTime = str_pad($this->numberConverter->toHex($this->timestamp()), self::TIMESTAMP_BYTES * 2, '0', STR_PAD_LEFT); |
| 79 |
return (string) hex2bin(str_pad(bin2hex($hash), $length - self::TIMESTAMP_BYTES, '0') . $lsbTime); |
| 80 |
} |
| 81 |
/** |
| 82 |
* Returns the current timestamp as a string integer, precise to 0.00001 seconds |
| 83 |
*/ |
| 84 |
private function timestamp() : string |
| 85 |
{ |
| 86 |
$time = explode(' ', microtime(\false)); |
| 87 |
return $time[1] . substr($time[0], 2, 5); |
| 88 |
} |
| 89 |
} |
| 90 |
|