暫無描述

DegradedUuid.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * @link https://benramsey.com/projects/ramsey-uuid/ Documentation
  11. * @link https://packagist.org/packages/ramsey/uuid Packagist
  12. * @link https://github.com/ramsey/uuid GitHub
  13. */
  14. namespace Ramsey\Uuid;
  15. use DateTime;
  16. use Moontoast\Math\BigNumber;
  17. use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
  18. use Ramsey\Uuid\Exception\UnsupportedOperationException;
  19. /**
  20. * DegradedUuid represents an RFC 4122 UUID on 32-bit systems
  21. *
  22. * @see Uuid
  23. */
  24. class DegradedUuid extends Uuid
  25. {
  26. /**
  27. * @inheritdoc
  28. */
  29. public function getDateTime()
  30. {
  31. if ($this->getVersion() != 1) {
  32. throw new UnsupportedOperationException('Not a time-based UUID');
  33. }
  34. $time = $this->converter->fromHex($this->getTimestampHex());
  35. $ts = new BigNumber($time, 20);
  36. $ts->subtract('122192928000000000');
  37. $ts->divide('10000000.0');
  38. $ts->floor();
  39. $unixTime = $ts->getValue();
  40. return new DateTime("@{$unixTime}");
  41. }
  42. /**
  43. * For degraded UUIDs, throws an `UnsatisfiedDependencyException` when
  44. * called on a 32-bit system
  45. *
  46. * @throws UnsatisfiedDependencyException if called on a 32-bit system
  47. */
  48. public function getFields()
  49. {
  50. throw new UnsatisfiedDependencyException(
  51. 'Cannot call ' . __METHOD__ . ' on a 32-bit system, since some '
  52. . 'values overflow the system max integer value'
  53. . '; consider calling getFieldsHex instead'
  54. );
  55. }
  56. /**
  57. * For degraded UUIDs, throws an `UnsatisfiedDependencyException` when
  58. * called on a 32-bit system
  59. *
  60. * @throws UnsatisfiedDependencyException if called on a 32-bit system
  61. */
  62. public function getNode()
  63. {
  64. throw new UnsatisfiedDependencyException(
  65. 'Cannot call ' . __METHOD__ . ' on a 32-bit system, since node '
  66. . 'is an unsigned 48-bit integer and can overflow the system '
  67. . 'max integer value'
  68. . '; consider calling getNodeHex instead'
  69. );
  70. }
  71. /**
  72. * For degraded UUIDs, throws an `UnsatisfiedDependencyException` when
  73. * called on a 32-bit system
  74. *
  75. * @throws UnsatisfiedDependencyException if called on a 32-bit system
  76. */
  77. public function getTimeLow()
  78. {
  79. throw new UnsatisfiedDependencyException(
  80. 'Cannot call ' . __METHOD__ . ' on a 32-bit system, since time_low '
  81. . 'is an unsigned 32-bit integer and can overflow the system '
  82. . 'max integer value'
  83. . '; consider calling getTimeLowHex instead'
  84. );
  85. }
  86. /**
  87. * For degraded UUIDs, throws an `UnsatisfiedDependencyException` when
  88. * called on a 32-bit system
  89. *
  90. * @throws UnsatisfiedDependencyException if called on a 32-bit system
  91. * @throws UnsupportedOperationException If this UUID is not a version 1 UUID
  92. */
  93. public function getTimestamp()
  94. {
  95. if ($this->getVersion() != 1) {
  96. throw new UnsupportedOperationException('Not a time-based UUID');
  97. }
  98. throw new UnsatisfiedDependencyException(
  99. 'Cannot call ' . __METHOD__ . ' on a 32-bit system, since timestamp '
  100. . 'is an unsigned 60-bit integer and can overflow the system '
  101. . 'max integer value'
  102. . '; consider calling getTimestampHex instead'
  103. );
  104. }
  105. }