[Pkg-owncloud-commits] [php-sabre-vobject] 66/106: Added the RDate iterator. Currently untested.
David Prévot
taffit at moszumanska.debian.org
Fri Aug 22 15:11:04 UTC 2014
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository php-sabre-vobject.
commit 53192cf28015f945b51de894370eb53fe92d869c
Author: Evert Pot <evert at rooftopsolutions.nl>
Date: Wed Aug 6 20:23:05 2014 -0400
Added the RDate iterator. Currently untested.
---
lib/Recur/EventIterator.php | 35 +++++++---
lib/Recur/RDateIterator.php | 164 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 191 insertions(+), 8 deletions(-)
diff --git a/lib/Recur/EventIterator.php b/lib/Recur/EventIterator.php
index 7221cdd..28c1889 100644
--- a/lib/Recur/EventIterator.php
+++ b/lib/Recur/EventIterator.php
@@ -155,7 +155,26 @@ class EventIterator implements \Iterator {
$this->eventDuration = 0;
}
- $this->rruleIterator = new RRuleIterator($rrule, $this->startDate);
+ 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(
+ array(
+ 'FREQ' => 'DAILY',
+ 'COUNT' => 1,
+ ),
+ $this->startDate
+ );
+ }
+
$this->rewind();
}
@@ -230,7 +249,7 @@ class EventIterator implements \Iterator {
if (isset($event->DTEND)) {
$event->DTEND->setDateTime($this->getDtEnd());
}
- if ($this->rruleIterator->key() > 0) {
+ if ($this->recurIterator->key() > 0) {
$event->{'RECURRENCE-ID'} = (string)$event->DTSTART;
}
return $event;
@@ -268,7 +287,7 @@ class EventIterator implements \Iterator {
*/
public function rewind() {
- $this->rruleIterator->rewind();
+ $this->recurIterator->rewind();
// re-creating overridden event index.
$index = array();
foreach($this->overriddenEvents as $key=>$event) {
@@ -305,15 +324,15 @@ class EventIterator implements \Iterator {
// We need to do this until we find a date that's not in the
// exception list.
do {
- if (!$this->rruleIterator->valid()) {
+ if (!$this->recurIterator->valid()) {
$nextDate = null;
break;
}
- $nextDate = $this->rruleIterator->current();
+ $nextDate = $this->recurIterator->current();
if (!$nextDate) {
break;
}
- $this->rruleIterator->next();
+ $this->recurIterator->next();
} while(isset($this->exceptions[$nextDate->getTimeStamp()]));
}
@@ -367,7 +386,7 @@ class EventIterator implements \Iterator {
*/
public function isInfinite() {
- return $this->rruleIterator->isInfinite();
+ return $this->recurIterator->isInfinite();
}
@@ -376,7 +395,7 @@ class EventIterator implements \Iterator {
*
* @var RRuleIterator
*/
- protected $rruleIterator;
+ protected $recurIterator;
/**
* The duration, in seconds, of the master event.
diff --git a/lib/Recur/RDateIterator.php b/lib/Recur/RDateIterator.php
new file mode 100644
index 0000000..4462bf7
--- /dev/null
+++ b/lib/Recur/RDateIterator.php
@@ -0,0 +1,164 @@
+<?php
+
+namespace Sabre\VObject\Recur;
+
+use DateTime;
+use InvalidArgumentException;
+use Iterator;
+use Sabre\VObject\DateTimeParser;
+
+
+/**
+ * RRuleParser
+ *
+ * This class receives an RRULE string, and allows you to iterate to get a list
+ * of dates in that recurrence.
+ *
+ * For instance, passing: FREQ=DAILY;LIMIT=5 will cause the iterator to contain
+ * 5 items, one for each day.
+ *
+ * @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
+ * @author Evert Pot (http://evertpot.com/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class RDateIterator implements Iterator {
+
+ /**
+ * Creates the Iterator
+ *
+ * @param string|array $rrule
+ * @param DateTime $start
+ */
+ public function __construct($rrule, DateTime $start) {
+
+ $this->startDate = $start;
+ $this->parseRDate($rrule);
+ $this->currentDate = clone $this->startDate;
+
+ }
+
+ /* Implementation of the Iterator interface {{{ */
+
+ public function current() {
+
+ if (!$this->valid()) return null;
+ return clone $this->currentDate;
+
+ }
+
+ /**
+ * Returns the current item number
+ *
+ * @return int
+ */
+ public function key() {
+
+ return $this->counter;
+
+ }
+
+ /**
+ * Returns whether the current item is a valid item for the recurrence
+ * iterator.
+ *
+ * @return bool
+ */
+ public function valid() {
+
+ return ($this->counter < count($this->dates));
+
+ }
+
+ /**
+ * Resets the iterator
+ *
+ * @return void
+ */
+ public function rewind() {
+
+ $this->currentDate = clone $this->startDate;
+ $this->counter = 0;
+
+ }
+
+ /**
+ * Goes on to the next iteration
+ *
+ * @return void
+ */
+ public function next() {
+
+ $this->currentDate->modify(
+ DateTimeParser::parse(
+ $this->dates[$this->counter]
+ )->format('Y-m-d H:i:s')
+ );
+ $this->counter++;
+
+ }
+
+ /* End of Iterator implementation }}} */
+
+ /**
+ * Returns true if this recurring event never ends.
+ *
+ * @return bool
+ */
+ public function isInfinite() {
+
+ return false;
+
+ }
+
+ /**
+ * This method allows you to quickly go to the next occurrence after the
+ * specified date.
+ *
+ * @param DateTime $dt
+ * @return void
+ */
+ public function fastForward(\DateTime $dt) {
+
+ while($this->valid() && $this->currentDate < $dt ) {
+ $this->next();
+ }
+
+ }
+
+ /**
+ * The reference start date/time for the rrule.
+ *
+ * All calculations are based on this initial date.
+ *
+ * @var DateTime
+ */
+ protected $startDate;
+
+ /**
+ * The date of the current iteration. You can get this by calling
+ * ->current().
+ *
+ * @var DateTime
+ */
+ protected $currentDate;
+
+ /* }}} */
+
+ /**
+ * This method receives a string from an RRULE property, and populates this
+ * class with all the values.
+ *
+ * @param string|array $rrule
+ * @return void
+ */
+ protected function parseRDate($rdate) {
+
+ if (is_string($rdate)) {
+ $rdate = explode(',', $rdate);
+ }
+
+ $this->dates = $rdate;
+
+ }
+
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabre-vobject.git
More information about the Pkg-owncloud-commits
mailing list