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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
<?php
namespace Sabre\VObject;
use ArrayIterator;
use Sabre\Xml;
/**
* VObject Parameter.
*
* This class represents a parameter. A parameter is always tied to a property.
* In the case of:
* DTSTART;VALUE=DATE:20101108
* VALUE=DATE would be the parameter name and value.
*
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class Parameter extends Node
{
/**
* Parameter name.
*
* @var string
*/
public $name;
/**
* vCard 2.1 allows parameters to be encoded without a name.
*
* We can deduce the parameter name based on its value.
*
* @var bool
*/
public $noName = false;
/**
* Parameter value.
*
* @var string
*/
protected $value;
/**
* Sets up the object.
*
* It's recommended to use the create:: factory method instead.
*
* @param string $name
* @param string $value
*/
public function __construct(Document $root, $name, $value = null)
{
$this->root = $root;
if (is_null($name)) {
$this->noName = true;
$this->name = static::guessParameterNameByValue($value);
} else {
$this->name = strtoupper($name);
}
// If guessParameterNameByValue() returns an empty string
// above, we're actually dealing with a parameter that has no value.
// In that case we have to move the value to the name.
if ('' === $this->name) {
$this->noName = false;
$this->name = strtoupper($value);
} else {
$this->setValue($value);
}
}
/**
* Try to guess property name by value, can be used for vCard 2.1 nameless parameters.
*
* Figuring out what the name should have been. Note that a ton of
* these are rather silly in 2014 and would probably rarely be
* used, but we like to be complete.
*
* @param string $value
*
* @return string
*/
public static function guessParameterNameByValue($value)
{
switch (strtoupper($value)) {
// Encodings
case '7-BIT':
case 'QUOTED-PRINTABLE':
case 'BASE64':
$name = 'ENCODING';
break;
// Common types
case 'WORK':
case 'HOME':
case 'PREF':
// Delivery Label Type
case 'DOM':
case 'INTL':
case 'POSTAL':
case 'PARCEL':
// Telephone types
case 'VOICE':
case 'FAX':
case 'MSG':
case 'CELL':
case 'PAGER':
case 'BBS':
case 'MODEM':
case 'CAR':
case 'ISDN':
case 'VIDEO':
// EMAIL types (lol)
case 'AOL':
case 'APPLELINK':
case 'ATTMAIL':
case 'CIS':
case 'EWORLD':
case 'INTERNET':
case 'IBMMAIL':
case 'MCIMAIL':
case 'POWERSHARE':
case 'PRODIGY':
case 'TLX':
case 'X400':
// Photo / Logo format types
case 'GIF':
case 'CGM':
case 'WMF':
case 'BMP':
case 'DIB':
case 'PICT':
case 'TIFF':
case 'PDF':
case 'PS':
case 'JPEG':
case 'MPEG':
case 'MPEG2':
case 'AVI':
case 'QTIME':
// Sound Digital Audio Type
case 'WAVE':
case 'PCM':
case 'AIFF':
// Key types
case 'X509':
case 'PGP':
$name = 'TYPE';
break;
// Value types
case 'INLINE':
case 'URL':
case 'CONTENT-ID':
case 'CID':
$name = 'VALUE';
break;
default:
$name = '';
}
return $name;
}
/**
* Updates the current value.
*
* This may be either a single, or multiple strings in an array.
*
* @param string|array $value
*/
public function setValue($value)
{
$this->value = $value;
}
/**
* Returns the current value.
*
* This method will always return a string, or null. If there were multiple
* values, it will automatically concatenate them (separated by comma).
*
* @return string|null
*/
public function getValue()
{
if (is_array($this->value)) {
return implode(',', $this->value);
} else {
return $this->value;
}
}
/**
* Sets multiple values for this parameter.
*/
public function setParts(array $value)
{
$this->value = $value;
}
/**
* Returns all values for this parameter.
*
* If there were no values, an empty array will be returned.
*
* @return array
*/
public function getParts()
{
if (is_array($this->value)) {
return $this->value;
} elseif (is_null($this->value)) {
return [];
} else {
return [$this->value];
}
}
/**
* Adds a value to this parameter.
*
* If the argument is specified as an array, all items will be added to the
* parameter value list.
*
* @param string|array $part
*/
public function addValue($part)
{
if (is_null($this->value)) {
$this->value = $part;
} else {
$this->value = array_merge((array) $this->value, (array) $part);
}
}
/**
* Checks if this parameter contains the specified value.
*
* This is a case-insensitive match. It makes sense to call this for for
* instance the TYPE parameter, to see if it contains a keyword such as
* 'WORK' or 'FAX'.
*
* @param string $value
*
* @return bool
*/
public function has($value)
{
return in_array(
strtolower($value),
array_map('strtolower', (array) $this->value)
);
}
/**
* Turns the object back into a serialized blob.
*
* @return string
*/
public function serialize()
{
$value = $this->getParts();
if (0 === count($value)) {
return $this->name.'=';
}
if (Document::VCARD21 === $this->root->getDocumentType() && $this->noName) {
return implode(';', $value);
}
return $this->name.'='.array_reduce(
$value,
function ($out, $item) {
if (!is_null($out)) {
$out .= ',';
}
// If there's no special characters in the string, we'll use the simple
// format.
//
// The list of special characters is defined as:
//
// Any character except CONTROL, DQUOTE, ";", ":", ","
//
// by the iCalendar spec:
// https://tools.ietf.org/html/rfc5545#section-3.1
//
// And we add ^ to that because of:
// https://tools.ietf.org/html/rfc6868
//
// But we've found that iCal (7.0, shipped with OSX 10.9)
// severely trips on + characters not being quoted, so we
// added + as well.
if (!preg_match('#(?: [\n":;\^,\+] )#x', $item)) {
return $out.$item;
} else {
// Enclosing in double-quotes, and using RFC6868 for encoding any
// special characters
$out .= '"'.strtr(
$item,
[
'^' => '^^',
"\n" => '^n',
'"' => '^\'',
]
).'"';
return $out;
}
}
);
}
/**
* This method returns an array, with the representation as it should be
* encoded in JSON. This is used to create jCard or jCal documents.
*
* @return array
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->value;
}
/**
* This method serializes the data into XML. This is used to create xCard or
* xCal documents.
*
* @param Xml\Writer $writer XML writer
*/
public function xmlSerialize(Xml\Writer $writer)
{
foreach (explode(',', $this->value) as $value) {
$writer->writeElement('text', $value);
}
}
/**
* Called when this object is being cast to a string.
*
* @return string
*/
public function __toString()
{
return (string) $this->getValue();
}
/**
* Returns the iterator for this object.
*
* @return ElementList
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
if (!is_null($this->iterator)) {
return $this->iterator;
}
return $this->iterator = new ArrayIterator((array) $this->value);
}
}
|