Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional datetime range filter when parsing large calendar files #474

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/Parser/MimeDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sabre\VObject\Parser;

use DateTimeInterface;
use Sabre\VObject\Component;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VCard;
Expand Down Expand Up @@ -39,6 +40,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 @@ -136,6 +151,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 +202,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