Debian Codegraph @

vendor/ramsey/uuid/src/Nonstandard/UuidV6.php

raw
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 Ramsey\Uuid\Codec\CodecInterface;
18 use Ramsey\Uuid\Converter\NumberConverterInterface;
19 use Ramsey\Uuid\Converter\TimeConverterInterface;
20 use Ramsey\Uuid\Exception\InvalidArgumentException;
21 use Ramsey\Uuid\Lazy\LazyUuidFromString;
22 use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
23 use Ramsey\Uuid\Rfc4122\TimeTrait;
24 use Ramsey\Uuid\Rfc4122\UuidInterface;
25 use Ramsey\Uuid\Rfc4122\UuidV1;
26 use Ramsey\Uuid\Uuid as BaseUuid;
27
28 /**
29 * Reordered time, or version 6, UUIDs include timestamp, clock sequence, and node values that are combined into a
30 * 128-bit unsigned integer
31 *
32 * @deprecated Use {@see \Ramsey\Uuid\Rfc4122\UuidV6} instead.
33 *
34 * @link https://github.com/uuid6/uuid6-ietf-draft UUID version 6 IETF draft
35 * @link http://gh.peabody.io/uuidv6/ "Version 6" UUIDs
36 * @link https://www.rfc-editor.org/rfc/rfc9562#section-5.6 RFC 9562, 5.6. UUID Version 6
37 *
38 * @immutable
39 */
40 class UuidV6 extends BaseUuid implements UuidInterface
41 {
42 use TimeTrait;
43
44 /**
45 * Creates a version 6 (reordered Gregorian time) UUID
46 *
47 * @param Rfc4122FieldsInterface $fields The fields from which to construct a UUID
48 * @param NumberConverterInterface $numberConverter The number converter to use for converting hex values to/from integers
49 * @param CodecInterface $codec The codec to use when encoding or decoding UUID strings
50 * @param TimeConverterInterface $timeConverter The time converter to use for converting timestamps extracted from a
51 * UUID to unix timestamps
52 */
53 public function __construct(
54 Rfc4122FieldsInterface $fields,
55 NumberConverterInterface $numberConverter,
56 CodecInterface $codec,
57 TimeConverterInterface $timeConverter,
58 ) {
59 if ($fields->getVersion() !== BaseUuid::UUID_TYPE_REORDERED_TIME) {
60 throw new InvalidArgumentException(
61 'Fields used to create a UuidV6 must represent a version 6 (reordered time) UUID',
62 );
63 }
64
65 parent::__construct($fields, $numberConverter, $codec, $timeConverter);
66 }
67
68 /**
69 * Converts this UUID into an instance of a version 1 UUID
70 */
71 public function toUuidV1(): UuidV1
72 {
73 $hex = $this->getHex()->toString();
74 $hex = substr($hex, 7, 5)
75 . substr($hex, 13, 3)
76 . substr($hex, 3, 4)
77 . '1' . substr($hex, 0, 3)
78 . substr($hex, 16);
79
80 /** @var LazyUuidFromString $uuid */
81 $uuid = BaseUuid::fromBytes((string) hex2bin($hex));
82
83 return $uuid->toUuidV1();
84 }
85
86 /**
87 * Converts a version 1 UUID into an instance of a version 6 UUID
88 */
89 public static function fromUuidV1(UuidV1 $uuidV1): \Ramsey\Uuid\Rfc4122\UuidV6
90 {
91 $hex = $uuidV1->getHex()->toString();
92 $hex = substr($hex, 13, 3)
93 . substr($hex, 8, 4)
94 . substr($hex, 0, 5)
95 . '6' . substr($hex, 5, 3)
96 . substr($hex, 16);
97
98 /** @var LazyUuidFromString $uuid */
99 $uuid = BaseUuid::fromBytes((string) hex2bin($hex));
100
101 return $uuid->toUuidV6();
102 }
103 }