PluginProbe
Media Cloud Sync / 1.0.1
Media Cloud Sync v1.0.1
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / Generator / CombGenerator.php

CombGenerator.php in Media Cloud Sync 1.0.1, at includes/sdk/google/ramsey/uuid/src/Generator/CombGenerator.php

98 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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