Meta.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Snowflake;
  12. class Meta
  13. {
  14. /**
  15. * @var int [0, 31]
  16. */
  17. protected int $dataCenterId;
  18. /**
  19. * @var int [0, 31]
  20. */
  21. protected int $workerId;
  22. /**
  23. * @var int [0, 4095]
  24. */
  25. protected int $sequence;
  26. /**
  27. * @var int seconds or milliseconds
  28. */
  29. protected int $timestamp = 0;
  30. /**
  31. * @var int seconds or milliseconds
  32. */
  33. protected int $beginTimestamp = 0;
  34. public function __construct(int $dataCenterId, int $workerId, int $sequence, int $timestamp, int $beginTimestamp = 1560960000)
  35. {
  36. $this->dataCenterId = $dataCenterId;
  37. $this->workerId = $workerId;
  38. $this->sequence = $sequence;
  39. $this->timestamp = $timestamp;
  40. $this->beginTimestamp = $beginTimestamp;
  41. }
  42. public function getTimeInterval(): int
  43. {
  44. return $this->timestamp - $this->beginTimestamp;
  45. }
  46. public function getDataCenterId(): int
  47. {
  48. return $this->dataCenterId;
  49. }
  50. public function getWorkerId(): int
  51. {
  52. return $this->workerId;
  53. }
  54. public function getSequence(): int
  55. {
  56. return $this->sequence;
  57. }
  58. public function setDataCenterId(int $dataCenterId): self
  59. {
  60. $this->dataCenterId = $dataCenterId;
  61. return $this;
  62. }
  63. public function setWorkerId(int $workerId): self
  64. {
  65. $this->workerId = $workerId;
  66. return $this;
  67. }
  68. public function setSequence(int $sequence): self
  69. {
  70. $this->sequence = $sequence;
  71. return $this;
  72. }
  73. public function getTimestamp(): int
  74. {
  75. return $this->timestamp;
  76. }
  77. public function getBeginTimestamp(): int
  78. {
  79. return $this->beginTimestamp;
  80. }
  81. }