Skip to content

Commit

Permalink
add optional datetime range filter when parsing calendar files
Browse files Browse the repository at this point in the history
  • Loading branch information
8633brown committed Oct 14, 2019
1 parent ebe9748 commit e5a1a2a
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion lib/Parser/MimeDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Sabre\VObject\Parser;

use DateTimeImmutable;
use DateTimeInterface;
use Sabre\VObject\Component;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VCard;
Expand Down Expand Up @@ -39,6 +41,20 @@ class MimeDir extends Parser
*/
protected $root;

/**
* Start of range.
*
* @var DateTimeInterface|null
*/
protected $start;

/**
* End of range.
*
* @var DateTimeInterface|null
*/
protected $end;

/**
* By default all input will be assumed to be UTF-8.
*
Expand Down Expand Up @@ -75,7 +91,7 @@ class MimeDir extends Parser
*
* @return \Sabre\VObject\Document
*/
public function parse($input = null, $options = 0)
public function parse($input = null, $options = 0, DateTimeInterface $start = null, DateTimeInterface $end = null)
{
$this->root = null;

Expand All @@ -87,6 +103,8 @@ public function parse($input = null, $options = 0)
$this->options = $options;
}

$this->setTimeRange($start, $end);

$this->parseDocument();

return $this->root;
Expand Down Expand Up @@ -136,6 +154,20 @@ public function setInput($input)
}
}

/**
* Sets the time range.
*
* Any VEvent object falling outside of this time range will be ignored.
*
* @param DateTimeInterface $start
* @param DateTimeInterface $end
*/
public function setTimeRange(DateTimeInterface $start = null, DateTimeInterface $end = null)
{
$this->start = $start;
$this->end = $end;
}

/**
* Parses an entire document.
*/
Expand Down Expand Up @@ -173,6 +205,13 @@ protected function parseDocument()
}
$result = $this->parseLine($line);
if ($result) {
if ($result instanceof Component\VEvent) {
if ($this->start && $this->end) {
if (! $result->isInTimeRange($this->start, $this->end)) {
continue;
}
}
}
$this->root->add($result);
}
}
Expand Down

0 comments on commit e5a1a2a

Please sign in to comment.