diff options
author | Max Kostikov <max@kostikov.co> | 2020-12-04 14:33:34 +0100 |
---|---|---|
committer | Max Kostikov <max@kostikov.co> | 2020-12-04 14:33:34 +0100 |
commit | a9dd6d6bdbc27e11ffe71644670686d82e05420f (patch) | |
tree | 05dd57d1c3f2a8b7541598544b92dbbfe964c329 /vendor/ramsey/uuid/src/Generator/CombGenerator.php | |
parent | c9ce562369556ae01044dfb91541c4e4ee35af9e (diff) | |
parent | 9c7ec55b4049599f3a61192eaf020e3a112dfa0e (diff) | |
download | volse-hubzilla-a9dd6d6bdbc27e11ffe71644670686d82e05420f.tar.gz volse-hubzilla-a9dd6d6bdbc27e11ffe71644670686d82e05420f.tar.bz2 volse-hubzilla-a9dd6d6bdbc27e11ffe71644670686d82e05420f.zip |
Merge branch 'dev' into 'dev'
Dev sync
See merge request kostikov/core!2
Diffstat (limited to 'vendor/ramsey/uuid/src/Generator/CombGenerator.php')
-rw-r--r-- | vendor/ramsey/uuid/src/Generator/CombGenerator.php | 102 |
1 files changed, 69 insertions, 33 deletions
diff --git a/vendor/ramsey/uuid/src/Generator/CombGenerator.php b/vendor/ramsey/uuid/src/Generator/CombGenerator.php index 1d4a5f604..88ae6ea23 100644 --- a/vendor/ramsey/uuid/src/Generator/CombGenerator.php +++ b/vendor/ramsey/uuid/src/Generator/CombGenerator.php @@ -1,4 +1,5 @@ <?php + /** * This file is part of the ramsey/uuid library * @@ -7,27 +8,58 @@ * * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> * @license http://opensource.org/licenses/MIT MIT - * @link https://benramsey.com/projects/ramsey-uuid/ Documentation - * @link https://packagist.org/packages/ramsey/uuid Packagist - * @link https://github.com/ramsey/uuid GitHub */ +declare(strict_types=1); + namespace Ramsey\Uuid\Generator; -use Exception; -use InvalidArgumentException; use Ramsey\Uuid\Converter\NumberConverterInterface; -use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; +use Ramsey\Uuid\Exception\InvalidArgumentException; + +use function bin2hex; +use function explode; +use function hex2bin; +use function microtime; +use function str_pad; +use function substr; + +use const STR_PAD_LEFT; /** - * CombGenerator provides functionality to generate COMB (combined GUID/timestamp) - * sequential UUIDs + * CombGenerator generates COMBs (combined UUID/timestamp) + * + * The CombGenerator, when used with the StringCodec (and, by proxy, the + * TimestampLastCombCodec) or the TimestampFirstCombCodec, combines the current + * timestamp with a UUID (hence the name "COMB"). The timestamp either appears + * as the first or last 48 bits of the COMB, depending on the codec used. + * + * By default, COMBs will have the timestamp set as the last 48 bits of the + * identifier. + * + * ``` php + * $factory = new UuidFactory(); + * + * $factory->setRandomGenerator(new CombGenerator( + * $factory->getRandomGenerator(), + * $factory->getNumberConverter() + * )); + * + * $comb = $factory->uuid4(); + * ``` * - * @link https://en.wikipedia.org/wiki/Globally_unique_identifier#Sequential_algorithms + * To generate a COMB with the timestamp as the first 48 bits, set the + * TimestampFirstCombCodec as the codec. + * + * ``` php + * $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder())); + * ``` + * + * @link https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys */ class CombGenerator implements RandomGeneratorInterface { - const TIMESTAMP_BYTES = 6; + public const TIMESTAMP_BYTES = 6; /** * @var RandomGeneratorInterface @@ -39,50 +71,54 @@ class CombGenerator implements RandomGeneratorInterface */ private $converter; - /** - * Constructs a `CombGenerator` using a random-number generator and a number converter - * - * @param RandomGeneratorInterface $generator Random-number generator for the non-time part. - * @param NumberConverterInterface $numberConverter Instance of number converter. - */ - public function __construct(RandomGeneratorInterface $generator, NumberConverterInterface $numberConverter) - { + public function __construct( + RandomGeneratorInterface $generator, + NumberConverterInterface $numberConverter + ) { $this->converter = $numberConverter; $this->randomGenerator = $generator; } /** - * Generates a string of binary data of the specified length + * @throws InvalidArgumentException if $length is not a positive integer + * greater than or equal to CombGenerator::TIMESTAMP_BYTES * - * @param integer $length The number of bytes of random binary data to generate - * @return string A binary string - * @throws UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present - * @throws InvalidArgumentException if length is not a positive integer - * @throws Exception + * @inheritDoc */ - public function generate($length) + public function generate(int $length): string { if ($length < self::TIMESTAMP_BYTES || $length < 0) { - throw new InvalidArgumentException('Length must be a positive integer.'); + throw new InvalidArgumentException( + 'Length must be a positive integer greater than or equal to ' . self::TIMESTAMP_BYTES + ); } $hash = ''; - if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) { $hash = $this->randomGenerator->generate($length - self::TIMESTAMP_BYTES); } - $lsbTime = str_pad($this->converter->toHex($this->timestamp()), self::TIMESTAMP_BYTES * 2, '0', STR_PAD_LEFT); + $lsbTime = str_pad( + $this->converter->toHex($this->timestamp()), + self::TIMESTAMP_BYTES * 2, + '0', + STR_PAD_LEFT + ); - return hex2bin(str_pad(bin2hex($hash), $length - self::TIMESTAMP_BYTES, '0') . $lsbTime); + return (string) hex2bin( + str_pad( + bin2hex((string) $hash), + $length - self::TIMESTAMP_BYTES, + '0' + ) + . $lsbTime + ); } /** - * Returns current timestamp as integer, precise to 0.00001 seconds - * - * @return string + * Returns current timestamp a string integer, precise to 0.00001 seconds */ - private function timestamp() + private function timestamp(): string { $time = explode(' ', microtime(false)); |