RandomSequenceResolver.php 829 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 RandomSequenceResolver implements SequenceResolver
  11. {
  12. /**
  13. * The las ttimestamp.
  14. *
  15. * @var null
  16. */
  17. protected $lastTimeStamp = -1;
  18. /**
  19. * The sequence.
  20. *
  21. * @var int
  22. */
  23. protected $sequence = 0;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function sequence(int $currentTime)
  28. {
  29. if ($this->lastTimeStamp === $currentTime) {
  30. ++$this->sequence;
  31. $this->lastTimeStamp = $currentTime;
  32. return $this->sequence;
  33. }
  34. $this->sequence = 0;
  35. $this->lastTimeStamp = $currentTime;
  36. return 0;
  37. }
  38. }