Update phing build script to use composer installation only
[bdrem.git] / src / bdrem / Source / vCard.php
1 <?php
2 /**
3  * Part of bdrem
4  *
5  * PHP version 7
6  *
7  * @category  Tools
8  * @package   Bdrem
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2023 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 use Sabre\VObject;
17 use Sabre\VObject\Component\VCard;
18
19 /**
20  * Read a folder of vcard files
21  * 2 subfolder levels are supported.
22  *
23  * @category  Tools
24  * @package   Bdrem
25  * @author    Christian Weiske <cweiske@cweiske.de>
26  * @copyright 2023 Christian Weiske
27  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
28  * @link      http://cweiske.de/bdrem.htm
29  */
30 class Source_vCard
31 {
32     /**
33      * Full path to a folder with .vcf files
34      */
35     protected string $folder;
36
37     /**
38      * Set the VCard folder path
39      */
40     public function __construct($config)
41     {
42         if (is_string($config)) {
43             $config = array(
44                 'folder' => $config
45             );
46         }
47         $this->folder = $config['folder'];
48         if (!is_dir($this->folder)) {
49             throw new \Exception(
50                 'VCard folder does not exist: ' . $this->folder
51             );
52         }
53     }
54
55     /**
56      * Return all events for the given date range
57      *
58      * @param string  $strDate       Date the events shall be found for,
59      *                               YYYY-MM-DD
60      * @param integer $nDaysPrevious Include number of days before $strDate
61      * @param integer $nDaysNext     Include number of days after $strDate
62      *
63      * @return Event[] Array of matching event objects
64      */
65     public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
66     {
67         $vcfFiles = glob($this->folder . '/{*,*/*,*/*/*}.vcf', GLOB_BRACE);
68         if (count($vcfFiles) == 0) {
69             throw new \Exception('No .vcf files found in folder');
70         }
71
72         $arEvents = [];
73         foreach ($vcfFiles as $vcfFile) {
74             $vcard = VObject\Reader::read(file_get_contents($vcfFile));
75
76             if (isset($vcard->BDAY)) {
77                 $event = new Event(
78                     $this->getName($vcard),
79                     'Birthday',
80                     $vcard->BDAY->getDateTime()->format('Y-m-d')
81                 );
82                 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
83                     $arEvents[] = $event;
84                 }
85             }
86
87             if (isset($vcard->{'X-ANNIVERSARY'})) {
88                 $event = new Event(
89                     $this->getName($vcard),
90                     'Anniversary',
91                     $vcard->{'X-ANNIVERSARY'}->getDateTime()->format('Y-m-d')
92                 );
93                 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
94                     $arEvents[] = $event;
95                 }
96             }
97         }
98
99         return $arEvents;
100     }
101
102     protected function getName(VCard $vcard)
103     {
104         return (string) $vcard->FN;
105     }
106 }
107 ?>