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

Add remove/clear functions for ExDates and RDates (#66)

- Cleanup tests and comment
- Add removeExdate and clearExdates
- Add removeDate and clearDates for RDATEs
This commit is contained in:
Ricky 2019-03-17 04:45:34 -05:00 committed by Rémi Lanvin
parent 90af29e79b
commit 58b9bd1115
5 changed files with 210 additions and 2 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
*.sublime*
vendor
test.php
composer.lock
composer.lock
.idea

View File

@ -2061,6 +2061,9 @@ class RRule implements RRuleInterface
* Parse a locale and returns a list of files to load.
* For example "fr_FR" will produce "fr" and "fr_FR"
*
* @param $locale
* @param null $use_intl
*
* @return array
*/
static protected function i18nFilesToLoad($locale, $use_intl = null)

View File

@ -223,6 +223,46 @@ class RSet implements RRuleInterface
return $this;
}
/**
* Remove an RDATE
*
* @param mixed $date a valid date representation or a \DateTime object
* @return $this
*/
public function removeDate($date)
{
try {
$date_to_remove = RRule::parseDate($date);
$index = array_search($date_to_remove, $this->rdates);
if ( $index !== false ) {
unset($this->rdates[$index]);
$this->rdates = array_values($this->rdates);
}
} catch (\Exception $e) {
throw new \InvalidArgumentException(
'Failed to parse RDATE - it must be a valid date, timestamp or \DateTime object'
);
}
$this->clearCache();
return $this;
}
/**
* Remove all RDATEs
*
* @return $this
*/
public function clearDates()
{
$this->rdates = [];
$this->clearCache();
return $this;
}
/**
* Return the RDATE(s) contained in this set
*
@ -257,6 +297,46 @@ class RSet implements RRuleInterface
return $this;
}
/**
* Remove an EXDATE
*
* @param mixed $date a valid date representation or a \DateTime object
* @return $this
*/
public function removeExDate($date)
{
try {
$date_to_remove = RRule::parseDate($date);
$index = array_search($date_to_remove, $this->exdates);
if ( $index !== false ) {
unset($this->exdates[$index]);
$this->exdates = array_values($this->exdates);
}
} catch (\Exception $e) {
throw new \InvalidArgumentException(
'Failed to parse EXDATE - it must be a valid date, timestamp or \DateTime object'
);
}
$this->clearCache();
return $this;
}
/**
* Removes all EXDATEs
*
* @return $this
*/
public function clearExDates()
{
$this->exdates = [];
$this->clearCache();
return $this;
}
/**
* Return the EXDATE(s) contained in this set
*

View File

@ -2771,6 +2771,7 @@ class RRuleTest extends TestCase
$method->invokeArgs(null, array($locale, false));
$this->fail('Expected InvalidArgumentException not thrown (files was '.json_encode($files).')');
} catch (\InvalidArgumentException $e) {
$this->assertStringStartsWith("The locale option does not look like a valid locale:", $e->getMessage());
}
}
else {

View File

@ -87,6 +87,55 @@ class RSetTest extends TestCase
$this->assertFalse($rset->occursAt('1997-09-03 09:00'));
}
public function testRemoveDateFromRSet()
{
$rset = new RSet();
$rset->addRRule(array(
'FREQ' => 'YEARLY',
'COUNT' => 1,
'BYDAY' => 'TU',
'DTSTART' => date_create('1997-09-02 09:00')
));
$rset->addDate(date_create('1997-09-04 09:00'));
$rset->addDate(date_create('1997-09-09 09:00'));
$this->assertEquals(array(
date_create('1997-09-02 09:00'),
date_create('1997-09-04 09:00'),
date_create('1997-09-09 09:00')
), $rset->getOccurrences());
$rset->removeDate(date_create('1997-09-09 09:00'));
$this->assertEquals(array(
date_create('1997-09-02 09:00'),
date_create('1997-09-04 09:00')
), $rset->getOccurrences());
}
public function testClearDatesFromRSet()
{
$rset = new RSet();
$rset->addRRule(array(
'FREQ' => 'YEARLY',
'COUNT' => 1,
'BYDAY' => 'TU',
'DTSTART' => date_create('1997-09-02 09:00')
));
$rset->addDate(date_create('1997-09-04 09:00'));
$rset->addDate(date_create('1997-09-09 09:00'));
$this->assertEquals(array(
date_create('1997-09-02 09:00'),
date_create('1997-09-04 09:00'),
date_create('1997-09-09 09:00')
), $rset->getOccurrences());
$rset->clearDates();
$this->assertEquals(array(
date_create('1997-09-02 09:00'),
), $rset->getOccurrences());
}
public function testCombineRRuleAndExRule()
{
$rset = new RSet();
@ -151,9 +200,83 @@ class RSetTest extends TestCase
$this->assertFalse($rset->occursAt('1997-09-04 09:00'));
}
public function testRemoveExdDateFromRSet()
{
$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'); // adding out of order
$this->assertEquals(array(
date_create('1997-09-02 09:00'),
date_create('1997-09-09 09:00'),
date_create('1997-09-16 09:00')
), $rset->getOccurrences());
$rset->removeExdate('1997-09-11 09:00:00');
$this->assertEquals(array(
date_create('1997-09-02 09:00'),
date_create('1997-09-09 09:00'),
date_create('1997-09-11 09:00'),
date_create('1997-09-16 09:00')
), $rset->getOccurrences());
$rset->removeExdate('1997-09-18 09:00:00');
$rset->removeExdate('1997-09-04 09:00:00');
$this->assertEquals(array(
date_create('1997-09-02 09:00'),
date_create('1997-09-04 09:00'),
date_create('1997-09-09 09:00'),
date_create('1997-09-11 09:00'),
date_create('1997-09-16 09:00'),
date_create('1997-09-18 09:00')
), $rset->getOccurrences());
}
public function testClearExDatesFromRSet()
{
$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'); // adding out of order
$this->assertEquals(array(
date_create('1997-09-02 09:00'),
date_create('1997-09-09 09:00'),
date_create('1997-09-16 09:00')
), $rset->getOccurrences());
$rset->clearExDates();
$this->assertEquals(array(
date_create('1997-09-02 09:00'),
date_create('1997-09-04 09:00'),
date_create('1997-09-09 09:00'),
date_create('1997-09-11 09:00'),
date_create('1997-09-16 09:00'),
date_create('1997-09-18 09:00')
), $rset->getOccurrences());
}
public function testCombineEverything()
{
// TODO
$this->markTestIncomplete("TODO!");
}
public function testCombineMultipleTimezones()