Sonyflake.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of the godruoyi/php-snowflake.
  4. *
  5. * (c) Godruoyi <g@godruoyi.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled.
  8. */
  9. namespace Godruoyi\Snowflake;
  10. class Sonyflake extends Snowflake
  11. {
  12. const MAX_TIMESTAMP_LENGTH = 39;
  13. const MAX_MACHINEID_LENGTH = 16;
  14. const MAX_SEQUENCE_LENGTH = 8;
  15. /**
  16. * The machine ID.
  17. *
  18. * @var int
  19. */
  20. protected $machineid;
  21. /**
  22. * Build Sonyflake Instance.
  23. *
  24. * @param int $machineid machine ID 0 ~ 65535 (2^16)-1
  25. */
  26. public function __construct(int $machineid = 0)
  27. {
  28. $maxMachineID = -1 ^ (-1 << self::MAX_MACHINEID_LENGTH);
  29. $this->machineid = $machineid;
  30. if ($this->machineid < 0 || $this->machineid > $maxMachineID) {
  31. throw new \InvalidArgumentException("Invalid machine ID, must be between 0 ~ {$maxMachineID}.");
  32. }
  33. }
  34. /**
  35. * Get Sonyflake id.
  36. *
  37. * @return string
  38. */
  39. public function id()
  40. {
  41. $elapsedTime = $this->elapsedTime();
  42. while (($sequence = $this->callResolver($elapsedTime)) > (-1 ^ (-1 << self::MAX_SEQUENCE_LENGTH))) {
  43. $elapsedTime2 = $this->elapsedTime();
  44. // Get next timestamp
  45. while ($elapsedTime2 == $elapsedTime) {
  46. usleep(1);
  47. $elapsedTime2 = $this->elapsedTime();
  48. }
  49. $elapsedTime = $elapsedTime2;
  50. }
  51. $machineidLeftMoveLength = self::MAX_SEQUENCE_LENGTH;
  52. $timestampLeftMoveLength = self::MAX_MACHINEID_LENGTH + $machineidLeftMoveLength;
  53. if ($elapsedTime > (-1 ^ (-1 << self::MAX_TIMESTAMP_LENGTH))) {
  54. // The lifetime (174 years).
  55. throw new \Exception('Exceeding the maximum life cycle of the algorithm.');
  56. }
  57. return (string) ($elapsedTime << $timestampLeftMoveLength
  58. | ($this->machineid << $machineidLeftMoveLength)
  59. | ($sequence));
  60. }
  61. /**
  62. * Set start time (millisecond).
  63. */
  64. public function setStartTimeStamp(int $startTime)
  65. {
  66. $elapsedTime = floor(($this->getCurrentMicrotime() - $startTime) / 10) | 0;
  67. if ($elapsedTime < 0) {
  68. throw new \Exception('The start time cannot be greater than the current time');
  69. }
  70. $maxTimeDiff = -1 ^ (-1 << self::MAX_TIMESTAMP_LENGTH);
  71. if ($elapsedTime > $maxTimeDiff) {
  72. throw new \Exception('Exceeding the maximum life cycle of the algorithm');
  73. }
  74. $this->startTime = $startTime;
  75. return $this;
  76. }
  77. /**
  78. * Parse snowflake id.
  79. */
  80. public function parseId(string $id, $transform = false): array
  81. {
  82. $id = decbin($id);
  83. $length = self::MAX_SEQUENCE_LENGTH + self::MAX_MACHINEID_LENGTH;
  84. $data = [
  85. 'sequence' => substr($id, -1 * self::MAX_SEQUENCE_LENGTH),
  86. 'machineid' => substr($id, -1 * $length, self::MAX_MACHINEID_LENGTH),
  87. 'timestamp' => substr($id, 0, $length),
  88. ];
  89. return $transform ? array_map(function ($value) {
  90. return bindec($value);
  91. }, $data) : $data;
  92. }
  93. /**
  94. * The Elapsed Time.
  95. *
  96. * @return int
  97. */
  98. private function elapsedTime()
  99. {
  100. return floor(($this->getCurrentMicrotime() - $this->getStartTimeStamp()) / 10) | 0;
  101. }
  102. }