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

OccursAt for RSet

Ref #7
This commit is contained in:
rlanvin 2016-03-23 17:36:24 +02:00
parent ea059ca381
commit 0a46d7cd35
2 changed files with 75 additions and 1 deletions

View File

@ -204,7 +204,49 @@ class RSet implements RRuleInterface
public function occursAt($date)
{
throw new \Exception(__METHOD__.' is unimplemented');
$date = RRule::parseDate($date);
if ( in_array($date, $this->cache) ) {
// in the cache (whether cache is complete or not)
return true;
}
elseif ( $this->total !== null ) {
// cache complete and not in cache
return false;
}
// test if it *should* occur (before exclusion)
$occurs = false;
foreach ( $this->rdates as $rdate ) {
if ( $rdate == $date ) {
$occurs = true;
break;
}
}
if ( ! $occurs ) {
foreach ( $this->rrules as $rrule ) {
if ( $rrule->occursAt($date) ) {
$occurs = true;
break;
}
}
}
// if it should occur, test if it's excluded
if ( $occurs ) {
foreach ( $this->exdates as $exdate ) {
if ( $exdate == $date ) {
return false;
}
}
foreach ( $this->exrules as $exrule ) {
if ( $exrule->occursAt($date) ) {
return false;
}
}
}
return $occurs;
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -29,6 +29,14 @@ class RSetTest extends PHPUnit_Framework_TestCase
$this->assertEquals(date_create('1997-09-04 09:00'),$rset[1]);
$this->assertEquals(array(date_create('1997-09-04 09:00')),$rset->getOccurrencesBetween('1997-09-04 00:00', '1997-09-05 00:00'));
$this->assertTrue($rset->occursAt('1997-09-02 09:00'));
$this->assertFalse($rset->occursAt('1997-09-03 09:00'));
$rset->clearCache();
$this->assertTrue($rset->occursAt('1997-09-02 09:00'));
$this->assertFalse($rset->occursAt('1997-09-03 09:00'));
}
public function testCombineRRuleAndDate()
@ -50,6 +58,14 @@ class RSetTest extends PHPUnit_Framework_TestCase
$this->assertEquals(date_create('1997-09-04 09:00'),$rset[1]);
$this->assertEquals(array(date_create('1997-09-04 09:00')),$rset->getOccurrencesBetween('1997-09-04 00:00', '1997-09-05 00:00'));
$this->assertTrue($rset->occursAt('1997-09-04 09:00'));
$this->assertFalse($rset->occursAt('1997-09-03 09:00'));
$rset->clearCache();
$this->assertTrue($rset->occursAt('1997-09-04 09:00'));
$this->assertFalse($rset->occursAt('1997-09-03 09:00'));
}
public function testCombineRRuleAndExRule()
@ -75,6 +91,14 @@ class RSetTest extends PHPUnit_Framework_TestCase
$this->assertEquals(date_create('1997-09-09 09:00'),$rset[1]);
$this->assertEquals(array(date_create('1997-09-16 09:00')),$rset->getOccurrencesBetween('1997-09-16 00:00', '1997-09-17 00:00'));
$this->assertTrue($rset->occursAt('1997-09-09 09:00'));
$this->assertFalse($rset->occursAt('1997-09-04 09:00'));
$rset->clearCache();
$this->assertTrue($rset->occursAt('1997-09-09 09:00'));
$this->assertFalse($rset->occursAt('1997-09-04 09:00'));
}
public function testCombineRRuleAndExDate()
@ -98,6 +122,14 @@ class RSetTest extends PHPUnit_Framework_TestCase
$this->assertEquals(date_create('1997-09-09 09:00'),$rset[1]);
$this->assertEquals(array(date_create('1997-09-16 09:00')),$rset->getOccurrencesBetween('1997-09-16 00:00', '1997-09-17 00:00'));
$this->assertTrue($rset->occursAt('1997-09-02 09:00'));
$this->assertFalse($rset->occursAt('1997-09-04 09:00'));
$rset->clearCache();
$this->assertTrue($rset->occursAt('1997-09-02 09:00'));
$this->assertFalse($rset->occursAt('1997-09-04 09:00'));
}
public function testCombineEverything()