RDateIterator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Sabre\VObject\Recur;
  3. use DateTimeInterface;
  4. use Iterator;
  5. use Sabre\VObject\DateTimeParser;
  6. /**
  7. * RRuleParser.
  8. *
  9. * This class receives an RRULE string, and allows you to iterate to get a list
  10. * of dates in that recurrence.
  11. *
  12. * For instance, passing: FREQ=DAILY;LIMIT=5 will cause the iterator to contain
  13. * 5 items, one for each day.
  14. *
  15. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  16. * @author Evert Pot (http://evertpot.com/)
  17. * @license http://sabre.io/license/ Modified BSD License
  18. */
  19. class RDateIterator implements Iterator
  20. {
  21. /**
  22. * Creates the Iterator.
  23. *
  24. * @param string|array $rrule
  25. */
  26. public function __construct($rrule, DateTimeInterface $start)
  27. {
  28. $this->startDate = $start;
  29. $this->parseRDate($rrule);
  30. $this->currentDate = clone $this->startDate;
  31. }
  32. /* Implementation of the Iterator interface {{{ */
  33. #[\ReturnTypeWillChange]
  34. public function current()
  35. {
  36. if (!$this->valid()) {
  37. return;
  38. }
  39. return clone $this->currentDate;
  40. }
  41. /**
  42. * Returns the current item number.
  43. *
  44. * @return int
  45. */
  46. #[\ReturnTypeWillChange]
  47. public function key()
  48. {
  49. return $this->counter;
  50. }
  51. /**
  52. * Returns whether the current item is a valid item for the recurrence
  53. * iterator.
  54. *
  55. * @return bool
  56. */
  57. #[\ReturnTypeWillChange]
  58. public function valid()
  59. {
  60. return $this->counter <= count($this->dates);
  61. }
  62. /**
  63. * Resets the iterator.
  64. *
  65. * @return void
  66. */
  67. #[\ReturnTypeWillChange]
  68. public function rewind()
  69. {
  70. $this->currentDate = clone $this->startDate;
  71. $this->counter = 0;
  72. }
  73. /**
  74. * Goes on to the next iteration.
  75. *
  76. * @return void
  77. */
  78. #[\ReturnTypeWillChange]
  79. public function next()
  80. {
  81. ++$this->counter;
  82. if (!$this->valid()) {
  83. return;
  84. }
  85. $this->currentDate =
  86. DateTimeParser::parse(
  87. $this->dates[$this->counter - 1],
  88. $this->startDate->getTimezone()
  89. );
  90. }
  91. /* End of Iterator implementation }}} */
  92. /**
  93. * Returns true if this recurring event never ends.
  94. *
  95. * @return bool
  96. */
  97. public function isInfinite()
  98. {
  99. return false;
  100. }
  101. /**
  102. * This method allows you to quickly go to the next occurrence after the
  103. * specified date.
  104. */
  105. public function fastForward(DateTimeInterface $dt)
  106. {
  107. while ($this->valid() && $this->currentDate < $dt) {
  108. $this->next();
  109. }
  110. }
  111. /**
  112. * The reference start date/time for the rrule.
  113. *
  114. * All calculations are based on this initial date.
  115. *
  116. * @var DateTimeInterface
  117. */
  118. protected $startDate;
  119. /**
  120. * The date of the current iteration. You can get this by calling
  121. * ->current().
  122. *
  123. * @var DateTimeInterface
  124. */
  125. protected $currentDate;
  126. /**
  127. * The current item in the list.
  128. *
  129. * You can get this number with the key() method.
  130. *
  131. * @var int
  132. */
  133. protected $counter = 0;
  134. /* }}} */
  135. /**
  136. * This method receives a string from an RRULE property, and populates this
  137. * class with all the values.
  138. *
  139. * @param string|array $rrule
  140. */
  141. protected function parseRDate($rdate)
  142. {
  143. if (is_string($rdate)) {
  144. $rdate = explode(',', $rdate);
  145. }
  146. $this->dates = $rdate;
  147. }
  148. /**
  149. * Array with the RRULE dates.
  150. *
  151. * @var array
  152. */
  153. protected $dates = [];
  154. }