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