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