aboutsummaryrefslogtreecommitdiff
path: root/src/bdrem/Source/Bdf.php
blob: e8208ebce5bd24e79df6763a2f3c98c21f0e793a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace bdrem;

/**
 * Reads birthday reminder 2's birthday files (.bdf).
 */
class Source_Bdf
{
    protected $filename;

    public function __construct($filename)
    {
        $this->filename = $filename;
        if (!file_exists($this->filename)) {
            throw new \Exception(
                'Birthday file does not exist: ' . $this->filename
            );
        }
    }

    /**
     * @param string $strDate Date the events shall be found for, YYYY-MM-DD
     */
    public function getEvents($strDate, $nDaysBefore, $nDaysAfter)
    {
        $x = simplexml_load_file($this->filename);

        $arEvents = array();
        foreach ($x->content->person as $xPerson) {
            $date = implode(
                '-',
                array_reverse(
                    explode('.', (string) $xPerson->date)
                )
            );
            $event = new Event(
                (string) $xPerson->name,
                (string) $xPerson->event,
                $date
            );
            if ($event->isWithin($strDate, $nDaysBefore, $nDaysAfter)) {
                $arEvents[] = $event;
            }
        }
        return $arEvents;
    }
}
?>