]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/VObject/Property.php
Move friendica-specific parts into an own subdirectory
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / VObject / Property.php
1 <?php
2
3 /**
4  * VObject Property
5  *
6  * A property in VObject is usually in the form PARAMNAME:paramValue.
7  * An example is : SUMMARY:Weekly meeting
8  *
9  * Properties can also have parameters:
10  * SUMMARY;LANG=en:Weekly meeting.
11  *
12  * Parameters can be accessed using the ArrayAccess interface.
13  *
14  * @package Sabre
15  * @subpackage VObject
16  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
17  * @author Evert Pot (http://www.rooftopsolutions.nl/)
18  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
19  */
20 class Sabre_VObject_Property extends Sabre_VObject_Element {
21
22     /**
23      * Propertyname
24      *
25      * @var string
26      */
27     public $name;
28
29     /**
30      * Group name
31      *
32      * This may be something like 'HOME' for vcards.
33      *
34      * @var string
35      */
36     public $group;
37
38     /**
39      * Property parameters
40      *
41      * @var array
42      */
43     public $parameters = array();
44
45     /**
46      * Property value
47      *
48      * @var string
49      */
50     public $value;
51
52     /**
53      * If properties are added to this map, they will be automatically mapped
54      * to their respective classes, if parsed by the reader or constructed with
55      * the 'create' method.
56      *
57      * @var array
58      */
59     static public $classMap = array(
60         'COMPLETED'     => 'Sabre_VObject_Property_DateTime',
61         'CREATED'       => 'Sabre_VObject_Property_DateTime',
62         'DTEND'         => 'Sabre_VObject_Property_DateTime',
63         'DTSTAMP'       => 'Sabre_VObject_Property_DateTime',
64         'DTSTART'       => 'Sabre_VObject_Property_DateTime',
65         'DUE'           => 'Sabre_VObject_Property_DateTime',
66         'EXDATE'        => 'Sabre_VObject_Property_MultiDateTime',
67         'LAST-MODIFIED' => 'Sabre_VObject_Property_DateTime',
68         'RECURRENCE-ID' => 'Sabre_VObject_Property_DateTime',
69         'TRIGGER'       => 'Sabre_VObject_Property_DateTime',
70     );
71
72     /**
73      * Creates the new property by name, but in addition will also see if
74      * there's a class mapped to the property name.
75      *
76      * @param string $name
77      * @param string $value
78      * @return Sabre_VObject_Property
79      */
80     static public function create($name, $value = null) {
81
82         $name = strtoupper($name);
83         $shortName = $name;
84         $group = null;
85         if (strpos($shortName,'.')!==false) {
86             list($group, $shortName) = explode('.', $shortName);
87         }
88
89         if (isset(self::$classMap[$shortName])) {
90             return new self::$classMap[$shortName]($name, $value);
91         } else {
92             return new self($name, $value);
93         }
94
95     }
96
97     /**
98      * Creates a new property object
99      *
100      * By default this object will iterate over its own children, but this can
101      * be overridden with the iterator argument
102      *
103      * @param string $name
104      * @param string $value
105      * @param Sabre_VObject_ElementList $iterator
106      */
107     public function __construct($name, $value = null, $iterator = null) {
108
109         $name = strtoupper($name);
110         $group = null;
111         if (strpos($name,'.')!==false) {
112             list($group, $name) = explode('.', $name);
113         }
114         $this->name = $name;
115         $this->group = $group;
116         if (!is_null($iterator)) $this->iterator = $iterator;
117         $this->setValue($value);
118
119     }
120
121
122
123     /**
124      * Updates the internal value
125      *
126      * @param string $value
127      * @return void
128      */
129     public function setValue($value) {
130
131         $this->value = $value;
132
133     }
134
135     /**
136      * Turns the object back into a serialized blob.
137      *
138      * @return string
139      */
140     public function serialize() {
141
142         $str = $this->name;
143         if ($this->group) $str = $this->group . '.' . $this->name;
144
145         if (count($this->parameters)) {
146             foreach($this->parameters as $param) {
147
148                 $str.=';' . $param->serialize();
149
150             }
151         }
152         $src = array(
153             '\\',
154             "\n",
155         );
156         $out = array(
157             '\\\\',
158             '\n',
159         );
160         $str.=':' . str_replace($src, $out, $this->value);
161
162         $out = '';
163         while(strlen($str)>0) {
164             if (strlen($str)>75) {
165                 $out.= mb_strcut($str,0,75,'utf-8') . "\r\n";
166                 $str = ' ' . mb_strcut($str,75,strlen($str),'utf-8');
167             } else {
168                 $out.=$str . "\r\n";
169                 $str='';
170                 break;
171             }
172         }
173
174         return $out;
175
176     }
177
178     /**
179      * Adds a new componenten or element
180      *
181      * You can call this method with the following syntaxes:
182      *
183      * add(Sabre_VObject_Parameter $element)
184      * add(string $name, $value)
185      *
186      * The first version adds an Parameter
187      * The second adds a property as a string.
188      *
189      * @param mixed $item
190      * @param mixed $itemValue
191      * @return void
192      */
193     public function add($item, $itemValue = null) {
194
195         if ($item instanceof Sabre_VObject_Parameter) {
196             if (!is_null($itemValue)) {
197                 throw new InvalidArgumentException('The second argument must not be specified, when passing a VObject');
198             }
199             $item->parent = $this;
200             $this->parameters[] = $item;
201         } elseif(is_string($item)) {
202
203             if (!is_scalar($itemValue) && !is_null($itemValue)) {
204                 throw new InvalidArgumentException('The second argument must be scalar');
205             }
206             $parameter = new Sabre_VObject_Parameter($item,$itemValue);
207             $parameter->parent = $this;
208             $this->parameters[] = $parameter;
209
210         } else {
211
212             throw new InvalidArgumentException('The first argument must either be a Sabre_VObject_Element or a string');
213
214         }
215
216     }
217
218     /* ArrayAccess interface {{{ */
219
220     /**
221      * Checks if an array element exists
222      *
223      * @param mixed $name
224      * @return bool
225      */
226     public function offsetExists($name) {
227
228         if (is_int($name)) return parent::offsetExists($name);
229
230         $name = strtoupper($name);
231
232         foreach($this->parameters as $parameter) {
233             if ($parameter->name == $name) return true;
234         }
235         return false;
236
237     }
238
239     /**
240      * Returns a parameter, or parameter list.
241      *
242      * @param string $name
243      * @return Sabre_VObject_Element
244      */
245     public function offsetGet($name) {
246
247         if (is_int($name)) return parent::offsetGet($name);
248         $name = strtoupper($name);
249
250         $result = array();
251         foreach($this->parameters as $parameter) {
252             if ($parameter->name == $name)
253                 $result[] = $parameter;
254         }
255
256         if (count($result)===0) {
257             return null;
258         } elseif (count($result)===1) {
259             return $result[0];
260         } else {
261             $result[0]->setIterator(new Sabre_VObject_ElementList($result));
262             return $result[0];
263         }
264
265     }
266
267     /**
268      * Creates a new parameter
269      *
270      * @param string $name
271      * @param mixed $value
272      * @return void
273      */
274     public function offsetSet($name, $value) {
275
276         if (is_int($name)) parent::offsetSet($name, $value);
277
278         if (is_scalar($value)) {
279             if (!is_string($name))
280                 throw new InvalidArgumentException('A parameter name must be specified. This means you cannot use the $array[]="string" to add parameters.');
281
282             $this->offsetUnset($name);
283             $parameter = new Sabre_VObject_Parameter($name, $value);
284             $parameter->parent = $this;
285             $this->parameters[] = $parameter;
286
287         } elseif ($value instanceof Sabre_VObject_Parameter) {
288             if (!is_null($name))
289                 throw new InvalidArgumentException('Don\'t specify a parameter name if you\'re passing a Sabre_VObject_Parameter. Add using $array[]=$parameterObject.');
290
291             $value->parent = $this;
292             $this->parameters[] = $value;
293         } else {
294             throw new InvalidArgumentException('You can only add parameters to the property object');
295         }
296
297     }
298
299     /**
300      * Removes one or more parameters with the specified name
301      *
302      * @param string $name
303      * @return void
304      */
305     public function offsetUnset($name) {
306
307         if (is_int($name)) parent::offsetUnset($name);
308         $name = strtoupper($name);
309
310         foreach($this->parameters as $key=>$parameter) {
311             if ($parameter->name == $name) {
312                 $parameter->parent = null;
313                 unset($this->parameters[$key]);
314             }
315
316         }
317
318     }
319
320     /* }}} */
321
322     /**
323      * Called when this object is being cast to a string
324      *
325      * @return string
326      */
327     public function __toString() {
328
329         return (string)$this->value;
330
331     }
332
333     /**
334      * This method is automatically called when the object is cloned.
335      * Specifically, this will ensure all child elements are also cloned.
336      *
337      * @return void
338      */
339     public function __clone() {
340
341         foreach($this->parameters as $key=>$child) {
342             $this->parameters[$key] = clone $child;
343             $this->parameters[$key]->parent = $this;
344         }
345
346     }
347
348 }