ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DateTime.php
Go to the documentation of this file.
1<?php
2
4
5use DateTimeInterface;
6use DateTimeZone;
11
27class DateTime extends Property {
28
35 public $delimiter = ',';
36
46 function setParts(array $parts) {
47
48 if (isset($parts[0]) && $parts[0] instanceof DateTimeInterface) {
49 $this->setDateTimes($parts);
50 } else {
51 parent::setParts($parts);
52 }
53
54 }
55
67 function setValue($value) {
68
69 if (is_array($value) && isset($value[0]) && $value[0] instanceof DateTimeInterface) {
70 $this->setDateTimes($value);
71 } elseif ($value instanceof DateTimeInterface) {
72 $this->setDateTimes([$value]);
73 } else {
74 parent::setValue($value);
75 }
76
77 }
78
89 function setRawMimeDirValue($val) {
90
91 $this->setValue(explode($this->delimiter, $val));
92
93 }
94
101
102 return implode($this->delimiter, $this->getParts());
103
104 }
105
111 function hasTime() {
112
113 return strtoupper((string)$this['VALUE']) !== 'DATE';
114
115 }
116
122 function isFloating() {
123
124 return
125 !$this->hasTime() ||
126 (
127 !isset($this['TZID']) &&
128 strpos($this->getValue(), 'Z') === false
129 );
130
131 }
132
148 function getDateTime(DateTimeZone $timeZone = null) {
149
150 $dt = $this->getDateTimes($timeZone);
151 if (!$dt) return;
152
153 return $dt[0];
154
155 }
156
169 function getDateTimes(DateTimeZone $timeZone = null) {
170
171 // Does the property have a TZID?
172 $tzid = $this['TZID'];
173
174 if ($tzid) {
175 $timeZone = TimeZoneUtil::getTimeZone((string)$tzid, $this->root);
176 }
177
178 $dts = [];
179 foreach ($this->getParts() as $part) {
180 $dts[] = DateTimeParser::parse($part, $timeZone);
181 }
182 return $dts;
183
184 }
185
194 function setDateTime(DateTimeInterface $dt, $isFloating = false) {
195
196 $this->setDateTimes([$dt], $isFloating);
197
198 }
199
211 function setDateTimes(array $dt, $isFloating = false) {
212
213 $values = [];
214
215 if ($this->hasTime()) {
216
217 $tz = null;
218 $isUtc = false;
219
220 foreach ($dt as $d) {
221
222 if ($isFloating) {
223 $values[] = $d->format('Ymd\\THis');
224 continue;
225 }
226 if (is_null($tz)) {
227 $tz = $d->getTimeZone();
228 $isUtc = in_array($tz->getName(), ['UTC', 'GMT', 'Z', '+00:00']);
229 if (!$isUtc) {
230 $this->offsetSet('TZID', $tz->getName());
231 }
232 } else {
233 $d = $d->setTimeZone($tz);
234 }
235
236 if ($isUtc) {
237 $values[] = $d->format('Ymd\\THis\\Z');
238 } else {
239 $values[] = $d->format('Ymd\\THis');
240 }
241
242 }
243 if ($isUtc || $isFloating) {
244 $this->offsetUnset('TZID');
245 }
246
247 } else {
248
249 foreach ($dt as $d) {
250
251 $values[] = $d->format('Ymd');
252
253 }
254 $this->offsetUnset('TZID');
255
256 }
257
258 $this->value = $values;
259
260 }
261
270 function getValueType() {
271
272 return $this->hasTime() ? 'DATE-TIME' : 'DATE';
273
274 }
275
283 function getJsonValue() {
284
285 $dts = $this->getDateTimes();
286 $hasTime = $this->hasTime();
287 $isFloating = $this->isFloating();
288
289 $tz = $dts[0]->getTimeZone();
290 $isUtc = $isFloating ? false : in_array($tz->getName(), ['UTC', 'GMT', 'Z']);
291
292 return array_map(
293 function(DateTimeInterface $dt) use ($hasTime, $isUtc) {
294
295 if ($hasTime) {
296 return $dt->format('Y-m-d\\TH:i:s') . ($isUtc ? 'Z' : '');
297 } else {
298 return $dt->format('Y-m-d');
299 }
300
301 },
302 $dts
303 );
304
305 }
306
316 function setJsonValue(array $value) {
317
318 // dates and times in jCal have one difference to dates and times in
319 // iCalendar. In jCal date-parts are separated by dashes, and
320 // time-parts are separated by colons. It makes sense to just remove
321 // those.
322 $this->setValue(
323 array_map(
324 function($item) {
325
326 return strtr($item, [':' => '', '-' => '']);
327
328 },
329 $value
330 )
331 );
332
333 }
334
344 function offsetSet($name, $value) {
345
346 parent::offsetSet($name, $value);
347 if (strtoupper($name) !== 'VALUE') {
348 return;
349 }
350
351 // This will ensure that dates are correctly encoded.
352 $this->setDateTimes($this->getDateTimes());
353
354 }
355
378 function validate($options = 0) {
379
380 $messages = parent::validate($options);
381 $valueType = $this->getValueType();
382 $values = $this->getParts();
383 try {
384 foreach ($values as $value) {
385 switch ($valueType) {
386 case 'DATE' :
388 break;
389 case 'DATE-TIME' :
391 break;
392 }
393 }
394 } catch (InvalidDataException $e) {
395 $messages[] = [
396 'level' => 3,
397 'message' => 'The supplied value (' . $value . ') is not a correct ' . $valueType,
398 'node' => $this,
399 ];
400 }
401 return $messages;
402
403 }
404}
An exception for terminatinating execution or to throw for unit testing.
static parse($date, $referenceTz=null)
Parses either a Date or DateTime, or Duration value.
static parseDateTime($dt, DateTimeZone $tz=null)
Parses an iCalendar (rfc5545) formatted datetime and returns a DateTimeImmutable object.
static parseDate($date, DateTimeZone $tz=null)
Parses an iCalendar (rfc5545) formatted date and returns a DateTimeImmutable object.
This exception is thrown whenever an invalid value is found anywhere in a iCalendar or vCard object.
setDateTimes(array $dt, $isFloating=false)
Sets the property as multiple date-time objects.
Definition: DateTime.php:211
getJsonValue()
Returns the value, in the format it should be encoded for JSON.
Definition: DateTime.php:283
getDateTime(DateTimeZone $timeZone=null)
Returns a date-time value.
Definition: DateTime.php:148
getValueType()
Returns the type of value.
Definition: DateTime.php:270
setValue($value)
Updates the current value.
Definition: DateTime.php:67
setDateTime(DateTimeInterface $dt, $isFloating=false)
Sets the property as a DateTime object.
Definition: DateTime.php:194
hasTime()
Returns true if this is a DATE-TIME value, false if it's a DATE.
Definition: DateTime.php:111
offsetSet($name, $value)
We need to intercept offsetSet, because it may be used to alter the VALUE from DATE-TIME to DATE or v...
Definition: DateTime.php:344
validate($options=0)
Validates the node for correctness.
Definition: DateTime.php:378
isFloating()
Returns true if this is a floating DATE or DATE-TIME.
Definition: DateTime.php:122
getRawMimeDirValue()
Returns a raw mime-dir representation of the value.
Definition: DateTime.php:100
setJsonValue(array $value)
Sets the json value, as it would appear in a jCard or jCal object.
Definition: DateTime.php:316
getDateTimes(DateTimeZone $timeZone=null)
Returns multiple date-time values.
Definition: DateTime.php:169
setParts(array $parts)
Sets a multi-valued property.
Definition: DateTime.php:46
setRawMimeDirValue($val)
Sets a raw value coming from a mimedir (iCalendar/vCard) file.
Definition: DateTime.php:89
offsetUnset($name)
Removes one or more parameters with the specified name.
Definition: Property.php:497
getParts()
Returns a multi-valued property.
Definition: Property.php:152
getValue()
Returns the current value.
Definition: Property.php:115
Time zone name translation.
static getTimeZone($tzid, Component $vcalendar=null, $failIfUncertain=false)
This method will try to find out the correct timezone for an iCalendar date-time value.
for( $i=6;$i< 13;$i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
$messages
Definition: en.php:5
$values