]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/VObject/Property/MultiDateTime.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / VObject / Property / MultiDateTime.php
1 <?php
2
3 /**
4  * Multi-DateTime property
5  *
6  * This element is used for iCalendar properties such as the EXDATE property.
7  * It basically provides a few helper functions that make it easier to deal
8  * with these. It supports both DATE-TIME and DATE values.
9  *
10  * In order to use this correctly, you must call setDateTimes and getDateTimes
11  * to retrieve and modify dates respectively.
12  *
13  * If you use the 'value' or properties directly, this object does not keep
14  * reference and results might appear incorrectly.
15  *
16  * @package Sabre
17  * @subpackage VObject
18  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
19  * @author Evert Pot (http://www.rooftopsolutions.nl/)
20  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
21  */
22 class Sabre_VObject_Property_MultiDateTime extends Sabre_VObject_Property {
23
24     /**
25      * DateTime representation
26      *
27      * @var DateTime[]
28      */
29     protected $dateTimes;
30
31     /**
32      * dateType
33      *
34      * This is one of the Sabre_VObject_Property_DateTime constants.
35      *
36      * @var int
37      */
38     protected $dateType;
39
40     /**
41      * Updates the value
42      *
43      * @param array $dt Must be an array of DateTime objects.
44      * @param int $dateType
45      * @return void
46      */
47     public function setDateTimes(array $dt, $dateType = Sabre_VObject_Property_DateTime::LOCALTZ) {
48
49         foreach($dt as $i)
50             if (!$i instanceof DateTime)
51                 throw new InvalidArgumentException('You must pass an array of DateTime objects');
52
53         $this->offsetUnset('VALUE');
54         $this->offsetUnset('TZID');
55         switch($dateType) {
56
57             case Sabre_VObject_Property_DateTime::LOCAL :
58                 $val = array();
59                 foreach($dt as $i) {
60                     $val[] = $i->format('Ymd\\THis');
61                 }
62                 $this->setValue(implode(',',$val));
63                 $this->offsetSet('VALUE','DATE-TIME');
64                 break;
65             case Sabre_VObject_Property_DateTime::UTC :
66                 $val = array();
67                 foreach($dt as $i) {
68                     $i->setTimeZone(new DateTimeZone('UTC'));
69                     $val[] = $i->format('Ymd\\THis\\Z');
70                 }
71                 $this->setValue(implode(',',$val));
72                 $this->offsetSet('VALUE','DATE-TIME');
73                 break;
74             case Sabre_VObject_Property_DateTime::LOCALTZ :
75                 $val = array();
76                 foreach($dt as $i) {
77                     $val[] = $i->format('Ymd\\THis');
78                 }
79                 $this->setValue(implode(',',$val));
80                 $this->offsetSet('VALUE','DATE-TIME');
81                 $this->offsetSet('TZID', $dt[0]->getTimeZone()->getName());
82                 break;
83             case Sabre_VObject_Property_DateTime::DATE :
84                 $val = array();
85                 foreach($dt as $i) {
86                     $val[] = $i->format('Ymd');
87                 }
88                 $this->setValue(implode(',',$val));
89                 $this->offsetSet('VALUE','DATE');
90                 break;
91             default :
92                 throw new InvalidArgumentException('You must pass a valid dateType constant');
93
94         }
95         $this->dateTimes = $dt;
96         $this->dateType = $dateType;
97
98     }
99
100     /**
101      * Returns the current DateTime value.
102      *
103      * If no value was set, this method returns null.
104      *
105      * @return array|null
106      */
107     public function getDateTimes() {
108
109         if ($this->dateTimes)
110             return $this->dateTimes;
111
112         $dts = array();
113
114         if (!$this->value) {
115             $this->dateTimes = null;
116             $this->dateType = null;
117             return null;
118         }
119
120         foreach(explode(',',$this->value) as $val) {
121             list(
122                 $type,
123                 $dt
124             ) = Sabre_VObject_Property_DateTime::parseData($val, $this);
125             $dts[] = $dt;
126             $this->dateType = $type;
127         }
128         $this->dateTimes = $dts;
129         return $this->dateTimes;
130
131     }
132
133     /**
134      * Returns the type of Date format.
135      *
136      * This method returns one of the format constants. If no date was set,
137      * this method will return null.
138      *
139      * @return int|null
140      */
141     public function getDateType() {
142
143         if ($this->dateType)
144             return $this->dateType;
145
146         if (!$this->value) {
147             $this->dateTimes = null;
148             $this->dateType = null;
149             return null;
150         }
151
152         $dts = array();
153         foreach(explode(',',$this->value) as $val) {
154             list(
155                 $type,
156                 $dt
157             ) = Sabre_VObject_Property_DateTime::parseData($val, $this);
158             $dts[] = $dt;
159             $this->dateType = $type;
160         }
161         $this->dateTimes = $dts;
162         return $this->dateType;
163
164     }
165
166 }