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