RDateIterator.php 3.3 KB

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