annotate vendor/sabre/vobject/lib/Recur/RDateIterator.php @ 7:430dbd5346f7

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