e8208ebce5bd24e79df6763a2f3c98c21f0e793a
[bdrem.git] / src / bdrem / Source / Bdf.php
1 <?php
2 namespace bdrem;
3
4 /**
5  * Reads birthday reminder 2's birthday files (.bdf).
6  */
7 class Source_Bdf
8 {
9     protected $filename;
10
11     public function __construct($filename)
12     {
13         $this->filename = $filename;
14         if (!file_exists($this->filename)) {
15             throw new \Exception(
16                 'Birthday file does not exist: ' . $this->filename
17             );
18         }
19     }
20
21     /**
22      * @param string $strDate Date the events shall be found for, YYYY-MM-DD
23      */
24     public function getEvents($strDate, $nDaysBefore, $nDaysAfter)
25     {
26         $x = simplexml_load_file($this->filename);
27
28         $arEvents = array();
29         foreach ($x->content->person as $xPerson) {
30             $date = implode(
31                 '-',
32                 array_reverse(
33                     explode('.', (string) $xPerson->date)
34                 )
35             );
36             $event = new Event(
37                 (string) $xPerson->name,
38                 (string) $xPerson->event,
39                 $date
40             );
41             if ($event->isWithin($strDate, $nDaysBefore, $nDaysAfter)) {
42                 $arEvents[] = $event;
43             }
44         }
45         return $arEvents;
46     }
47 }
48 ?>