UuidV1.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 DateTimeImmutable;
  14. use DateTimeInterface;
  15. use Ramsey\Uuid\Codec\CodecInterface;
  16. use Ramsey\Uuid\Converter\NumberConverterInterface;
  17. use Ramsey\Uuid\Converter\TimeConverterInterface;
  18. use Ramsey\Uuid\Exception\DateTimeException;
  19. use Ramsey\Uuid\Exception\InvalidArgumentException;
  20. use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
  21. use Ramsey\Uuid\Uuid;
  22. use Throwable;
  23. use function str_pad;
  24. use const STR_PAD_LEFT;
  25. /**
  26. * Time-based, or version 1, UUIDs include timestamp, clock sequence, and node
  27. * values that are combined into a 128-bit unsigned integer
  28. *
  29. * @psalm-immutable
  30. */
  31. final class UuidV1 extends Uuid implements UuidInterface
  32. {
  33. /**
  34. * Creates a version 1 (time-based) UUID
  35. *
  36. * @param Rfc4122FieldsInterface $fields The fields from which to construct a UUID
  37. * @param NumberConverterInterface $numberConverter The number converter to use
  38. * for converting hex values to/from integers
  39. * @param CodecInterface $codec The codec to use when encoding or decoding
  40. * UUID strings
  41. * @param TimeConverterInterface $timeConverter The time converter to use
  42. * for converting timestamps extracted from a UUID to unix timestamps
  43. */
  44. public function __construct(
  45. Rfc4122FieldsInterface $fields,
  46. NumberConverterInterface $numberConverter,
  47. CodecInterface $codec,
  48. TimeConverterInterface $timeConverter
  49. ) {
  50. if ($fields->getVersion() !== Uuid::UUID_TYPE_TIME) {
  51. throw new InvalidArgumentException(
  52. 'Fields used to create a UuidV1 must represent a '
  53. . 'version 1 (time-based) UUID'
  54. );
  55. }
  56. parent::__construct($fields, $numberConverter, $codec, $timeConverter);
  57. }
  58. /**
  59. * Returns a DateTimeInterface object representing the timestamp associated
  60. * with the UUID
  61. *
  62. * The timestamp value is only meaningful in a time-based UUID, which
  63. * has version type 1.
  64. *
  65. * @return DateTimeImmutable A PHP DateTimeImmutable instance representing
  66. * the timestamp of a version 1 UUID
  67. */
  68. public function getDateTime(): DateTimeInterface
  69. {
  70. $time = $this->timeConverter->convertTime($this->fields->getTimestamp());
  71. try {
  72. return new DateTimeImmutable(
  73. '@'
  74. . $time->getSeconds()->toString()
  75. . '.'
  76. . str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)
  77. );
  78. } catch (Throwable $e) {
  79. throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e);
  80. }
  81. }
  82. }