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