UuidV2.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * This file is part of the ramsey/uuid library
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
  9. * @license http://opensource.org/licenses/MIT MIT
  10. */
  11. declare(strict_types=1);
  12. namespace Ramsey\Uuid\Rfc4122;
  13. use Ramsey\Uuid\Codec\CodecInterface;
  14. use Ramsey\Uuid\Converter\NumberConverterInterface;
  15. use Ramsey\Uuid\Converter\TimeConverterInterface;
  16. use Ramsey\Uuid\Exception\InvalidArgumentException;
  17. use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
  18. use Ramsey\Uuid\Type\Integer as IntegerObject;
  19. use Ramsey\Uuid\Uuid;
  20. use function hexdec;
  21. /**
  22. * DCE Security version, or version 2, UUIDs include local domain identifier,
  23. * local ID for the specified domain, and node values that are combined into a
  24. * 128-bit unsigned integer
  25. *
  26. * It is important to note that a version 2 UUID suffers from some loss of
  27. * fidelity of the timestamp, due to replacing the time_low field with the
  28. * local identifier. When constructing the timestamp value for date
  29. * purposes, we replace the local identifier bits with zeros. As a result,
  30. * the timestamp can be off by a range of 0 to 429.4967295 seconds (or 7
  31. * minutes, 9 seconds, and 496730 microseconds).
  32. *
  33. * Astute observers might note this value directly corresponds to 2^32 - 1,
  34. * or 0xffffffff. The local identifier is 32-bits, and we have set each of
  35. * these bits to 0, so the maximum range of timestamp drift is 0x00000000
  36. * to 0xffffffff (counted in 100-nanosecond intervals).
  37. *
  38. * @link https://publications.opengroup.org/c311 DCE 1.1: Authentication and Security Services
  39. * @link https://publications.opengroup.org/c706 DCE 1.1: Remote Procedure Call
  40. * @link https://pubs.opengroup.org/onlinepubs/9696989899/chap5.htm#tagcjh_08_02_01_01 DCE 1.1: Auth & Sec, §5.2.1.1
  41. * @link https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 DCE 1.1: Auth & Sec, §11.5.1.1
  42. * @link https://pubs.opengroup.org/onlinepubs/9629399/apdxa.htm DCE 1.1: RPC, Appendix A
  43. * @link https://github.com/google/uuid Go package for UUIDs (includes DCE implementation)
  44. *
  45. * @psalm-immutable
  46. */
  47. final class UuidV2 extends Uuid implements UuidInterface
  48. {
  49. use TimeTrait;
  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. parent::__construct($fields, $numberConverter, $codec, $timeConverter);
  74. }
  75. /**
  76. * Returns the local domain used to create this version 2 UUID
  77. */
  78. public function getLocalDomain(): int
  79. {
  80. /** @var Rfc4122FieldsInterface $fields */
  81. $fields = $this->getFields();
  82. return (int) hexdec($fields->getClockSeqLow()->toString());
  83. }
  84. /**
  85. * Returns the string name of the local domain
  86. */
  87. public function getLocalDomainName(): string
  88. {
  89. return Uuid::DCE_DOMAIN_NAMES[$this->getLocalDomain()];
  90. }
  91. /**
  92. * Returns the local identifier for the domain used to create this version 2 UUID
  93. */
  94. public function getLocalIdentifier(): IntegerObject
  95. {
  96. /** @var Rfc4122FieldsInterface $fields */
  97. $fields = $this->getFields();
  98. return new IntegerObject(
  99. $this->numberConverter->fromHex($fields->getTimeLow()->toString())
  100. );
  101. }
  102. }