From e5a1a2a0497c82940cea0335bd9f1ad432723259 Mon Sep 17 00:00:00 2001 From: 8633brown Date: Sun, 13 Oct 2019 19:25:05 -0600 Subject: [PATCH] add optional datetime range filter when parsing calendar files --- lib/Parser/MimeDir.php | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/Parser/MimeDir.php b/lib/Parser/MimeDir.php index 26a7101e5..fcf4127b0 100644 --- a/lib/Parser/MimeDir.php +++ b/lib/Parser/MimeDir.php @@ -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; @@ -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. * @@ -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; @@ -87,6 +103,8 @@ public function parse($input = null, $options = 0) $this->options = $options; } + $this->setTimeRange($start, $end); + $this->parseDocument(); return $this->root; @@ -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. */ @@ -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); } }