1
0
mirror of https://github.com/rlanvin/php-rrule.git synced 2025-02-20 09:54:16 +01:00

count, offsetExists and offsetGet for RSet

offsetGet is uncached at the moment (ref #7)
This commit is contained in:
rlanvin 2016-03-21 23:56:35 +02:00
parent d2384c1997
commit 0cf1540f85
4 changed files with 142 additions and 62 deletions

View File

@ -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]);

View File

@ -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;
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -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]);
}
}

View File

@ -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()