1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
<?php
namespace Sabre\VObject\Recur;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
use Sabre\VObject\Component;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\Settings;
/**
* This class is used to determine new for a recurring event, when the next
* events occur.
*
* This iterator may loop infinitely in the future, therefore it is important
* that if you use this class, you set hard limits for the amount of iterations
* you want to handle.
*
* Note that currently there is not full support for the entire iCalendar
* specification, as it's very complex and contains a lot of permutations
* that's not yet used very often in software.
*
* For the focus has been on features as they actually appear in Calendaring
* software, but this may well get expanded as needed / on demand
*
* The following RRULE properties are supported
* * UNTIL
* * INTERVAL
* * COUNT
* * FREQ=DAILY
* * BYDAY
* * BYHOUR
* * BYMONTH
* * FREQ=WEEKLY
* * BYDAY
* * BYHOUR
* * WKST
* * FREQ=MONTHLY
* * BYMONTHDAY
* * BYDAY
* * BYSETPOS
* * FREQ=YEARLY
* * BYMONTH
* * BYYEARDAY
* * BYWEEKNO
* * BYMONTHDAY (only if BYMONTH is also set)
* * BYDAY (only if BYMONTH is also set)
*
* Anything beyond this is 'undefined', which means that it may get ignored, or
* you may get unexpected results. The effect is that in some applications the
* specified recurrence may look incorrect, or is missing.
*
* The recurrence iterator also does not yet support THISANDFUTURE.
*
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class EventIterator implements \Iterator {
/**
* Reference timeZone for floating dates and times.
*
* @var DateTimeZone
*/
protected $timeZone;
/**
* True if we're iterating an all-day event.
*
* @var bool
*/
protected $allDay = false;
/**
* Creates the iterator.
*
* There's three ways to set up the iterator.
*
* 1. You can pass a VCALENDAR component and a UID.
* 2. You can pass an array of VEVENTs (all UIDS should match).
* 3. You can pass a single VEVENT component.
*
* Only the second method is recomended. The other 1 and 3 will be removed
* at some point in the future.
*
* The $uid parameter is only required for the first method.
*
* @param Component|array $input
* @param string|null $uid
* @param DateTimeZone $timeZone Reference timezone for floating dates and
* times.
*/
function __construct($input, $uid = null, DateTimeZone $timeZone = null) {
if (is_null($timeZone)) {
$timeZone = new DateTimeZone('UTC');
}
$this->timeZone = $timeZone;
if (is_array($input)) {
$events = $input;
} elseif ($input instanceof VEvent) {
// Single instance mode.
$events = [$input];
} else {
// Calendar + UID mode.
$uid = (string)$uid;
if (!$uid) {
throw new InvalidArgumentException('The UID argument is required when a VCALENDAR is passed to this constructor');
}
if (!isset($input->VEVENT)) {
throw new InvalidArgumentException('No events found in this calendar');
}
$events = $input->getByUID($uid);
}
foreach ($events as $vevent) {
if (!isset($vevent->{'RECURRENCE-ID'})) {
$this->masterEvent = $vevent;
} else {
$this->exceptions[
$vevent->{'RECURRENCE-ID'}->getDateTime($this->timeZone)->getTimeStamp()
] = true;
$this->overriddenEvents[] = $vevent;
}
}
if (!$this->masterEvent) {
// No base event was found. CalDAV does allow cases where only
// overridden instances are stored.
//
// In this particular case, we're just going to grab the first
// event and use that instead. This may not always give the
// desired result.
if (!count($this->overriddenEvents)) {
throw new InvalidArgumentException('This VCALENDAR did not have an event with UID: ' . $uid);
}
$this->masterEvent = array_shift($this->overriddenEvents);
}
$this->startDate = $this->masterEvent->DTSTART->getDateTime($this->timeZone);
$this->allDay = !$this->masterEvent->DTSTART->hasTime();
if (isset($this->masterEvent->EXDATE)) {
foreach ($this->masterEvent->EXDATE as $exDate) {
foreach ($exDate->getDateTimes($this->timeZone) as $dt) {
$this->exceptions[$dt->getTimeStamp()] = true;
}
}
}
if (isset($this->masterEvent->DTEND)) {
$this->eventDuration =
$this->masterEvent->DTEND->getDateTime($this->timeZone)->getTimeStamp() -
$this->startDate->getTimeStamp();
} elseif (isset($this->masterEvent->DURATION)) {
$duration = $this->masterEvent->DURATION->getDateInterval();
$end = clone $this->startDate;
$end = $end->add($duration);
$this->eventDuration = $end->getTimeStamp() - $this->startDate->getTimeStamp();
} elseif ($this->allDay) {
$this->eventDuration = 3600 * 24;
} else {
$this->eventDuration = 0;
}
if (isset($this->masterEvent->RDATE)) {
$this->recurIterator = new RDateIterator(
$this->masterEvent->RDATE->getParts(),
$this->startDate
);
} elseif (isset($this->masterEvent->RRULE)) {
$this->recurIterator = new RRuleIterator(
$this->masterEvent->RRULE->getParts(),
$this->startDate
);
} else {
$this->recurIterator = new RRuleIterator(
[
'FREQ' => 'DAILY',
'COUNT' => 1,
],
$this->startDate
);
}
$this->rewind();
if (!$this->valid()) {
throw new NoInstancesException('This recurrence rule does not generate any valid instances');
}
}
/**
* Returns the date for the current position of the iterator.
*
* @return DateTimeImmutable
*/
function current() {
if ($this->currentDate) {
return clone $this->currentDate;
}
}
/**
* This method returns the start date for the current iteration of the
* event.
*
* @return DateTimeImmutable
*/
function getDtStart() {
if ($this->currentDate) {
return clone $this->currentDate;
}
}
/**
* This method returns the end date for the current iteration of the
* event.
*
* @return DateTimeImmutable
*/
function getDtEnd() {
if (!$this->valid()) {
return;
}
$end = clone $this->currentDate;
return $end->modify('+' . $this->eventDuration . ' seconds');
}
/**
* Returns a VEVENT for the current iterations of the event.
*
* This VEVENT will have a recurrence id, and it's DTSTART and DTEND
* altered.
*
* @return VEvent
*/
function getEventObject() {
if ($this->currentOverriddenEvent) {
return $this->currentOverriddenEvent;
}
$event = clone $this->masterEvent;
// Ignoring the following block, because PHPUnit's code coverage
// ignores most of these lines, and this messes with our stats.
//
// @codeCoverageIgnoreStart
unset(
$event->RRULE,
$event->EXDATE,
$event->RDATE,
$event->EXRULE,
$event->{'RECURRENCE-ID'}
);
// @codeCoverageIgnoreEnd
$event->DTSTART->setDateTime($this->getDtStart(), $event->DTSTART->isFloating());
if (isset($event->DTEND)) {
$event->DTEND->setDateTime($this->getDtEnd(), $event->DTEND->isFloating());
}
$recurid = clone $event->DTSTART;
$recurid->name = 'RECURRENCE-ID';
$event->add($recurid);
return $event;
}
/**
* Returns the current position of the iterator.
*
* This is for us simply a 0-based index.
*
* @return int
*/
function key() {
// The counter is always 1 ahead.
return $this->counter - 1;
}
/**
* This is called after next, to see if the iterator is still at a valid
* position, or if it's at the end.
*
* @return bool
*/
function valid() {
if ($this->counter > Settings::$maxRecurrences && Settings::$maxRecurrences !== -1) {
throw new MaxInstancesExceededException('Recurring events are only allowed to generate ' . Settings::$maxRecurrences);
}
return !!$this->currentDate;
}
/**
* Sets the iterator back to the starting point.
*/
function rewind() {
$this->recurIterator->rewind();
// re-creating overridden event index.
$index = [];
foreach ($this->overriddenEvents as $key => $event) {
$stamp = $event->DTSTART->getDateTime($this->timeZone)->getTimeStamp();
$index[$stamp][] = $key;
}
krsort($index);
$this->counter = 0;
$this->overriddenEventsIndex = $index;
$this->currentOverriddenEvent = null;
$this->nextDate = null;
$this->currentDate = clone $this->startDate;
$this->next();
}
/**
* Advances the iterator with one step.
*
* @return void
*/
function next() {
$this->currentOverriddenEvent = null;
$this->counter++;
if ($this->nextDate) {
// We had a stored value.
$nextDate = $this->nextDate;
$this->nextDate = null;
} else {
// We need to ask rruleparser for the next date.
// We need to do this until we find a date that's not in the
// exception list.
do {
if (!$this->recurIterator->valid()) {
$nextDate = null;
break;
}
$nextDate = $this->recurIterator->current();
$this->recurIterator->next();
} while (isset($this->exceptions[$nextDate->getTimeStamp()]));
}
// $nextDate now contains what rrule thinks is the next one, but an
// overridden event may cut ahead.
if ($this->overriddenEventsIndex) {
$offsets = end($this->overriddenEventsIndex);
$timestamp = key($this->overriddenEventsIndex);
$offset = end($offsets);
if (!$nextDate || $timestamp < $nextDate->getTimeStamp()) {
// Overridden event comes first.
$this->currentOverriddenEvent = $this->overriddenEvents[$offset];
// Putting the rrule next date aside.
$this->nextDate = $nextDate;
$this->currentDate = $this->currentOverriddenEvent->DTSTART->getDateTime($this->timeZone);
// Ensuring that this item will only be used once.
array_pop($this->overriddenEventsIndex[$timestamp]);
if (!$this->overriddenEventsIndex[$timestamp]) {
array_pop($this->overriddenEventsIndex);
}
// Exit point!
return;
}
}
$this->currentDate = $nextDate;
}
/**
* Quickly jump to a date in the future.
*
* @param DateTimeInterface $dateTime
*/
function fastForward(DateTimeInterface $dateTime) {
while ($this->valid() && $this->getDtEnd() <= $dateTime) {
$this->next();
}
}
/**
* Returns true if this recurring event never ends.
*
* @return bool
*/
function isInfinite() {
return $this->recurIterator->isInfinite();
}
/**
* RRULE parser.
*
* @var RRuleIterator
*/
protected $recurIterator;
/**
* The duration, in seconds, of the master event.
*
* We use this to calculate the DTEND for subsequent events.
*/
protected $eventDuration;
/**
* A reference to the main (master) event.
*
* @var VEVENT
*/
protected $masterEvent;
/**
* List of overridden events.
*
* @var array
*/
protected $overriddenEvents = [];
/**
* Overridden event index.
*
* Key is timestamp, value is the list of indexes of the item in the $overriddenEvent
* property.
*
* @var array
*/
protected $overriddenEventsIndex;
/**
* A list of recurrence-id's that are either part of EXDATE, or are
* overridden.
*
* @var array
*/
protected $exceptions = [];
/**
* Internal event counter.
*
* @var int
*/
protected $counter;
/**
* The very start of the iteration process.
*
* @var DateTimeImmutable
*/
protected $startDate;
/**
* Where we are currently in the iteration process.
*
* @var DateTimeImmutable
*/
protected $currentDate;
/**
* The next date from the rrule parser.
*
* Sometimes we need to temporary store the next date, because an
* overridden event came before.
*
* @var DateTimeImmutable
*/
protected $nextDate;
/**
* The event that overwrites the current iteration
*
* @var VEVENT
*/
protected $currentOverriddenEvent;
}
|