RRuleIterator.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. <?php
  2. namespace Sabre\VObject\Recur;
  3. use DateTime;
  4. use InvalidArgumentException;
  5. use Iterator;
  6. use Sabre\VObject\DateTimeParser;
  7. use Sabre\VObject\Property;
  8. /**
  9. * RRuleParser
  10. *
  11. * This class receives an RRULE string, and allows you to iterate to get a list
  12. * of dates in that recurrence.
  13. *
  14. * For instance, passing: FREQ=DAILY;LIMIT=5 will cause the iterator to contain
  15. * 5 items, one for each day.
  16. *
  17. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  18. * @author Evert Pot (http://evertpot.com/)
  19. * @license http://sabre.io/license/ Modified BSD License
  20. */
  21. class RRuleIterator implements Iterator {
  22. /**
  23. * Creates the Iterator
  24. *
  25. * @param string|array $rrule
  26. * @param DateTime $start
  27. */
  28. public function __construct($rrule, DateTime $start) {
  29. $this->startDate = $start;
  30. $this->parseRRule($rrule);
  31. $this->currentDate = clone $this->startDate;
  32. }
  33. /* Implementation of the Iterator interface {{{ */
  34. public function current() {
  35. if (!$this->valid()) return null;
  36. return clone $this->currentDate;
  37. }
  38. /**
  39. * Returns the current item number
  40. *
  41. * @return int
  42. */
  43. public function key() {
  44. return $this->counter;
  45. }
  46. /**
  47. * Returns whether the current item is a valid item for the recurrence
  48. * iterator. This will return false if we've gone beyond the UNTIL or COUNT
  49. * statements.
  50. *
  51. * @return bool
  52. */
  53. public function valid() {
  54. if (!is_null($this->count)) {
  55. return $this->counter < $this->count;
  56. }
  57. return is_null($this->until) || $this->currentDate <= $this->until;
  58. }
  59. /**
  60. * Resets the iterator
  61. *
  62. * @return void
  63. */
  64. public function rewind() {
  65. $this->currentDate = clone $this->startDate;
  66. $this->counter = 0;
  67. }
  68. /**
  69. * Goes on to the next iteration
  70. *
  71. * @return void
  72. */
  73. public function next() {
  74. $previousStamp = $this->currentDate->getTimeStamp();
  75. // Otherwise, we find the next event in the normal RRULE
  76. // sequence.
  77. switch($this->frequency) {
  78. case 'hourly' :
  79. $this->nextHourly();
  80. break;
  81. case 'daily' :
  82. $this->nextDaily();
  83. break;
  84. case 'weekly' :
  85. $this->nextWeekly();
  86. break;
  87. case 'monthly' :
  88. $this->nextMonthly();
  89. break;
  90. case 'yearly' :
  91. $this->nextYearly();
  92. break;
  93. }
  94. $this->counter++;
  95. }
  96. /* End of Iterator implementation }}} */
  97. /**
  98. * Returns true if this recurring event never ends.
  99. *
  100. * @return bool
  101. */
  102. public function isInfinite() {
  103. return !$this->count && !$this->until;
  104. }
  105. /**
  106. * This method allows you to quickly go to the next occurrence after the
  107. * specified date.
  108. *
  109. * @param DateTime $dt
  110. * @return void
  111. */
  112. public function fastForward(\DateTime $dt) {
  113. while($this->valid() && $this->currentDate < $dt ) {
  114. $this->next();
  115. }
  116. }
  117. /**
  118. * The reference start date/time for the rrule.
  119. *
  120. * All calculations are based on this initial date.
  121. *
  122. * @var DateTime
  123. */
  124. protected $startDate;
  125. /**
  126. * The date of the current iteration. You can get this by calling
  127. * ->current().
  128. *
  129. * @var DateTime
  130. */
  131. protected $currentDate;
  132. /**
  133. * Frequency is one of: secondly, minutely, hourly, daily, weekly, monthly,
  134. * yearly.
  135. *
  136. * @var string
  137. */
  138. protected $frequency;
  139. /**
  140. * The number of recurrences, or 'null' if infinitely recurring.
  141. *
  142. * @var int
  143. */
  144. protected $count;
  145. /**
  146. * The interval.
  147. *
  148. * If for example frequency is set to daily, interval = 2 would mean every
  149. * 2 days.
  150. *
  151. * @var int
  152. */
  153. protected $interval = 1;
  154. /**
  155. * The last instance of this recurrence, inclusively
  156. *
  157. * @var \DateTime|null
  158. */
  159. protected $until;
  160. /**
  161. * Which seconds to recur.
  162. *
  163. * This is an array of integers (between 0 and 60)
  164. *
  165. * @var array
  166. */
  167. protected $bySecond;
  168. /**
  169. * Which minutes to recur
  170. *
  171. * This is an array of integers (between 0 and 59)
  172. *
  173. * @var array
  174. */
  175. protected $byMinute;
  176. /**
  177. * Which hours to recur
  178. *
  179. * This is an array of integers (between 0 and 23)
  180. *
  181. * @var array
  182. */
  183. protected $byHour;
  184. /**
  185. * The current item in the list.
  186. *
  187. * You can get this number with the key() method.
  188. *
  189. * @var int
  190. */
  191. protected $counter = 0;
  192. /**
  193. * Which weekdays to recur.
  194. *
  195. * This is an array of weekdays
  196. *
  197. * This may also be preceeded by a positive or negative integer. If present,
  198. * this indicates the nth occurrence of a specific day within the monthly or
  199. * yearly rrule. For instance, -2TU indicates the second-last tuesday of
  200. * the month, or year.
  201. *
  202. * @var array
  203. */
  204. protected $byDay;
  205. /**
  206. * Which days of the month to recur
  207. *
  208. * This is an array of days of the months (1-31). The value can also be
  209. * negative. -5 for instance means the 5th last day of the month.
  210. *
  211. * @var array
  212. */
  213. protected $byMonthDay;
  214. /**
  215. * Which days of the year to recur.
  216. *
  217. * This is an array with days of the year (1 to 366). The values can also
  218. * be negative. For instance, -1 will always represent the last day of the
  219. * year. (December 31st).
  220. *
  221. * @var array
  222. */
  223. protected $byYearDay;
  224. /**
  225. * Which week numbers to recur.
  226. *
  227. * This is an array of integers from 1 to 53. The values can also be
  228. * negative. -1 will always refer to the last week of the year.
  229. *
  230. * @var array
  231. */
  232. protected $byWeekNo;
  233. /**
  234. * Which months to recur.
  235. *
  236. * This is an array of integers from 1 to 12.
  237. *
  238. * @var array
  239. */
  240. protected $byMonth;
  241. /**
  242. * Which items in an existing st to recur.
  243. *
  244. * These numbers work together with an existing by* rule. It specifies
  245. * exactly which items of the existing by-rule to filter.
  246. *
  247. * Valid values are 1 to 366 and -1 to -366. As an example, this can be
  248. * used to recur the last workday of the month.
  249. *
  250. * This would be done by setting frequency to 'monthly', byDay to
  251. * 'MO,TU,WE,TH,FR' and bySetPos to -1.
  252. *
  253. * @var array
  254. */
  255. protected $bySetPos;
  256. /**
  257. * When the week starts.
  258. *
  259. * @var string
  260. */
  261. protected $weekStart = 'MO';
  262. /* Functions that advance the iterator {{{ */
  263. /**
  264. * Does the processing for advancing the iterator for hourly frequency.
  265. *
  266. * @return void
  267. */
  268. protected function nextHourly() {
  269. $this->currentDate->modify('+' . $this->interval . ' hours');
  270. }
  271. /**
  272. * Does the processing for advancing the iterator for daily frequency.
  273. *
  274. * @return void
  275. */
  276. protected function nextDaily() {
  277. if (!$this->byHour && !$this->byDay) {
  278. $this->currentDate->modify('+' . $this->interval . ' days');
  279. return;
  280. }
  281. if (isset($this->byHour)) {
  282. $recurrenceHours = $this->getHours();
  283. }
  284. if (isset($this->byDay)) {
  285. $recurrenceDays = $this->getDays();
  286. }
  287. if (isset($this->byMonth)) {
  288. $recurrenceMonths = $this->getMonths();
  289. }
  290. do {
  291. if ($this->byHour) {
  292. if ($this->currentDate->format('G') == '23') {
  293. // to obey the interval rule
  294. $this->currentDate->modify('+' . $this->interval-1 . ' days');
  295. }
  296. $this->currentDate->modify('+1 hours');
  297. } else {
  298. $this->currentDate->modify('+' . $this->interval . ' days');
  299. }
  300. // Current month of the year
  301. $currentMonth = $this->currentDate->format('n');
  302. // Current day of the week
  303. $currentDay = $this->currentDate->format('w');
  304. // Current hour of the day
  305. $currentHour = $this->currentDate->format('G');
  306. } while (
  307. ($this->byDay && !in_array($currentDay, $recurrenceDays)) ||
  308. ($this->byHour && !in_array($currentHour, $recurrenceHours)) ||
  309. ($this->byMonth && !in_array($currentMonth, $recurrenceMonths))
  310. );
  311. }
  312. /**
  313. * Does the processing for advancing the iterator for weekly frequency.
  314. *
  315. * @return void
  316. */
  317. protected function nextWeekly() {
  318. if (!$this->byHour && !$this->byDay) {
  319. $this->currentDate->modify('+' . $this->interval . ' weeks');
  320. return;
  321. }
  322. if ($this->byHour) {
  323. $recurrenceHours = $this->getHours();
  324. }
  325. if ($this->byDay) {
  326. $recurrenceDays = $this->getDays();
  327. }
  328. // First day of the week:
  329. $firstDay = $this->dayMap[$this->weekStart];
  330. do {
  331. if ($this->byHour) {
  332. $this->currentDate->modify('+1 hours');
  333. } else {
  334. $this->currentDate->modify('+1 days');
  335. }
  336. // Current day of the week
  337. $currentDay = (int) $this->currentDate->format('w');
  338. // Current hour of the day
  339. $currentHour = (int) $this->currentDate->format('G');
  340. // We need to roll over to the next week
  341. if ($currentDay === $firstDay && (!$this->byHour || $currentHour == '0')) {
  342. $this->currentDate->modify('+' . $this->interval-1 . ' weeks');
  343. // We need to go to the first day of this week, but only if we
  344. // are not already on this first day of this week.
  345. if($this->currentDate->format('w') != $firstDay) {
  346. $this->currentDate->modify('last ' . $this->dayNames[$this->dayMap[$this->weekStart]]);
  347. }
  348. }
  349. // We have a match
  350. } while (($this->byDay && !in_array($currentDay, $recurrenceDays)) || ($this->byHour && !in_array($currentHour, $recurrenceHours)));
  351. }
  352. /**
  353. * Does the processing for advancing the iterator for monthly frequency.
  354. *
  355. * @return void
  356. */
  357. protected function nextMonthly() {
  358. $currentDayOfMonth = $this->currentDate->format('j');
  359. if (!$this->byMonthDay && !$this->byDay) {
  360. // If the current day is higher than the 28th, rollover can
  361. // occur to the next month. We Must skip these invalid
  362. // entries.
  363. if ($currentDayOfMonth < 29) {
  364. $this->currentDate->modify('+' . $this->interval . ' months');
  365. } else {
  366. $increase = 0;
  367. do {
  368. $increase++;
  369. $tempDate = clone $this->currentDate;
  370. $tempDate->modify('+ ' . ($this->interval*$increase) . ' months');
  371. } while ($tempDate->format('j') != $currentDayOfMonth);
  372. $this->currentDate = $tempDate;
  373. }
  374. return;
  375. }
  376. while(true) {
  377. $occurrences = $this->getMonthlyOccurrences();
  378. foreach($occurrences as $occurrence) {
  379. // The first occurrence thats higher than the current
  380. // day of the month wins.
  381. if ($occurrence > $currentDayOfMonth) {
  382. break 2;
  383. }
  384. }
  385. // If we made it all the way here, it means there were no
  386. // valid occurrences, and we need to advance to the next
  387. // month.
  388. //
  389. // This line does not currently work in hhvm. Temporary workaround
  390. // follows:
  391. // $this->currentDate->modify('first day of this month');
  392. $this->currentDate = new \DateTime($this->currentDate->format('Y-m-1 H:i:s'), $this->currentDate->getTimezone());
  393. // end of workaround
  394. $this->currentDate->modify('+ ' . $this->interval . ' months');
  395. // This goes to 0 because we need to start counting at the
  396. // beginning.
  397. $currentDayOfMonth = 0;
  398. }
  399. $this->currentDate->setDate($this->currentDate->format('Y'), $this->currentDate->format('n'), $occurrence);
  400. }
  401. /**
  402. * Does the processing for advancing the iterator for yearly frequency.
  403. *
  404. * @return void
  405. */
  406. protected function nextYearly() {
  407. $currentMonth = $this->currentDate->format('n');
  408. $currentYear = $this->currentDate->format('Y');
  409. $currentDayOfMonth = $this->currentDate->format('j');
  410. // No sub-rules, so we just advance by year
  411. if (!$this->byMonth) {
  412. // Unless it was a leap day!
  413. if ($currentMonth==2 && $currentDayOfMonth==29) {
  414. $counter = 0;
  415. do {
  416. $counter++;
  417. // Here we increase the year count by the interval, until
  418. // we hit a date that's also in a leap year.
  419. //
  420. // We could just find the next interval that's dividable by
  421. // 4, but that would ignore the rule that there's no leap
  422. // year every year that's dividable by a 100, but not by
  423. // 400. (1800, 1900, 2100). So we just rely on the datetime
  424. // functions instead.
  425. $nextDate = clone $this->currentDate;
  426. $nextDate->modify('+ ' . ($this->interval*$counter) . ' years');
  427. } while ($nextDate->format('n')!=2);
  428. $this->currentDate = $nextDate;
  429. return;
  430. }
  431. // The easiest form
  432. $this->currentDate->modify('+' . $this->interval . ' years');
  433. return;
  434. }
  435. $currentMonth = $this->currentDate->format('n');
  436. $currentYear = $this->currentDate->format('Y');
  437. $currentDayOfMonth = $this->currentDate->format('j');
  438. $advancedToNewMonth = false;
  439. // If we got a byDay or getMonthDay filter, we must first expand
  440. // further.
  441. if ($this->byDay || $this->byMonthDay) {
  442. while(true) {
  443. $occurrences = $this->getMonthlyOccurrences();
  444. foreach($occurrences as $occurrence) {
  445. // The first occurrence that's higher than the current
  446. // day of the month wins.
  447. // If we advanced to the next month or year, the first
  448. // occurrence is always correct.
  449. if ($occurrence > $currentDayOfMonth || $advancedToNewMonth) {
  450. break 2;
  451. }
  452. }
  453. // If we made it here, it means we need to advance to
  454. // the next month or year.
  455. $currentDayOfMonth = 1;
  456. $advancedToNewMonth = true;
  457. do {
  458. $currentMonth++;
  459. if ($currentMonth>12) {
  460. $currentYear+=$this->interval;
  461. $currentMonth = 1;
  462. }
  463. } while (!in_array($currentMonth, $this->byMonth));
  464. $this->currentDate->setDate($currentYear, $currentMonth, $currentDayOfMonth);
  465. }
  466. // If we made it here, it means we got a valid occurrence
  467. $this->currentDate->setDate($currentYear, $currentMonth, $occurrence);
  468. return;
  469. } else {
  470. // These are the 'byMonth' rules, if there are no byDay or
  471. // byMonthDay sub-rules.
  472. do {
  473. $currentMonth++;
  474. if ($currentMonth>12) {
  475. $currentYear+=$this->interval;
  476. $currentMonth = 1;
  477. }
  478. } while (!in_array($currentMonth, $this->byMonth));
  479. $this->currentDate->setDate($currentYear, $currentMonth, $currentDayOfMonth);
  480. return;
  481. }
  482. }
  483. /* }}} */
  484. /**
  485. * This method receives a string from an RRULE property, and populates this
  486. * class with all the values.
  487. *
  488. * @param string|array $rrule
  489. * @return void
  490. */
  491. protected function parseRRule($rrule) {
  492. if (is_string($rrule)) {
  493. $rrule = Property\ICalendar\Recur::stringToArray($rrule);
  494. }
  495. foreach($rrule as $key=>$value) {
  496. $key = strtoupper($key);
  497. switch($key) {
  498. case 'FREQ' :
  499. $value = strtolower($value);
  500. if (!in_array(
  501. $value,
  502. array('secondly','minutely','hourly','daily','weekly','monthly','yearly')
  503. )) {
  504. throw new InvalidArgumentException('Unknown value for FREQ=' . strtoupper($value));
  505. }
  506. $this->frequency = $value;
  507. break;
  508. case 'UNTIL' :
  509. $this->until = DateTimeParser::parse($value, $this->startDate->getTimezone());
  510. // In some cases events are generated with an UNTIL=
  511. // parameter before the actual start of the event.
  512. //
  513. // Not sure why this is happening. We assume that the
  514. // intention was that the event only recurs once.
  515. //
  516. // So we are modifying the parameter so our code doesn't
  517. // break.
  518. if($this->until < $this->startDate) {
  519. $this->until = $this->startDate;
  520. }
  521. break;
  522. case 'INTERVAL' :
  523. // No break
  524. case 'COUNT' :
  525. $val = (int)$value;
  526. if ($val < 1) {
  527. throw new \InvalidArgumentException(strtoupper($key) . ' in RRULE must be a positive integer!');
  528. }
  529. $key = strtolower($key);
  530. $this->$key = $val;
  531. break;
  532. case 'BYSECOND' :
  533. $this->bySecond = (array)$value;
  534. break;
  535. case 'BYMINUTE' :
  536. $this->byMinute = (array)$value;
  537. break;
  538. case 'BYHOUR' :
  539. $this->byHour = (array)$value;
  540. break;
  541. case 'BYDAY' :
  542. $value = (array)$value;
  543. foreach($value as $part) {
  544. if (!preg_match('#^ (-|\+)? ([1-5])? (MO|TU|WE|TH|FR|SA|SU) $# xi', $part)) {
  545. throw new \InvalidArgumentException('Invalid part in BYDAY clause: ' . $part);
  546. }
  547. }
  548. $this->byDay = $value;
  549. break;
  550. case 'BYMONTHDAY' :
  551. $this->byMonthDay = (array)$value;
  552. break;
  553. case 'BYYEARDAY' :
  554. $this->byYearDay = (array)$value;
  555. break;
  556. case 'BYWEEKNO' :
  557. $this->byWeekNo = (array)$value;
  558. break;
  559. case 'BYMONTH' :
  560. $this->byMonth = (array)$value;
  561. break;
  562. case 'BYSETPOS' :
  563. $this->bySetPos = (array)$value;
  564. break;
  565. case 'WKST' :
  566. $this->weekStart = strtoupper($value);
  567. break;
  568. default:
  569. throw new \InvalidArgumentException('Not supported: ' . strtoupper($key));
  570. }
  571. }
  572. }
  573. /**
  574. * Mappings between the day number and english day name.
  575. *
  576. * @var array
  577. */
  578. protected $dayNames = array(
  579. 0 => 'Sunday',
  580. 1 => 'Monday',
  581. 2 => 'Tuesday',
  582. 3 => 'Wednesday',
  583. 4 => 'Thursday',
  584. 5 => 'Friday',
  585. 6 => 'Saturday',
  586. );
  587. /**
  588. * Returns all the occurrences for a monthly frequency with a 'byDay' or
  589. * 'byMonthDay' expansion for the current month.
  590. *
  591. * The returned list is an array of integers with the day of month (1-31).
  592. *
  593. * @return array
  594. */
  595. protected function getMonthlyOccurrences() {
  596. $startDate = clone $this->currentDate;
  597. $byDayResults = array();
  598. // Our strategy is to simply go through the byDays, advance the date to
  599. // that point and add it to the results.
  600. if ($this->byDay) foreach($this->byDay as $day) {
  601. $dayName = $this->dayNames[$this->dayMap[substr($day,-2)]];
  602. // Dayname will be something like 'wednesday'. Now we need to find
  603. // all wednesdays in this month.
  604. $dayHits = array();
  605. // workaround for missing 'first day of the month' support in hhvm
  606. $checkDate = new \DateTime($startDate->format('Y-m-1'));
  607. // workaround modify always advancing the date even if the current day is a $dayName in hhvm
  608. if ($checkDate->format('l') !== $dayName) {
  609. $checkDate->modify($dayName);
  610. }
  611. do {
  612. $dayHits[] = $checkDate->format('j');
  613. $checkDate->modify('next ' . $dayName);
  614. } while ($checkDate->format('n') === $startDate->format('n'));
  615. // So now we have 'all wednesdays' for month. It is however
  616. // possible that the user only really wanted the 1st, 2nd or last
  617. // wednesday.
  618. if (strlen($day)>2) {
  619. $offset = (int)substr($day,0,-2);
  620. if ($offset>0) {
  621. // It is possible that the day does not exist, such as a
  622. // 5th or 6th wednesday of the month.
  623. if (isset($dayHits[$offset-1])) {
  624. $byDayResults[] = $dayHits[$offset-1];
  625. }
  626. } else {
  627. // if it was negative we count from the end of the array
  628. // might not exist, fx. -5th tuesday
  629. if (isset($dayHits[count($dayHits) + $offset])) {
  630. $byDayResults[] = $dayHits[count($dayHits) + $offset];
  631. }
  632. }
  633. } else {
  634. // There was no counter (first, second, last wednesdays), so we
  635. // just need to add the all to the list).
  636. $byDayResults = array_merge($byDayResults, $dayHits);
  637. }
  638. }
  639. $byMonthDayResults = array();
  640. if ($this->byMonthDay) foreach($this->byMonthDay as $monthDay) {
  641. // Removing values that are out of range for this month
  642. if ($monthDay > $startDate->format('t') ||
  643. $monthDay < 0-$startDate->format('t')) {
  644. continue;
  645. }
  646. if ($monthDay>0) {
  647. $byMonthDayResults[] = $monthDay;
  648. } else {
  649. // Negative values
  650. $byMonthDayResults[] = $startDate->format('t') + 1 + $monthDay;
  651. }
  652. }
  653. // If there was just byDay or just byMonthDay, they just specify our
  654. // (almost) final list. If both were provided, then byDay limits the
  655. // list.
  656. if ($this->byMonthDay && $this->byDay) {
  657. $result = array_intersect($byMonthDayResults, $byDayResults);
  658. } elseif ($this->byMonthDay) {
  659. $result = $byMonthDayResults;
  660. } else {
  661. $result = $byDayResults;
  662. }
  663. $result = array_unique($result);
  664. sort($result, SORT_NUMERIC);
  665. // The last thing that needs checking is the BYSETPOS. If it's set, it
  666. // means only certain items in the set survive the filter.
  667. if (!$this->bySetPos) {
  668. return $result;
  669. }
  670. $filteredResult = array();
  671. foreach($this->bySetPos as $setPos) {
  672. if ($setPos<0) {
  673. $setPos = count($result)+($setPos+1);
  674. }
  675. if (isset($result[$setPos-1])) {
  676. $filteredResult[] = $result[$setPos-1];
  677. }
  678. }
  679. sort($filteredResult, SORT_NUMERIC);
  680. return $filteredResult;
  681. }
  682. /**
  683. * Simple mapping from iCalendar day names to day numbers
  684. *
  685. * @var array
  686. */
  687. protected $dayMap = array(
  688. 'SU' => 0,
  689. 'MO' => 1,
  690. 'TU' => 2,
  691. 'WE' => 3,
  692. 'TH' => 4,
  693. 'FR' => 5,
  694. 'SA' => 6,
  695. );
  696. protected function getHours()
  697. {
  698. $recurrenceHours = array();
  699. foreach($this->byHour as $byHour) {
  700. $recurrenceHours[] = $byHour;
  701. }
  702. return $recurrenceHours;
  703. }
  704. protected function getDays() {
  705. $recurrenceDays = array();
  706. foreach($this->byDay as $byDay) {
  707. // The day may be preceeded with a positive (+n) or
  708. // negative (-n) integer. However, this does not make
  709. // sense in 'weekly' so we ignore it here.
  710. $recurrenceDays[] = $this->dayMap[substr($byDay,-2)];
  711. }
  712. return $recurrenceDays;
  713. }
  714. protected function getMonths() {
  715. $recurrenceMonths = array();
  716. foreach($this->byMonth as $byMonth) {
  717. $recurrenceMonths[] = $byMonth;
  718. }
  719. return $recurrenceMonths;
  720. }
  721. }