1
0

EventReader.php 23 KB

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