Skip to content

Commit e5a1a2a

Browse files
committed
add optional datetime range filter when parsing calendar files
1 parent ebe9748 commit e5a1a2a

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

lib/Parser/MimeDir.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sabre\VObject\Parser;
44

5+
use DateTimeImmutable;
6+
use DateTimeInterface;
57
use Sabre\VObject\Component;
68
use Sabre\VObject\Component\VCalendar;
79
use Sabre\VObject\Component\VCard;
@@ -39,6 +41,20 @@ class MimeDir extends Parser
3941
*/
4042
protected $root;
4143

44+
/**
45+
* Start of range.
46+
*
47+
* @var DateTimeInterface|null
48+
*/
49+
protected $start;
50+
51+
/**
52+
* End of range.
53+
*
54+
* @var DateTimeInterface|null
55+
*/
56+
protected $end;
57+
4258
/**
4359
* By default all input will be assumed to be UTF-8.
4460
*
@@ -75,7 +91,7 @@ class MimeDir extends Parser
7591
*
7692
* @return \Sabre\VObject\Document
7793
*/
78-
public function parse($input = null, $options = 0)
94+
public function parse($input = null, $options = 0, DateTimeInterface $start = null, DateTimeInterface $end = null)
7995
{
8096
$this->root = null;
8197

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

106+
$this->setTimeRange($start, $end);
107+
90108
$this->parseDocument();
91109

92110
return $this->root;
@@ -136,6 +154,20 @@ public function setInput($input)
136154
}
137155
}
138156

157+
/**
158+
* Sets the time range.
159+
*
160+
* Any VEvent object falling outside of this time range will be ignored.
161+
*
162+
* @param DateTimeInterface $start
163+
* @param DateTimeInterface $end
164+
*/
165+
public function setTimeRange(DateTimeInterface $start = null, DateTimeInterface $end = null)
166+
{
167+
$this->start = $start;
168+
$this->end = $end;
169+
}
170+
139171
/**
140172
* Parses an entire document.
141173
*/
@@ -173,6 +205,13 @@ protected function parseDocument()
173205
}
174206
$result = $this->parseLine($line);
175207
if ($result) {
208+
if ($result instanceof Component\VEvent) {
209+
if ($this->start && $this->end) {
210+
if (! $result->isInTimeRange($this->start, $this->end)) {
211+
continue;
212+
}
213+
}
214+
}
176215
$this->root->add($result);
177216
}
178217
}

0 commit comments

Comments
 (0)