forked from sabre-io/vobject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recur.php
226 lines (183 loc) · 5.28 KB
/
Recur.php
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace Sabre\VObject\Property\ICalendar;
use Sabre\VObject\Property;
use Sabre\Xml;
/**
* Recur property.
*
* This object represents RECUR properties.
* These values are just used for RRULE and the now deprecated EXRULE.
*
* The RRULE property may look something like this:
*
* RRULE:FREQ=MONTHLY;BYDAY=1,2,3;BYHOUR=5.
*
* This property exposes this as a key=>value array that is accessible using
* getParts, and may be set using setParts.
*
* @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 Recur extends Property {
/**
* Updates the current value.
*
* This may be either a single, or multiple strings in an array.
*
* @param string|array $value
*
* @return void
*/
function setValue($value) {
// If we're getting the data from json, we'll be receiving an object
if ($value instanceof \StdClass) {
$value = (array)$value;
}
if (is_array($value)) {
$newVal = [];
foreach ($value as $k => $v) {
if (is_string($v)) {
$v = strtoupper($v);
// The value had multiple sub-values
if (strpos($v, ',') !== false) {
$v = explode(',', $v);
}
if (strcmp($k, 'until') === 0) {
$v = strtr($v, [':' => '', '-' => '']);
}
} else {
$v = array_map('strtoupper', $v);
}
$newVal[strtoupper($k)] = $v;
}
$this->value = $newVal;
} elseif (is_string($value)) {
$this->value = self::stringToArray($value);
} else {
throw new \InvalidArgumentException('You must either pass a string, or a key=>value array');
}
}
/**
* Returns the current value.
*
* This method will always return a singular value. If this was a
* multi-value object, some decision will be made first on how to represent
* it as a string.
*
* To get the correct multi-value version, use getParts.
*
* @return string
*/
function getValue() {
$out = [];
foreach ($this->value as $key => $value) {
$out[] = $key . '=' . (is_array($value) ? implode(',', $value) : $value);
}
return strtoupper(implode(';', $out));
}
/**
* Sets a multi-valued property.
*
* @param array $parts
* @return void
*/
function setParts(array $parts) {
$this->setValue($parts);
}
/**
* Returns a multi-valued property.
*
* This method always returns an array, if there was only a single value,
* it will still be wrapped in an array.
*
* @return array
*/
function getParts() {
return $this->value;
}
/**
* Sets a raw value coming from a mimedir (iCalendar/vCard) file.
*
* This has been 'unfolded', so only 1 line will be passed. Unescaping is
* not yet done, but parameters are not included.
*
* @param string $val
*
* @return void
*/
function setRawMimeDirValue($val) {
$this->setValue($val);
}
/**
* Returns a raw mime-dir representation of the value.
*
* @return string
*/
function getRawMimeDirValue() {
return $this->getValue();
}
/**
* Returns the type of value.
*
* This corresponds to the VALUE= parameter. Every property also has a
* 'default' valueType.
*
* @return string
*/
function getValueType() {
return 'RECUR';
}
/**
* Returns the value, in the format it should be encoded for json.
*
* This method must always return an array.
*
* @return array
*/
function getJsonValue() {
$values = [];
foreach ($this->getParts() as $k => $v) {
$values[strtolower($k)] = $v;
}
return [$values];
}
/**
* This method serializes only the value of a property. This is used to
* create xCard or xCal documents.
*
* @param Xml\Writer $writer XML writer.
*
* @return void
*/
protected function xmlSerializeValue(Xml\Writer $writer) {
$valueType = strtolower($this->getValueType());
foreach ($this->getJsonValue() as $value) {
$writer->writeElement($valueType, $value);
}
}
/**
* Parses an RRULE value string, and turns it into a struct-ish array.
*
* @param string $value
*
* @return array
*/
static function stringToArray($value) {
$value = strtoupper($value);
$newValue = [];
foreach (explode(';', $value) as $part) {
// Skipping empty parts.
if (empty($part)) {
continue;
}
list($partName, $partValue) = explode('=', $part);
// The value itself had multiple values..
if (strpos($partValue, ',') !== false) {
$partValue = explode(',', $partValue);
}
$newValue[$partName] = $partValue;
}
return $newValue;
}
}