PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.21
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.21
1.10.25 1.10.24 1.10.23 1.10.22 1.10.21 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 All 169 releases
woocommerce-pos / vendor / ramsey / uuid / src / Nonstandard / UuidV6.php

UuidV6.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.21, at vendor/ramsey/uuid/src/Nonstandard/UuidV6.php

134 lines 4.1 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\Nonstandard;
16
17 use DateTimeImmutable;
18 use DateTimeInterface;
19 use Ramsey\Uuid\Codec\CodecInterface;
20 use Ramsey\Uuid\Converter\NumberConverterInterface;
21 use Ramsey\Uuid\Converter\TimeConverterInterface;
22 use Ramsey\Uuid\Exception\DateTimeException;
23 use Ramsey\Uuid\Exception\InvalidArgumentException;
24 use Ramsey\Uuid\Lazy\LazyUuidFromString;
25 use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
26 use Ramsey\Uuid\Rfc4122\UuidInterface;
27 use Ramsey\Uuid\Rfc4122\UuidV1;
28 use Ramsey\Uuid\Uuid;
29 use Throwable;
30
31 use function hex2bin;
32 use function str_pad;
33 use function substr;
34
35 use const STR_PAD_LEFT;
36
37 /**
38 * Ordered-time, or version 6, UUIDs include timestamp, clock sequence, and node
39 * values that are combined into a 128-bit unsigned integer
40 *
41 * @link https://github.com/uuid6/uuid6-ietf-draft UUID version 6 IETF draft
42 * @link http://gh.peabody.io/uuidv6/ "Version 6" UUIDs
43 *
44 * @psalm-immutable
45 */
46 final class UuidV6 extends Uuid implements UuidInterface
47 {
48 /**
49 * Creates a version 6 (time-based) UUID
50 *
51 * @param Rfc4122FieldsInterface $fields The fields from which to construct a UUID
52 * @param NumberConverterInterface $numberConverter The number converter to use
53 * for converting hex values to/from integers
54 * @param CodecInterface $codec The codec to use when encoding or decoding
55 * UUID strings
56 * @param TimeConverterInterface $timeConverter The time converter to use
57 * for converting timestamps extracted from a UUID to unix timestamps
58 */
59 public function __construct(
60 Rfc4122FieldsInterface $fields,
61 NumberConverterInterface $numberConverter,
62 CodecInterface $codec,
63 TimeConverterInterface $timeConverter
64 ) {
65 if ($fields->getVersion() !== Uuid::UUID_TYPE_PEABODY) {
66 throw new InvalidArgumentException(
67 'Fields used to create a UuidV6 must represent a '
68 . 'version 6 (ordered-time) UUID'
69 );
70 }
71
72 parent::__construct($fields, $numberConverter, $codec, $timeConverter);
73 }
74
75 /**
76 * Returns a DateTimeInterface object representing the timestamp associated
77 * with the UUID
78 *
79 * @return DateTimeImmutable A PHP DateTimeImmutable instance representing
80 * the timestamp of a version 6 UUID
81 */
82 public function getDateTime(): DateTimeInterface
83 {
84 $time = $this->timeConverter->convertTime($this->fields->getTimestamp());
85
86 try {
87 return new DateTimeImmutable(
88 '@'
89 . $time->getSeconds()->toString()
90 . '.'
91 . str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)
92 );
93 } catch (Throwable $e) {
94 throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e);
95 }
96 }
97
98 /**
99 * Converts this UUID into an instance of a version 1 UUID
100 */
101 public function toUuidV1(): UuidV1
102 {
103 $hex = $this->getHex()->toString();
104 $hex = substr($hex, 7, 5)
105 . substr($hex, 13, 3)
106 . substr($hex, 3, 4)
107 . '1' . substr($hex, 0, 3)
108 . substr($hex, 16);
109
110 /** @var LazyUuidFromString $uuid */
111 $uuid = Uuid::fromBytes((string) hex2bin($hex));
112
113 return $uuid->toUuidV1();
114 }
115
116 /**
117 * Converts a version 1 UUID into an instance of a version 6 UUID
118 */
119 public static function fromUuidV1(UuidV1 $uuidV1): UuidV6
120 {
121 $hex = $uuidV1->getHex()->toString();
122 $hex = substr($hex, 13, 3)
123 . substr($hex, 8, 4)
124 . substr($hex, 0, 5)
125 . '6' . substr($hex, 5, 3)
126 . substr($hex, 16);
127
128 /** @var LazyUuidFromString $uuid */
129 $uuid = Uuid::fromBytes((string) hex2bin($hex));
130
131 return $uuid->toUuidV6();
132 }
133 }
134