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