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