aboutsummaryrefslogtreecommitdiffstats
path: root/libcalendaring/lib/OldSabre/VObject/Property/MultiDateTime.php
blob: 45ee81118b3d5e3e6804994216796338332ae3fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php

namespace OldSabre\VObject\Property;

use OldSabre\VObject;

/**
 * Multi-DateTime property
 *
 * This element is used for iCalendar properties such as the EXDATE property.
 * It basically provides a few helper functions that make it easier to deal
 * with these. It supports both DATE-TIME and DATE values.
 *
 * In order to use this correctly, you must call setDateTimes and getDateTimes
 * to retrieve and modify dates respectively.
 *
 * If you use the 'value' or properties directly, this object does not keep
 * reference and results might appear incorrectly.
 *
 * @copyright Copyright (C) 2011-2015 fruux GmbH (https://fruux.com/).
 * @author Evert Pot (http://evertpot.com/)
 * @license http://sabre.io/license/ Modified BSD License
 */
class MultiDateTime extends VObject\Property {

    /**
     * DateTime representation
     *
     * @var DateTime[]
     */
    protected $dateTimes;

    /**
     * dateType
     *
     * This is one of the OldSabre\VObject\Property\DateTime constants.
     *
     * @var int
     */
    protected $dateType;

    /**
     * Updates the value
     *
     * @param array $dt Must be an array of DateTime objects.
     * @param int $dateType
     * @return void
     */
    public function setDateTimes(array $dt, $dateType = VObject\Property\DateTime::LOCALTZ) {

        foreach($dt as $i)
            if (!$i instanceof \DateTime)
                throw new \InvalidArgumentException('You must pass an array of DateTime objects');

        $this->offsetUnset('VALUE');
        $this->offsetUnset('TZID');
        switch($dateType) {

            case DateTime::LOCAL :
                $val = array();
                foreach($dt as $i) {
                    $val[] = $i->format('Ymd\\THis');
                }
                $this->setValue(implode(',',$val));
                $this->offsetSet('VALUE','DATE-TIME');
                break;
            case DateTime::UTC :
                $val = array();
                foreach($dt as $i) {
                    $i->setTimeZone(new \DateTimeZone('UTC'));
                    $val[] = $i->format('Ymd\\THis\\Z');
                }
                $this->setValue(implode(',',$val));
                $this->offsetSet('VALUE','DATE-TIME');
                break;
            case DateTime::LOCALTZ :
                $val = array();
                foreach($dt as $i) {
                    $val[] = $i->format('Ymd\\THis');
                }
                $this->setValue(implode(',',$val));
                $this->offsetSet('VALUE','DATE-TIME');
                $this->offsetSet('TZID', $dt[0]->getTimeZone()->getName());
                break;
            case DateTime::DATE :
                $val = array();
                foreach($dt as $i) {
                    $val[] = $i->format('Ymd');
                }
                $this->setValue(implode(',',$val));
                $this->offsetSet('VALUE','DATE');
                break;
            default :
                throw new \InvalidArgumentException('You must pass a valid dateType constant');

        }
        $this->dateTimes = $dt;
        $this->dateType = $dateType;

    }

    /**
     * Returns the current DateTime value.
     *
     * If no value was set, this method returns null.
     *
     * @return array|null
     */
    public function getDateTimes() {

        if ($this->dateTimes)
            return $this->dateTimes;

        $dts = array();

        if (!$this->value) {
            $this->dateTimes = null;
            $this->dateType = null;
            return null;
        }

        foreach(explode(',',$this->value) as $val) {
            list(
                $type,
                $dt
            ) = DateTime::parseData($val, $this);
            $dts[] = $dt;
            $this->dateType = $type;
        }
        $this->dateTimes = $dts;
        return $this->dateTimes;

    }

    /**
     * Returns the type of Date format.
     *
     * This method returns one of the format constants. If no date was set,
     * this method will return null.
     *
     * @return int|null
     */
    public function getDateType() {

        if ($this->dateType)
            return $this->dateType;

        if (!$this->value) {
            $this->dateTimes = null;
            $this->dateType = null;
            return null;
        }

        $dts = array();
        foreach(explode(',',$this->value) as $val) {
            list(
                $type,
                $dt
            ) = DateTime::parseData($val, $this);
            $dts[] = $dt;
            $this->dateType = $type;
        }
        $this->dateTimes = $dts;
        return $this->dateType;

    }

    /**
     * This method will return true, if the property had a date and a time, as
     * opposed to only a date.
     *
     * @return bool
     */
    public function hasTime() {

        return $this->getDateType()!==DateTime::DATE;

    }

}

© 2014-2024 Faster IT GmbH | imprint | privacy policy