diff --git a/src/RRule.php b/src/RRule.php index cf35b05..d4eddfc 100755 --- a/src/RRule.php +++ b/src/RRule.php @@ -1419,6 +1419,7 @@ class RRule implements RRuleInterface } // echo "Cache used up with occurrences remaining\n"; if ( $dtstart ) { + $dtstart = clone $dtstart; // since DateTime is not immutable, avoid any problem // so we skip the last occurrence of the cache if ( $this->freq === self::SECONDLY ) { $dtstart->modify('+'.$this->interval.'second'); @@ -1628,7 +1629,11 @@ class RRule implements RRuleInterface else { // normal loop, without BYSETPOS while ( ($yearday = current($dayset)) !== false ) { - $occurrence = \DateTime::createFromFormat('Y z', "$year $yearday",$this->dtstart->getTimezone()); + $occurrence = \DateTime::createFromFormat( + 'Y z', + "$year $yearday", + $this->dtstart->getTimezone() + ); while ( ($time = current($timeset)) !== false ) { $occurrence->setTime($time[0], $time[1], $time[2]); diff --git a/src/RSet.php b/src/RSet.php index 31bee4d..3e047f3 100755 --- a/src/RSet.php +++ b/src/RSet.php @@ -204,13 +204,13 @@ class RSet implements RRuleInterface public function offsetExists($offset) { - throw new \Exception(__METHOD__.' is unimplemented'); - // return is_numeric($offset) && $offset >= 0 && $offset < count($this); + return is_numeric($offset) && $offset >= 0 && $offset < count($this); } public function offsetGet($offset) { - throw new \Exception(__METHOD__.' is unimplemented'); + // TODO: Cache + // if ( isset($this->cache[$offset]) ) { // // found in cache // return $this->cache[$offset]; @@ -220,30 +220,28 @@ class RSet implements RRuleInterface // return null; // } - // // not in cache and cache not complete, we have to loop to find it - // $i = 0; - // foreach ( $this as $occurrence ) { - // if ( $i == $offset ) { - // return $occurrence; - // } - // $i++; - // if ( $i > $offset ) { - // break; - // } - // } - // return null; + // not in cache and cache not complete, we have to loop to find it + $i = 0; + foreach ( $this as $occurrence ) { + if ( $i == $offset ) { + return $occurrence; + } + $i++; + if ( $i > $offset ) { + break; + } + } + return null; } public function offsetSet($offset, $value) { - throw new \Exception(__METHOD__.' is unimplemented'); - // throw new \LogicException('Setting a Date in a RRule is not supported'); + throw new \LogicException('Setting a Date in a RSet is not supported (use addDate)'); } public function offsetUnset($offset) { - throw new \Exception(__METHOD__.' is unimplemented'); - // throw new \LogicException('Unsetting a Date in a RRule is not supported'); + throw new \LogicException('Unsetting a Date in a RSet is not supported (use addDate)'); } /////////////////////////////////////////////////////////////////////////////// @@ -257,16 +255,15 @@ class RSet implements RRuleInterface */ public function count() { - throw new \Exception(__METHOD__.' is unimplemented'); - // if ( ! $this->count && ! $this->until ) { - // throw new \LogicException('Cannot count an infinite recurrence rule.'); - // } + if ( $this->isInfinite() ) { + throw new \LogicException('Cannot count an infinite recurrence set.'); + } - // if ( $this->total === null ) { - // foreach ( $this as $occurrence ) {} - // } + if ( $this->total === null ) { + foreach ( $this as $occurrence ) {} + } - // return $this->total; + return $this->total; } /////////////////////////////////////////////////////////////////////////////// diff --git a/tests/RRuleTest.php b/tests/RRuleTest.php index 6cf4b0c..1db6efc 100755 --- a/tests/RRuleTest.php +++ b/tests/RRuleTest.php @@ -4,39 +4,6 @@ use RRule\RRule; class RRuleTest extends PHPUnit_Framework_TestCase { - public function testIsFinite() - { - $rrule = new RRule(array( - 'freq' => 'yearly' - )); - $this->assertTrue($rrule->isInfinite()); - $this->assertFalse($rrule->isFinite()); - - $rrule = new RRule(array( - 'freq' => 'yearly', - 'count' => 10 - )); - $this->assertFalse($rrule->isInfinite()); - $this->assertTrue($rrule->isFinite()); - } - - public function testIsLeapYear() - { - $this->assertFalse(\RRule\is_leap_year(1700)); - $this->assertFalse(\RRule\is_leap_year(1800)); - $this->assertFalse(\RRule\is_leap_year(1900)); - $this->assertTrue(\RRule\is_leap_year(2000)); - } - - public function testCountable() - { - $rrule = new RRule(array( - 'freq' => 'yearly', - 'count' => 10 - )); - $this->assertEquals(10, count($rrule)); - } - /** * These rules are invalid according to the RFC */ @@ -1724,4 +1691,65 @@ class RRuleTest extends PHPUnit_Framework_TestCase $this->assertEquals(date_create('1997-09-01 09:00:00', new DateTimeZone('America/New_York')), $rrule[0]); } + public function testIsFinite() + { + $rrule = new RRule(array( + 'freq' => 'yearly' + )); + $this->assertTrue($rrule->isInfinite()); + $this->assertFalse($rrule->isFinite()); + + $rrule = new RRule(array( + 'freq' => 'yearly', + 'count' => 10 + )); + $this->assertFalse($rrule->isInfinite()); + $this->assertTrue($rrule->isFinite()); + } + + public function testIsLeapYear() + { + $this->assertFalse(\RRule\is_leap_year(1700)); + $this->assertFalse(\RRule\is_leap_year(1800)); + $this->assertFalse(\RRule\is_leap_year(1900)); + $this->assertTrue(\RRule\is_leap_year(2000)); + } + + public function testCountable() + { + $rrule = new RRule(array( + 'freq' => 'yearly', + 'count' => 10 + )); + $this->assertEquals(10, count($rrule)); + } + + public function testOffsetExists() + { + $rrule = new RRule(array( + 'freq' => 'daily', + 'count' => 3, + 'byday' => 'TU,TH', + 'dtstart' => '2007-01-01' + )); + $this->assertTrue(isset($rrule[0])); + $this->assertTrue(isset($rrule[1])); + $this->assertTrue(isset($rrule[2])); + $this->assertFalse(isset($rrule[3])); + } + + public function testOffsetGet() + { + $rrule = new RRule(array( + 'freq' => 'daily', + 'count' => 3, + 'byday' => 'TU,TH', + 'dtstart' => '2007-01-01' + )); + + $this->assertEquals(date_create('2007-01-02'), $rrule[0]); + $this->assertEquals(date_create('2007-01-04'), $rrule[1]); + $this->assertEquals(date_create('2007-01-09'), $rrule[2]); + $this->assertEquals(null, $rrule[4]); + } } diff --git a/tests/RSetTest.php b/tests/RSetTest.php index 7bcfd12..36ce34b 100755 --- a/tests/RSetTest.php +++ b/tests/RSetTest.php @@ -115,7 +115,57 @@ class RSetTest extends PHPUnit_Framework_TestCase public function testCountable() { - // TODO + $rset = new RSet(); + $rset->addRRule(array( + 'FREQ' => 'YEARLY', + 'COUNT' => 6, + 'BYDAY' => 'TU, TH', + 'DTSTART' => date_create('1997-09-02 09:00') + )); + $rset->addExdate('1997-09-04 09:00:00'); + $rset->addExdate('1997-09-11 09:00:00'); + $rset->addExdate('1997-09-18 09:00:00'); + + $this->assertEquals(3, count($rset)); + } + + public function testOffsetExists() + { + $rset = new RSet(); + $rset->addRRule(array( + 'FREQ' => 'YEARLY', + 'COUNT' => 6, + 'BYDAY' => 'TU, TH', + 'DTSTART' => date_create('1997-09-02 09:00') + )); + $rset->addExdate('1997-09-04 09:00:00'); + $rset->addExdate('1997-09-11 09:00:00'); + $rset->addExdate('1997-09-18 09:00:00'); + + $this->assertTrue(isset($rset[0])); + $this->assertTrue(isset($rset[1])); + $this->assertTrue(isset($rset[2])); + $this->assertFalse(isset($rset[3])); + } + + public function testOffsetGet() + { + $rset = new RSet(); + $rset->addRRule(array( + 'FREQ' => 'YEARLY', + 'COUNT' => 6, + 'BYDAY' => 'TU, TH', + 'DTSTART' => date_create('1997-09-02 09:00:00') + )); + $rset->addExdate('1997-09-04 09:00:00'); + $rset->addExdate('1997-09-11 09:00:00'); + $rset->addExdate('1997-09-18 09:00:00'); + +// var_dump($rset->getOccurrences()); + $this->assertEquals(date_create('1997-09-02 09:00:00'), $rset[0]); + $this->assertEquals(date_create('1997-09-09 09:00:00'), $rset[1]); + $this->assertEquals(date_create('1997-09-16 09:00:00'), $rset[2]); + $this->assertEquals(null, $rset[3]); } public function testRSetInRset()