Sequence.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Factories;
  3. use Countable;
  4. class Sequence implements Countable
  5. {
  6. /**
  7. * The sequence of return values.
  8. *
  9. * @var array
  10. */
  11. protected $sequence;
  12. /**
  13. * The count of the sequence items.
  14. *
  15. * @var int
  16. */
  17. public $count;
  18. /**
  19. * The current index of the sequence iteration.
  20. *
  21. * @var int
  22. */
  23. public $index = 0;
  24. /**
  25. * Create a new sequence instance.
  26. *
  27. * @param mixed ...$sequence
  28. * @return void
  29. */
  30. public function __construct(...$sequence)
  31. {
  32. $this->sequence = $sequence;
  33. $this->count = count($sequence);
  34. }
  35. /**
  36. * Get the current count of the sequence items.
  37. *
  38. * @return int
  39. */
  40. public function count(): int
  41. {
  42. return $this->count;
  43. }
  44. /**
  45. * Get the next value in the sequence.
  46. *
  47. * @return mixed
  48. */
  49. public function __invoke()
  50. {
  51. return tap(value($this->sequence[$this->index % $this->count], $this), function () {
  52. $this->index = $this->index + 1;
  53. });
  54. }
  55. }