EventReader.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV;
  8. use DateTime;
  9. use DateTimeInterface;
  10. use DateTimeZone;
  11. use InvalidArgumentException;
  12. use Sabre\VObject\Component\VCalendar;
  13. use Sabre\VObject\Component\VEvent;
  14. use Sabre\VObject\Reader;
  15. class EventReader {
  16. protected VEvent $baseEvent;
  17. protected DateTimeInterface $baseEventStartDate;
  18. protected DateTimeZone $baseEventStartTimeZone;
  19. protected DateTimeInterface $baseEventEndDate;
  20. protected DateTimeZone $baseEventEndTimeZone;
  21. protected bool $baseEventStartDateFloating = false;
  22. protected bool $baseEventEndDateFloating = false;
  23. protected int $baseEventDuration;
  24. protected ?EventReaderRRule $rruleIterator = null;
  25. protected ?EventReaderRDate $rdateIterator = null;
  26. protected ?EventReaderRRule $eruleIterator = null;
  27. protected ?EventReaderRDate $edateIterator = null;
  28. protected array $recurrenceModified;
  29. protected ?DateTimeInterface $recurrenceCurrentDate;
  30. protected array $dayNamesMap = [
  31. 'MO' => 'Monday', 'TU' => 'Tuesday', 'WE' => 'Wednesday', 'TH' => 'Thursday', 'FR' => 'Friday', 'SA' => 'Saturday', 'SU' => 'Sunday'
  32. ];
  33. protected array $monthNamesMap = [
  34. 1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June',
  35. 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December'
  36. ];
  37. protected array $relativePositionNamesMap = [
  38. 1 => 'First', 2 => 'Second', 3 => 'Third', 4 => 'Fourth', 5 => 'Fifty',
  39. -1 => 'Last', -2 => 'Second Last', -3 => 'Third Last', -4 => 'Fourth Last', -5 => 'Fifty Last'
  40. ];
  41. /**
  42. * Initilizes the Event Reader
  43. *
  44. * There is several ways to set up the iterator.
  45. *
  46. * 1. You can pass a VCALENDAR component (as object or string) and a UID.
  47. * 2. You can pass an array of VEVENTs (all UIDS should match).
  48. * 3. You can pass a single VEVENT component (as object or string).
  49. *
  50. * Only the second method is recommended. The other 1 and 3 will be removed
  51. * at some point in the future.
  52. *
  53. * The $uid parameter is only required for the first method.
  54. *
  55. * @since 30.0.0
  56. *
  57. * @param VCalendar|VEvent|Array|String $input
  58. * @param string|null $uid
  59. * @param DateTimeZone|null $timeZone reference timezone for floating dates and times
  60. */
  61. public function __construct(VCalendar|VEvent|array|string $input, ?string $uid = null, ?DateTimeZone $timeZone = null) {
  62. // evaluate if the input is a string and convert it to and vobject if required
  63. if (is_string($input)) {
  64. $input = Reader::read($input);
  65. }
  66. // evaluate if input is a single event vobject and convert it to a collection
  67. if ($input instanceof VEvent) {
  68. $events = [$input];
  69. }
  70. // evaluate if input is a calendar vobject
  71. elseif ($input instanceof VCalendar) {
  72. // Calendar + UID mode.
  73. if ($uid === null) {
  74. throw new InvalidArgumentException('The UID argument is required when a VCALENDAR object is used');
  75. }
  76. // extract events from calendar
  77. $events = $input->getByUID($uid);
  78. // evaluate if any event where found
  79. if (count($events) === 0) {
  80. throw new InvalidArgumentException('This VCALENDAR did not have an event with UID: '.$uid);
  81. }
  82. // extract calendar timezone
  83. if (isset($input->VTIMEZONE) && isset($input->VTIMEZONE->TZID)) {
  84. $calendarTimeZone = new DateTimeZone($input->VTIMEZONE->TZID->getValue());
  85. }
  86. }
  87. // evaluate if input is a collection of event vobjects
  88. elseif (is_array($input)) {
  89. $events = $input;
  90. } else {
  91. throw new InvalidArgumentException('Invalid input data type');
  92. }
  93. // find base event instance and remove it from events collection
  94. foreach ($events as $key => $vevent) {
  95. if (!isset($vevent->{'RECURRENCE-ID'})) {
  96. $this->baseEvent = $vevent;
  97. unset($events[$key]);
  98. }
  99. }
  100. // No base event was found. CalDAV does allow cases where only
  101. // overridden instances are stored.
  102. //
  103. // In this particular case, we're just going to grab the first
  104. // event and use that instead. This may not always give the
  105. // desired result.
  106. if (!isset($this->baseEvent) && count($events) > 0) {
  107. $this->baseEvent = array_shift($events);
  108. }
  109. // determain the event starting time zone
  110. // we require this to align all other dates times
  111. // evaluate if timezone paramater was used (treat this as a override)
  112. if ($timeZone !== null) {
  113. $this->baseEventStartTimeZone = $timeZone;
  114. }
  115. // evaluate if event start date has a timezone parameter
  116. elseif (isset($this->baseEvent->DTSTART->parameters['TZID'])) {
  117. $this->baseEventStartTimeZone = new DateTimeZone($this->baseEvent->DTSTART->parameters['TZID']->getValue());
  118. }
  119. // evaluate if event parent calendar has a time zone
  120. elseif (isset($calendarTimeZone)) {
  121. $this->baseEventStartTimeZone = clone $calendarTimeZone;
  122. }
  123. // otherwise, as a last resort use the UTC timezone
  124. else {
  125. $this->baseEventStartTimeZone = new DateTimeZone('UTC');
  126. }
  127. // determain the event end time zone
  128. // we require this to align all other dates and times
  129. // evaluate if timezone paramater was used (treat this as a override)
  130. if ($timeZone !== null) {
  131. $this->baseEventEndTimeZone = $timeZone;
  132. }
  133. // evaluate if event end date has a timezone parameter
  134. elseif (isset($this->baseEvent->DTEND->parameters['TZID'])) {
  135. $this->baseEventEndTimeZone = new DateTimeZone($this->baseEvent->DTEND->parameters['TZID']->getValue());
  136. }
  137. // evaluate if event parent calendar has a time zone
  138. elseif (isset($calendarTimeZone)) {
  139. $this->baseEventEndTimeZone = clone $calendarTimeZone;
  140. }
  141. // otherwise, as a last resort use the start date time zone
  142. else {
  143. $this->baseEventEndTimeZone = clone $this->baseEventStartTimeZone;
  144. }
  145. // extract start date and time
  146. $this->baseEventStartDate = $this->baseEvent->DTSTART->getDateTime($this->baseEventStartTimeZone);
  147. $this->baseEventStartDateFloating = $this->baseEvent->DTSTART->isFloating();
  148. // determine event end date and duration
  149. // evaluate if end date exists
  150. // extract end date and calculate duration
  151. if (isset($this->baseEvent->DTEND)) {
  152. $this->baseEventEndDate = $this->baseEvent->DTEND->getDateTime($this->baseEventEndTimeZone);
  153. $this->baseEventEndDateFloating = $this->baseEvent->DTEND->isFloating();
  154. $this->baseEventDuration =
  155. $this->baseEvent->DTEND->getDateTime($this->baseEventEndTimeZone)->getTimeStamp() -
  156. $this->baseEventStartDate->getTimeStamp();
  157. }
  158. // evaluate if duration exists
  159. // extract duration and calculate end date
  160. elseif (isset($this->baseEvent->DURATION)) {
  161. $this->baseEventDuration = $this->baseEvent->DURATION->getDateInterval();
  162. $this->baseEventEndDate = ((clone $this->baseEventStartDate)->add($this->baseEventDuration));
  163. }
  164. // evaluate if start date is floating
  165. // set duration to 24 hours and calculate the end date
  166. // according to the rfc any event without a end date or duration is a complete day
  167. elseif ($this->baseEventStartDateFloating == true) {
  168. $this->baseEventDuration = 86400;
  169. $this->baseEventEndDate = ((clone $this->baseEventStartDate)->add($this->baseEventDuration));
  170. }
  171. // otherwise, set duration to zero this should never happen
  172. else {
  173. $this->baseEventDuration = 0;
  174. $this->baseEventEndDate = $this->baseEventStartDate;
  175. }
  176. // evaluate if RRULE exist and construct iterator
  177. if (isset($this->baseEvent->RRULE)) {
  178. $this->rruleIterator = new EventReaderRRule(
  179. $this->baseEvent->RRULE->getParts(),
  180. $this->baseEventStartDate
  181. );
  182. }
  183. // evaluate if RDATE exist and construct iterator
  184. if (isset($this->baseEvent->RDATE)) {
  185. $this->rdateIterator = new EventReaderRDate(
  186. $this->baseEvent->RDATE->getValue(),
  187. $this->baseEventStartDate
  188. );
  189. }
  190. // evaluate if EXRULE exist and construct iterator
  191. if (isset($this->baseEvent->EXRULE)) {
  192. $this->eruleIterator = new EventReaderRRule(
  193. $this->baseEvent->EXRULE->getParts(),
  194. $this->baseEventStartDate
  195. );
  196. }
  197. // evaluate if EXDATE exist and construct iterator
  198. if (isset($this->baseEvent->EXDATE)) {
  199. $this->edateIterator = new EventReaderRDate(
  200. $this->baseEvent->EXDATE->getValue(),
  201. $this->baseEventStartDate
  202. );
  203. }
  204. // construct collection of modified events with recurrence id as hash
  205. foreach ($events as $vevent) {
  206. $this->recurrenceModified[$vevent->{'RECURRENCE-ID'}->getDateTime($this->baseEventStartTimeZone)->getTimeStamp()] = $vevent;
  207. }
  208. $this->recurrenceCurrentDate = clone $this->baseEventStartDate;
  209. }
  210. /**
  211. * retrieve date and time of event start
  212. *
  213. * @since 30.0.0
  214. *
  215. * @return DateTime
  216. */
  217. public function startDateTime(): DateTime {
  218. return DateTime::createFromInterface($this->baseEventStartDate);
  219. }
  220. /**
  221. * retrieve time zone of event start
  222. *
  223. * @since 30.0.0
  224. *
  225. * @return DateTimeZone
  226. */
  227. public function startTimeZone(): DateTimeZone {
  228. return $this->baseEventStartTimeZone;
  229. }
  230. /**
  231. * retrieve date and time of event end
  232. *
  233. * @since 30.0.0
  234. *
  235. * @return DateTime
  236. */
  237. public function endDateTime(): DateTime {
  238. return DateTime::createFromInterface($this->baseEventEndDate);
  239. }
  240. /**
  241. * retrieve time zone of event end
  242. *
  243. * @since 30.0.0
  244. *
  245. * @return DateTimeZone
  246. */
  247. public function endTimeZone(): DateTimeZone {
  248. return $this->baseEventEndTimeZone;
  249. }
  250. /**
  251. * is this an all day event
  252. *
  253. * @since 30.0.0
  254. *
  255. * @return bool
  256. */
  257. public function entireDay(): bool {
  258. return $this->baseEventStartDateFloating;
  259. }
  260. /**
  261. * is this a recurring event
  262. *
  263. * @since 30.0.0
  264. *
  265. * @return bool
  266. */
  267. public function recurs(): bool {
  268. return ($this->rruleIterator !== null || $this->rdateIterator !== null);
  269. }
  270. /**
  271. * event recurrence pattern
  272. *
  273. * @since 30.0.0
  274. *
  275. * @return string|null R - Relative or A - Absolute
  276. */
  277. public function recurringPattern(): string | null {
  278. if ($this->rruleIterator === null && $this->rdateIterator === null) {
  279. return null;
  280. }
  281. if ($this->rruleIterator?->isRelative()) {
  282. return 'R';
  283. }
  284. return 'A';
  285. }
  286. /**
  287. * event recurrence precision
  288. *
  289. * @since 30.0.0
  290. *
  291. * @return string|null daily, weekly, monthly, yearly, fixed
  292. */
  293. public function recurringPrecision(): string | null {
  294. if ($this->rruleIterator !== null) {
  295. return $this->rruleIterator->precision();
  296. }
  297. if ($this->rdateIterator !== null) {
  298. return 'fixed';
  299. }
  300. return null;
  301. }
  302. /**
  303. * event recurrence interval
  304. *
  305. * @since 30.0.0
  306. *
  307. * @return int|null
  308. */
  309. public function recurringInterval(): int | null {
  310. return $this->rruleIterator?->interval();
  311. }
  312. /**
  313. * event recurrence conclusion
  314. *
  315. * returns true if RRULE with UNTIL or COUNT (calculated) is used
  316. * returns true RDATE is used
  317. * returns false if RRULE or RDATE are absent, or RRRULE is infinite
  318. *
  319. * @since 30.0.0
  320. *
  321. * @return bool
  322. */
  323. public function recurringConcludes(): bool {
  324. // retrieve rrule conclusions
  325. if ($this->rruleIterator?->concludesOn() !== null ||
  326. $this->rruleIterator?->concludesAfter() !== null) {
  327. return true;
  328. }
  329. // retrieve rdate conclusions
  330. if ($this->rdateIterator?->concludesAfter() !== null) {
  331. return true;
  332. }
  333. return false;
  334. }
  335. /**
  336. * event recurrence conclusion iterations
  337. *
  338. * returns the COUNT value if RRULE is used
  339. * returns the collection count if RDATE is used
  340. * returns combined count of RRULE COUNT and RDATE if both are used
  341. * returns null if RRULE and RDATE are absent
  342. *
  343. * @since 30.0.0
  344. *
  345. * @return int|null
  346. */
  347. public function recurringConcludesAfter(): int | null {
  348. // construct count place holder
  349. $count = 0;
  350. // retrieve and add RRULE iterations count
  351. $count += (int) $this->rruleIterator?->concludesAfter();
  352. // retrieve and add RDATE iterations count
  353. $count += (int) $this->rdateIterator?->concludesAfter();
  354. // return count
  355. return !empty($count) ? $count : null;
  356. }
  357. /**
  358. * event recurrence conclusion date
  359. *
  360. * returns the last date of UNTIL or COUNT (calculated) if RRULE is used
  361. * returns the last date in the collection if RDATE is used
  362. * returns the highest date if both RRULE and RDATE are used
  363. * returns null if RRULE and RDATE are absent or RRULE is infinite
  364. *
  365. * @since 30.0.0
  366. *
  367. * @return DateTime|null
  368. */
  369. public function recurringConcludesOn(): DateTime | null {
  370. if ($this->rruleIterator !== null) {
  371. // retrieve rrule conclusion date
  372. $rrule = $this->rruleIterator->concludes();
  373. // evaluate if rrule conclusion is null
  374. // if this is null that means the recurrence is infinate
  375. if ($rrule === null) {
  376. return null;
  377. }
  378. }
  379. // retrieve rdate conclusion date
  380. if ($this->rdateIterator !== null) {
  381. $rdate = $this->rdateIterator->concludes();
  382. }
  383. // evaluate if both rrule and rdate have date
  384. if (isset($rdate) && isset($rrule)) {
  385. // return the highest date
  386. return (($rdate > $rrule) ? $rdate : $rrule);
  387. } elseif (isset($rrule)) {
  388. return $rrule;
  389. } elseif (isset($rdate)) {
  390. return $rdate;
  391. }
  392. return null;
  393. }
  394. /**
  395. * event recurrence days of the week
  396. *
  397. * returns collection of RRULE BYDAY day(s) ['MO','WE','FR']
  398. * returns blank collection if RRULE is absent, RDATE presents or absents has no affect
  399. *
  400. * @since 30.0.0
  401. *
  402. * @return array
  403. */
  404. public function recurringDaysOfWeek(): array {
  405. // evaluate if RRULE exists and return day(s) of the week
  406. return $this->rruleIterator !== null ? $this->rruleIterator->daysOfWeek() : [];
  407. }
  408. /**
  409. * event recurrence days of the week (named)
  410. *
  411. * returns collection of RRULE BYDAY day(s) ['Monday','Wednesday','Friday']
  412. * returns blank collection if RRULE is absent, RDATE presents or absents has no affect
  413. *
  414. * @since 30.0.0
  415. *
  416. * @return array
  417. */
  418. public function recurringDaysOfWeekNamed(): array {
  419. // evaluate if RRULE exists and extract day(s) of the week
  420. $days = $this->rruleIterator !== null ? $this->rruleIterator->daysOfWeek() : [];
  421. // convert numberic month to month name
  422. foreach ($days as $key => $value) {
  423. $days[$key] = $this->dayNamesMap[$value];
  424. }
  425. // return names collection
  426. return $days;
  427. }
  428. /**
  429. * event recurrence days of the month
  430. *
  431. * returns collection of RRULE BYMONTHDAY day(s) [7, 15, 31]
  432. * returns blank collection if RRULE is absent, RDATE presents or absents has no affect
  433. *
  434. * @since 30.0.0
  435. *
  436. * @return array
  437. */
  438. public function recurringDaysOfMonth(): array {
  439. // evaluate if RRULE exists and return day(s) of the month
  440. return $this->rruleIterator !== null ? $this->rruleIterator->daysOfMonth() : [];
  441. }
  442. /**
  443. * event recurrence days of the year
  444. *
  445. * returns collection of RRULE BYYEARDAY day(s) [57, 205, 365]
  446. * returns blank collection if RRULE is absent, RDATE presents or absents has no affect
  447. *
  448. * @since 30.0.0
  449. *
  450. * @return array
  451. */
  452. public function recurringDaysOfYear(): array {
  453. // evaluate if RRULE exists and return day(s) of the year
  454. return $this->rruleIterator !== null ? $this->rruleIterator->daysOfYear() : [];
  455. }
  456. /**
  457. * event recurrence weeks of the month
  458. *
  459. * returns collection of RRULE SETPOS weeks(s) [1, 3, -1]
  460. * returns blank collection if RRULE is absent or SETPOS is absent, RDATE presents or absents has no affect
  461. *
  462. * @since 30.0.0
  463. *
  464. * @return array
  465. */
  466. public function recurringWeeksOfMonth(): array {
  467. // evaluate if RRULE exists and RRULE is relative return relative position(s)
  468. return $this->rruleIterator?->isRelative() ? $this->rruleIterator->relativePosition() : [];
  469. }
  470. /**
  471. * event recurrence weeks of the month (named)
  472. *
  473. * returns collection of RRULE SETPOS weeks(s) [1, 3, -1]
  474. * returns blank collection if RRULE is absent or SETPOS is absent, RDATE presents or absents has no affect
  475. *
  476. * @since 30.0.0
  477. *
  478. * @return array
  479. */
  480. public function recurringWeeksOfMonthNamed(): array {
  481. // evaluate if RRULE exists and extract relative position(s)
  482. $positions = $this->rruleIterator?->isRelative() ? $this->rruleIterator->relativePosition() : [];
  483. // convert numberic relative position to relative label
  484. foreach ($positions as $key => $value) {
  485. $positions[$key] = $this->relativePositionNamesMap[$value];
  486. }
  487. // return positions collection
  488. return $positions;
  489. }
  490. /**
  491. * event recurrence weeks of the year
  492. *
  493. * returns collection of RRULE BYWEEKNO weeks(s) [12, 32, 52]
  494. * returns blank collection if RRULE is absent, RDATE presents or absents has no affect
  495. *
  496. * @since 30.0.0
  497. *
  498. * @return array
  499. */
  500. public function recurringWeeksOfYear(): array {
  501. // evaluate if RRULE exists and return weeks(s) of the year
  502. return $this->rruleIterator !== null ? $this->rruleIterator->weeksOfYear() : [];
  503. }
  504. /**
  505. * event recurrence months of the year
  506. *
  507. * returns collection of RRULE BYMONTH month(s) [3, 7, 12]
  508. * returns blank collection if RRULE is absent, RDATE presents or absents has no affect
  509. *
  510. * @since 30.0.0
  511. *
  512. * @return array
  513. */
  514. public function recurringMonthsOfYear(): array {
  515. // evaluate if RRULE exists and return month(s) of the year
  516. return $this->rruleIterator !== null ? $this->rruleIterator->monthsOfYear() : [];
  517. }
  518. /**
  519. * event recurrence months of the year (named)
  520. *
  521. * returns collection of RRULE BYMONTH month(s) [3, 7, 12]
  522. * returns blank collection if RRULE is absent, RDATE presents or absents has no affect
  523. *
  524. * @since 30.0.0
  525. *
  526. * @return array
  527. */
  528. public function recurringMonthsOfYearNamed(): array {
  529. // evaluate if RRULE exists and extract month(s) of the year
  530. $months = $this->rruleIterator !== null ? $this->rruleIterator->monthsOfYear() : [];
  531. // convert numberic month to month name
  532. foreach ($months as $key => $value) {
  533. $months[$key] = $this->monthNamesMap[$value];
  534. }
  535. // return months collection
  536. return $months;
  537. }
  538. /**
  539. * event recurrence relative positions
  540. *
  541. * returns collection of RRULE SETPOS value(s) [1, 5, -3]
  542. * returns blank collection if RRULE is absent, RDATE presents or absents has no affect
  543. *
  544. * @since 30.0.0
  545. *
  546. * @return array
  547. */
  548. public function recurringRelativePosition(): array {
  549. // evaluate if RRULE exists and return relative position(s)
  550. return $this->rruleIterator !== null ? $this->rruleIterator->relativePosition() : [];
  551. }
  552. /**
  553. * event recurrence relative positions (named)
  554. *
  555. * returns collection of RRULE SETPOS [1, 3, -1]
  556. * returns blank collection if RRULE is absent or SETPOS is absent, RDATE presents or absents has no affect
  557. *
  558. * @since 30.0.0
  559. *
  560. * @return array
  561. */
  562. public function recurringRelativePositionNamed(): array {
  563. // evaluate if RRULE exists and extract relative position(s)
  564. $positions = $this->rruleIterator?->isRelative() ? $this->rruleIterator->relativePosition() : [];
  565. // convert numberic relative position to relative label
  566. foreach ($positions as $key => $value) {
  567. $positions[$key] = $this->relativePositionNamesMap[$value];
  568. }
  569. // return positions collection
  570. return $positions;
  571. }
  572. /**
  573. * event recurrence date
  574. *
  575. * returns date of currently selected recurrence
  576. *
  577. * @since 30.0.0
  578. *
  579. * @return DateTime
  580. */
  581. public function recurrenceDate(): DateTime | null {
  582. if ($this->recurrenceCurrentDate !== null) {
  583. return DateTime::createFromInterface($this->recurrenceCurrentDate);
  584. } else {
  585. return null;
  586. }
  587. }
  588. /**
  589. * event recurrence rewind
  590. *
  591. * sets the current recurrence to the first recurrence in the collection
  592. *
  593. * @since 30.0.0
  594. *
  595. * @return void
  596. */
  597. public function recurrenceRewind(): void {
  598. // rewind and increment rrule
  599. if ($this->rruleIterator !== null) {
  600. $this->rruleIterator->rewind();
  601. }
  602. // rewind and increment rdate
  603. if ($this->rdateIterator !== null) {
  604. $this->rdateIterator->rewind();
  605. }
  606. // rewind and increment exrule
  607. if ($this->eruleIterator !== null) {
  608. $this->eruleIterator->rewind();
  609. }
  610. // rewind and increment exdate
  611. if ($this->edateIterator !== null) {
  612. $this->edateIterator->rewind();
  613. }
  614. // set current date to event start date
  615. $this->recurrenceCurrentDate = clone $this->baseEventStartDate;
  616. }
  617. /**
  618. * event recurrence advance
  619. *
  620. * sets the current recurrence to the next recurrence in the collection
  621. *
  622. * @since 30.0.0
  623. *
  624. * @return void
  625. */
  626. public function recurrenceAdvance(): void {
  627. // place holders
  628. $nextOccurrenceDate = null;
  629. $nextExceptionDate = null;
  630. $rruleDate = null;
  631. $rdateDate = null;
  632. $eruleDate = null;
  633. $edateDate = null;
  634. // evaludate if rrule is set and advance one interation past current date
  635. if ($this->rruleIterator !== null) {
  636. // forward rrule to the next future date
  637. while ($this->rruleIterator->valid() && $this->rruleIterator->current() <= $this->recurrenceCurrentDate) {
  638. $this->rruleIterator->next();
  639. }
  640. $rruleDate = $this->rruleIterator->current();
  641. }
  642. // evaludate if rdate is set and advance one interation past current date
  643. if ($this->rdateIterator !== null) {
  644. // forward rdate to the next future date
  645. while ($this->rdateIterator->valid() && $this->rdateIterator->current() <= $this->recurrenceCurrentDate) {
  646. $this->rdateIterator->next();
  647. }
  648. $rdateDate = $this->rdateIterator->current();
  649. }
  650. if ($rruleDate !== null && $rdateDate !== null) {
  651. $nextOccurrenceDate = ($rruleDate <= $rdateDate) ? $rruleDate : $rdateDate;
  652. } elseif ($rruleDate !== null) {
  653. $nextOccurrenceDate = $rruleDate;
  654. } elseif ($rdateDate !== null) {
  655. $nextOccurrenceDate = $rdateDate;
  656. }
  657. // evaludate if exrule is set and advance one interation past current date
  658. if ($this->eruleIterator !== null) {
  659. // forward exrule to the next future date
  660. while ($this->eruleIterator->valid() && $this->eruleIterator->current() <= $this->recurrenceCurrentDate) {
  661. $this->eruleIterator->next();
  662. }
  663. $eruleDate = $this->eruleIterator->current();
  664. }
  665. // evaludate if exdate is set and advance one interation past current date
  666. if ($this->edateIterator !== null) {
  667. // forward exdate to the next future date
  668. while ($this->edateIterator->valid() && $this->edateIterator->current() <= $this->recurrenceCurrentDate) {
  669. $this->edateIterator->next();
  670. }
  671. $edateDate = $this->edateIterator->current();
  672. }
  673. // evaludate if exrule and exdate are set and set nextExDate to the first next date
  674. if ($eruleDate !== null && $edateDate !== null) {
  675. $nextExceptionDate = ($eruleDate <= $edateDate) ? $eruleDate : $edateDate;
  676. } elseif ($eruleDate !== null) {
  677. $nextExceptionDate = $eruleDate;
  678. } elseif ($edateDate !== null) {
  679. $nextExceptionDate = $edateDate;
  680. }
  681. // if the next date is part of exrule or exdate find another date
  682. if ($nextOccurrenceDate !== null && $nextExceptionDate !== null && $nextOccurrenceDate == $nextExceptionDate) {
  683. $this->recurrenceCurrentDate = $nextOccurrenceDate;
  684. $this->recurrenceAdvance();
  685. } else {
  686. $this->recurrenceCurrentDate = $nextOccurrenceDate;
  687. }
  688. }
  689. /**
  690. * event recurrence advance
  691. *
  692. * sets the current recurrence to the next recurrence in the collection after the specific date
  693. *
  694. * @since 30.0.0
  695. *
  696. * @param DateTimeInterface $dt date and time to advance
  697. *
  698. * @return void
  699. */
  700. public function recurrenceAdvanceTo(DateTimeInterface $dt): void {
  701. while ($this->recurrenceCurrentDate !== null && $this->recurrenceCurrentDate < $dt) {
  702. $this->recurrenceAdvance();
  703. }
  704. }
  705. }