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