ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Parameter.php
Go to the documentation of this file.
1<?php
2
3namespace Sabre\VObject;
4
5use ArrayIterator;
6use Sabre\Xml;
7
20class Parameter extends Node {
21
27 public $name;
28
36 public $noName = false;
37
43 protected $value;
44
53 function __construct(Document $root, $name, $value = null) {
54
55 $this->name = strtoupper($name);
56 $this->root = $root;
57 if (is_null($name)) {
58 $this->noName = true;
59 $this->name = static::guessParameterNameByValue($value);
60 }
61
62 // If guessParameterNameByValue() returns an empty string
63 // above, we're actually dealing with a parameter that has no value.
64 // In that case we have to move the value to the name.
65 if ($this->name === '') {
66 $this->noName = false;
67 $this->name = strtoupper($value);
68 } else {
69 $this->setValue($value);
70 }
71
72 }
73
86 switch (strtoupper($value)) {
87
88 // Encodings
89 case '7-BIT' :
90 case 'QUOTED-PRINTABLE' :
91 case 'BASE64' :
92 $name = 'ENCODING';
93 break;
94
95 // Common types
96 case 'WORK' :
97 case 'HOME' :
98 case 'PREF' :
99
100 // Delivery Label Type
101 case 'DOM' :
102 case 'INTL' :
103 case 'POSTAL' :
104 case 'PARCEL' :
105
106 // Telephone types
107 case 'VOICE' :
108 case 'FAX' :
109 case 'MSG' :
110 case 'CELL' :
111 case 'PAGER' :
112 case 'BBS' :
113 case 'MODEM' :
114 case 'CAR' :
115 case 'ISDN' :
116 case 'VIDEO' :
117
118 // EMAIL types (lol)
119 case 'AOL' :
120 case 'APPLELINK' :
121 case 'ATTMAIL' :
122 case 'CIS' :
123 case 'EWORLD' :
124 case 'INTERNET' :
125 case 'IBMMAIL' :
126 case 'MCIMAIL' :
127 case 'POWERSHARE' :
128 case 'PRODIGY' :
129 case 'TLX' :
130 case 'X400' :
131
132 // Photo / Logo format types
133 case 'GIF' :
134 case 'CGM' :
135 case 'WMF' :
136 case 'BMP' :
137 case 'DIB' :
138 case 'PICT' :
139 case 'TIFF' :
140 case 'PDF' :
141 case 'PS' :
142 case 'JPEG' :
143 case 'MPEG' :
144 case 'MPEG2' :
145 case 'AVI' :
146 case 'QTIME' :
147
148 // Sound Digital Audio Type
149 case 'WAVE' :
150 case 'PCM' :
151 case 'AIFF' :
152
153 // Key types
154 case 'X509' :
155 case 'PGP' :
156 $name = 'TYPE';
157 break;
158
159 // Value types
160 case 'INLINE' :
161 case 'URL' :
162 case 'CONTENT-ID' :
163 case 'CID' :
164 $name = 'VALUE';
165 break;
166
167 default:
168 $name = '';
169 }
170
171 return $name;
172 }
173
183 function setValue($value) {
184
185 $this->value = $value;
186
187 }
188
197 function getValue() {
198
199 if (is_array($this->value)) {
200 return implode(',', $this->value);
201 } else {
202 return $this->value;
203 }
204
205 }
206
214 function setParts(array $value) {
215
216 $this->value = $value;
217
218 }
219
227 function getParts() {
228
229 if (is_array($this->value)) {
230 return $this->value;
231 } elseif (is_null($this->value)) {
232 return [];
233 } else {
234 return [$this->value];
235 }
236
237 }
238
249 function addValue($part) {
250
251 if (is_null($this->value)) {
252 $this->value = $part;
253 } else {
254 $this->value = array_merge((array)$this->value, (array)$part);
255 }
256
257 }
258
270 function has($value) {
271
272 return in_array(
273 strtolower($value),
274 array_map('strtolower', (array)$this->value)
275 );
276
277 }
278
284 function serialize() {
285
286 $value = $this->getParts();
287
288 if (count($value) === 0) {
289 return $this->name . '=';
290 }
291
292 if ($this->root->getDocumentType() === Document::VCARD21 && $this->noName) {
293
294 return implode(';', $value);
295
296 }
297
298 return $this->name . '=' . array_reduce(
299 $value,
300 function($out, $item) {
301
302 if (!is_null($out)) $out .= ',';
303
304 // If there's no special characters in the string, we'll use the simple
305 // format.
306 //
307 // The list of special characters is defined as:
308 //
309 // Any character except CONTROL, DQUOTE, ";", ":", ","
310 //
311 // by the iCalendar spec:
312 // https://tools.ietf.org/html/rfc5545#section-3.1
313 //
314 // And we add ^ to that because of:
315 // https://tools.ietf.org/html/rfc6868
316 //
317 // But we've found that iCal (7.0, shipped with OSX 10.9)
318 // severaly trips on + characters not being quoted, so we
319 // added + as well.
320 if (!preg_match('#(?: [\n":;\^,\+] )#x', $item)) {
321 return $out . $item;
322 } else {
323 // Enclosing in double-quotes, and using RFC6868 for encoding any
324 // special characters
325 $out .= '"' . strtr(
326 $item,
327 [
328 '^' => '^^',
329 "\n" => '^n',
330 '"' => '^\'',
331 ]
332 ) . '"';
333 return $out;
334 }
335
336 }
337 );
338
339 }
340
347 function jsonSerialize() {
348
349 return $this->value;
350
351 }
352
361 function xmlSerialize(Xml\Writer $writer) {
362
363 foreach (explode(',', $this->value) as $value) {
364 $writer->writeElement('text', $value);
365 }
366
367 }
368
374 function __toString() {
375
376 return (string)$this->getValue();
377
378 }
379
385 function getIterator() {
386
387 if (!is_null($this->iterator))
388 return $this->iterator;
389
390 return $this->iterator = new ArrayIterator((array)$this->value);
391
392 }
393
394}
An exception for terminatinating execution or to throw for unit testing.
const VCARD21
vCard 2.1.
Definition: Document.php:39
A node is the root class for every element in an iCalendar of vCard object.
Definition: Node.php:19
count()
Returns the number of elements.
Definition: Node.php:177
VObject Parameter.
Definition: Parameter.php:20
__toString()
Called when this object is being cast to a string.
Definition: Parameter.php:374
static guessParameterNameByValue($value)
Try to guess property name by value, can be used for vCard 2.1 nameless parameters.
Definition: Parameter.php:85
__construct(Document $root, $name, $value=null)
Sets up the object.
Definition: Parameter.php:53
has($value)
Checks if this parameter contains the specified value.
Definition: Parameter.php:270
xmlSerialize(Xml\Writer $writer)
This method serializes the data into XML.
Definition: Parameter.php:361
setParts(array $value)
Sets multiple values for this parameter.
Definition: Parameter.php:214
getValue()
Returns the current value.
Definition: Parameter.php:197
getParts()
Returns all values for this parameter.
Definition: Parameter.php:227
jsonSerialize()
This method returns an array, with the representation as it should be encoded in JSON.
Definition: Parameter.php:347
serialize()
Turns the object back into a serialized blob.
Definition: Parameter.php:284
getIterator()
Returns the iterator for this object.
Definition: Parameter.php:385
setValue($value)
Updates the current value.
Definition: Parameter.php:183
addValue($part)
Adds a value to this parameter.
Definition: Parameter.php:249
iCalendar/vCard/jCal/jCard/xCal/xCard writer object.
Definition: Writer.php:17