add docblocks to all files, classes, methods and variables
[bdrem.git] / src / bdrem / Source / Bdf.php
1 <?php
2 /**
3  * Part of bdrem
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Bdrem
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/bdrem.htm
13  */
14 namespace bdrem;
15
16 /**
17  * Reads birthday reminder 2's birthday files (.bdf).
18  *
19  * @category  Tools
20  * @package   Bdrem
21  * @author    Christian Weiske <cweiske@cweiske.de>
22  * @copyright 2014 Christian Weiske
23  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
24  * @link      http://cweiske.de/bdrem.htm
25  */
26 class Source_Bdf
27 {
28     /**
29      * Full path of bdf birthday file
30      * @var string
31      */
32     protected $filename;
33
34     /**
35      * Set the birthday file name
36      *
37      * @param string $filename Path to bdf file
38      */
39     public function __construct($filename)
40     {
41         $this->filename = $filename;
42         if (!file_exists($this->filename)) {
43             throw new \Exception(
44                 'Birthday file does not exist: ' . $this->filename
45             );
46         }
47     }
48
49     /**
50      * Return all events for the given date range
51      *
52      * @param string  $strDate       Date the events shall be found for,
53      *                               YYYY-MM-DD
54      * @param integer $nDaysPrevious Include number of days before $strDate
55      * @param integer $nDaysNext     Include number of days after $strDate
56      *
57      * @return Event[] Array of matching event objects
58      */
59     public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
60     {
61         $x = simplexml_load_file($this->filename);
62
63         $arEvents = array();
64         foreach ($x->content->person as $xPerson) {
65             $date = implode(
66                 '-',
67                 array_reverse(
68                     explode('.', (string) $xPerson->date)
69                 )
70             );
71             $event = new Event(
72                 (string) $xPerson->name,
73                 (string) $xPerson->event,
74                 $date
75             );
76             if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
77                 $arEvents[] = $event;
78             }
79         }
80         return $arEvents;
81     }
82 }
83 ?>