Debian Codegraph @

vendor/ramsey/uuid/src/Rfc4122/UuidV2.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\Rfc4122;
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\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
22 use Ramsey\Uuid\Type\Integer as IntegerObject;
23 use Ramsey\Uuid\Uuid;
24
25 use function hexdec;
26
27 /**
28 * DCE Security version, or version 2, UUIDs include local domain identifier, local ID for the specified domain, and
29 * node values that are combined into a 128-bit unsigned integer
30 *
31 * It is important to note that a version 2 UUID suffers from some loss of timestamp fidelity, due to replacing the
32 * time_low field with the local identifier. When constructing the timestamp value for date purposes, we replace the
33 * local identifier bits with zeros. As a result, the timestamp can be off by a range of 0 to 429.4967295 seconds (or 7
34 * minutes, 9 seconds, and 496,730 microseconds).
35 *
36 * Astute observers might note this value directly corresponds to `2^32-1`, or `0xffffffff`. The local identifier is
37 * 32-bits, and we have set each of these bits to `0`, so the maximum range of timestamp drift is `0x00000000` to
38 * `0xffffffff` (counted in 100-nanosecond intervals).
39 *
40 * @link https://www.rfc-editor.org/rfc/rfc9562#section-5.2 RFC 9562, 5.2. UUID Version 2
41 * @link https://publications.opengroup.org/c311 DCE 1.1: Authentication and Security Services
42 * @link https://publications.opengroup.org/c706 DCE 1.1: Remote Procedure Call
43 * @link https://pubs.opengroup.org/onlinepubs/9696989899/chap5.htm#tagcjh_08_02_01_01 DCE 1.1: Auth & Sec, §5.2.1.1
44 * @link https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 DCE 1.1: Auth & Sec, §11.5.1.1
45 * @link https://pubs.opengroup.org/onlinepubs/9629399/apdxa.htm DCE 1.1: RPC, Appendix A
46 * @link https://github.com/google/uuid Go package for UUIDs (includes DCE implementation)
47 *
48 * @immutable
49 */
50 final class UuidV2 extends Uuid implements UuidInterface
51 {
52 use TimeTrait;
53
54 /**
55 * Creates a version 2 (DCE Security) UUID
56 *
57 * @param Rfc4122FieldsInterface $fields The fields from which to construct a UUID
58 * @param NumberConverterInterface $numberConverter The number converter to use for converting hex values to/from integers
59 * @param CodecInterface $codec The codec to use when encoding or decoding UUID strings
60 * @param TimeConverterInterface $timeConverter The time converter to use for converting timestamps extracted from a
61 * UUID to unix timestamps
62 */
63 public function __construct(
64 Rfc4122FieldsInterface $fields,
65 NumberConverterInterface $numberConverter,
66 CodecInterface $codec,
67 TimeConverterInterface $timeConverter,
68 ) {
69 if ($fields->getVersion() !== Uuid::UUID_TYPE_DCE_SECURITY) {
70 throw new InvalidArgumentException(
71 'Fields used to create a UuidV2 must represent a version 2 (DCE Security) UUID'
72 );
73 }
74
75 parent::__construct($fields, $numberConverter, $codec, $timeConverter);
76 }
77
78 /**
79 * Returns the local domain used to create this version 2 UUID
80 */
81 public function getLocalDomain(): int
82 {
83 /** @var Rfc4122FieldsInterface $fields */
84 $fields = $this->getFields();
85
86 return (int) hexdec($fields->getClockSeqLow()->toString());
87 }
88
89 /**
90 * Returns the string name of the local domain
91 */
92 public function getLocalDomainName(): string
93 {
94 return Uuid::DCE_DOMAIN_NAMES[$this->getLocalDomain()];
95 }
96
97 /**
98 * Returns the local identifier for the domain used to create this version 2 UUID
99 */
100 public function getLocalIdentifier(): IntegerObject
101 {
102 /** @var Rfc4122FieldsInterface $fields */
103 $fields = $this->getFields();
104
105 return new IntegerObject($this->numberConverter->fromHex($fields->getTimeLow()->toString()));
106 }
107 }