PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor / ramsey / uuid / src / Generator / CombGenerator.php

CombGenerator.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor/ramsey/uuid/src/Generator/CombGenerator.php

128 lines 3.4 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 <[email protected]>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12
13 declare(strict_types=1);
14
15 namespace Ramsey\Uuid\Generator;
16
17 use Ramsey\Uuid\Converter\NumberConverterInterface;
18 use Ramsey\Uuid\Exception\InvalidArgumentException;
19
20 use function bin2hex;
21 use function explode;
22 use function hex2bin;
23 use function microtime;
24 use function str_pad;
25 use function substr;
26
27 use const STR_PAD_LEFT;
28
29 /**
30 * CombGenerator generates COMBs (combined UUID/timestamp)
31 *
32 * The CombGenerator, when used with the StringCodec (and, by proxy, the
33 * TimestampLastCombCodec) or the TimestampFirstCombCodec, combines the current
34 * timestamp with a UUID (hence the name "COMB"). The timestamp either appears
35 * as the first or last 48 bits of the COMB, depending on the codec used.
36 *
37 * By default, COMBs will have the timestamp set as the last 48 bits of the
38 * identifier.
39 *
40 * ``` php
41 * $factory = new UuidFactory();
42 *
43 * $factory->setRandomGenerator(new CombGenerator(
44 * $factory->getRandomGenerator(),
45 * $factory->getNumberConverter()
46 * ));
47 *
48 * $comb = $factory->uuid4();
49 * ```
50 *
51 * To generate a COMB with the timestamp as the first 48 bits, set the
52 * TimestampFirstCombCodec as the codec.
53 *
54 * ``` php
55 * $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder()));
56 * ```
57 *
58 * @link https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys
59 */
60 class CombGenerator implements RandomGeneratorInterface
61 {
62 public const TIMESTAMP_BYTES = 6;
63
64 /**
65 * @var RandomGeneratorInterface
66 */
67 private $randomGenerator;
68
69 /**
70 * @var NumberConverterInterface
71 */
72 private $converter;
73
74 public function __construct(
75 RandomGeneratorInterface $generator,
76 NumberConverterInterface $numberConverter
77 ) {
78 $this->converter = $numberConverter;
79 $this->randomGenerator = $generator;
80 }
81
82 /**
83 * @throws InvalidArgumentException if $length is not a positive integer
84 * greater than or equal to CombGenerator::TIMESTAMP_BYTES
85 *
86 * @inheritDoc
87 */
88 public function generate(int $length): string
89 {
90 if ($length < self::TIMESTAMP_BYTES || $length < 0) {
91 throw new InvalidArgumentException(
92 'Length must be a positive integer greater than or equal to ' . self::TIMESTAMP_BYTES
93 );
94 }
95
96 $hash = '';
97 if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) {
98 $hash = $this->randomGenerator->generate($length - self::TIMESTAMP_BYTES);
99 }
100
101 $lsbTime = str_pad(
102 $this->converter->toHex($this->timestamp()),
103 self::TIMESTAMP_BYTES * 2,
104 '0',
105 STR_PAD_LEFT
106 );
107
108 return (string) hex2bin(
109 str_pad(
110 bin2hex($hash),
111 $length - self::TIMESTAMP_BYTES,
112 '0'
113 )
114 . $lsbTime
115 );
116 }
117
118 /**
119 * Returns current timestamp a string integer, precise to 0.00001 seconds
120 */
121 private function timestamp(): string
122 {
123 $time = explode(' ', microtime(false));
124
125 return $time[1] . substr($time[0], 2, 5);
126 }
127 }
128