annotate plugins/libcalendaring/lib/Sabre/VObject/Component.php @ 4:888e774ee983

libcalendar plugin as distributed
author Charlie Root
date Sat, 13 Jan 2018 08:57:56 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1 <?php
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
2
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
3 namespace Sabre\VObject;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
4
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
5 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
6 * VObject Component
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
7 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
8 * This class represents a VCALENDAR/VCARD component. A component is for example
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
9 * VEVENT, VTODO and also VCALENDAR. It starts with BEGIN:COMPONENTNAME and
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
10 * ends with END:COMPONENTNAME
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
11 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
12 * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
13 * @author Evert Pot (http://evertpot.com/)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
14 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
15 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
16 class Component extends Node {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
17
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
18 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
19 * Name, for example VEVENT
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
20 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
21 * @var string
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
22 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
23 public $name;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
24
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
25 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
26 * Children properties and components
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
27 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
28 * @var array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
29 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
30 public $children = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
31
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
32 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
33 * If components are added to this map, they will be automatically mapped
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
34 * to their respective classes, if parsed by the reader or constructed with
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
35 * the 'create' method.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
36 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
37 * @var array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
38 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
39 static public $classMap = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
40 'VALARM' => 'Sabre\\VObject\\Component\\VAlarm',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
41 'VCALENDAR' => 'Sabre\\VObject\\Component\\VCalendar',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
42 'VCARD' => 'Sabre\\VObject\\Component\\VCard',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
43 'VEVENT' => 'Sabre\\VObject\\Component\\VEvent',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
44 'VJOURNAL' => 'Sabre\\VObject\\Component\\VJournal',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
45 'VTODO' => 'Sabre\\VObject\\Component\\VTodo',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
46 'VFREEBUSY' => 'Sabre\\VObject\\Component\\VFreeBusy',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
47 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
48
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
49 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
50 * Creates the new component by name, but in addition will also see if
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
51 * there's a class mapped to the property name.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
52 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
53 * @param string $name
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
54 * @param string $value
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
55 * @return Component
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
56 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
57 static public function create($name, $value = null) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
58
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
59 $name = strtoupper($name);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
60
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
61 if (isset(self::$classMap[$name])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
62 return new self::$classMap[$name]($name, $value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
63 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
64 return new self($name, $value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
65 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
66
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
67 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
68
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
69 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
70 * Creates a new component.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
71 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
72 * By default this object will iterate over its own children, but this can
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
73 * be overridden with the iterator argument
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
74 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
75 * @param string $name
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
76 * @param ElementList $iterator
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
77 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
78 public function __construct($name, ElementList $iterator = null) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
79
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
80 $this->name = strtoupper($name);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
81 if (!is_null($iterator)) $this->iterator = $iterator;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
82
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
83 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
84
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
85 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
86 * Turns the object back into a serialized blob.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
87 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
88 * @return string
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
89 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
90 public function serialize() {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
91
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
92 $str = "BEGIN:" . $this->name . "\r\n";
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
93
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
94 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
95 * Gives a component a 'score' for sorting purposes.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
96 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
97 * This is solely used by the childrenSort method.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
98 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
99 * A higher score means the item will be lower in the list.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
100 * To avoid score collisions, each "score category" has a reasonable
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
101 * space to accomodate elements. The $key is added to the $score to
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
102 * preserve the original relative order of elements.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
103 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
104 * @param int $key
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
105 * @param array $array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
106 * @return int
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
107 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
108 $sortScore = function($key, $array) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
109
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
110 if ($array[$key] instanceof Component) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
111
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
112 // We want to encode VTIMEZONE first, this is a personal
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
113 // preference.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
114 if ($array[$key]->name === 'VTIMEZONE') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
115 $score=300000000;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
116 return $score+$key;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
117 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
118 $score=400000000;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
119 return $score+$key;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
120 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
121 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
122 // Properties get encoded first
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
123 // VCARD version 4.0 wants the VERSION property to appear first
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
124 if ($array[$key] instanceof Property) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
125 if ($array[$key]->name === 'VERSION') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
126 $score=100000000;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
127 return $score+$key;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
128 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
129 // All other properties
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
130 $score=200000000;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
131 return $score+$key;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
132 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
133 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
134 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
135
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
136 };
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
137
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
138 $tmp = $this->children;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
139 uksort($this->children, function($a, $b) use ($sortScore, $tmp) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
140
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
141 $sA = $sortScore($a, $tmp);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
142 $sB = $sortScore($b, $tmp);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
143
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
144 if ($sA === $sB) return 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
145
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
146 return ($sA < $sB) ? -1 : 1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
147
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
148 });
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
149
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
150 foreach($this->children as $child) $str.=$child->serialize();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
151 $str.= "END:" . $this->name . "\r\n";
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
152
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
153 return $str;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
154
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
155 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
156
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
157 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
158 * Adds a new component or element
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
159 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
160 * You can call this method with the following syntaxes:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
161 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
162 * add(Node $node)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
163 * add(string $name, $value, array $parameters = array())
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
164 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
165 * The first version adds an Element
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
166 * The second adds a property as a string.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
167 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
168 * @param mixed $item
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
169 * @param mixed $itemValue
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
170 * @return void
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
171 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
172 public function add($item, $itemValue = null, array $parameters = array()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
173
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
174 if ($item instanceof Node) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
175 if (!is_null($itemValue)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
176 throw new \InvalidArgumentException('The second argument must not be specified, when passing a VObject Node');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
177 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
178 $item->parent = $this;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
179 $this->children[] = $item;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
180 } elseif(is_string($item)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
181
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
182 $item = Property::create($item,$itemValue, $parameters);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
183 $item->parent = $this;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
184 $this->children[] = $item;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
185
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
186 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
187
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
188 throw new \InvalidArgumentException('The first argument must either be a \\Sabre\\VObject\\Node or a string');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
189
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
190 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
191
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
192 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
193
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
194 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
195 * Returns an iterable list of children
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
196 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
197 * @return ElementList
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
198 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
199 public function children() {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
200
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
201 return new ElementList($this->children);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
202
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
203 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
204
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
205 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
206 * Returns an array with elements that match the specified name.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
207 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
208 * This function is also aware of MIME-Directory groups (as they appear in
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
209 * vcards). This means that if a property is grouped as "HOME.EMAIL", it
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
210 * will also be returned when searching for just "EMAIL". If you want to
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
211 * search for a property in a specific group, you can select on the entire
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
212 * string ("HOME.EMAIL"). If you want to search on a specific property that
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
213 * has not been assigned a group, specify ".EMAIL".
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
214 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
215 * Keys are retained from the 'children' array, which may be confusing in
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
216 * certain cases.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
217 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
218 * @param string $name
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
219 * @return array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
220 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
221 public function select($name) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
222
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
223 $group = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
224 $name = strtoupper($name);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
225 if (strpos($name,'.')!==false) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
226 list($group,$name) = explode('.', $name, 2);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
227 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
228
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
229 $result = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
230 foreach($this->children as $key=>$child) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
231
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
232 if (
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
233 strtoupper($child->name) === $name &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
234 (is_null($group) || ( $child instanceof Property && strtoupper($child->group) === $group))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
235 ) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
236
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
237 $result[$key] = $child;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
238
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
239 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
240 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
241
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
242 reset($result);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
243 return $result;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
244
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
245 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
246
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
247 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
248 * This method only returns a list of sub-components. Properties are
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
249 * ignored.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
250 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
251 * @return array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
252 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
253 public function getComponents() {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
254
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
255 $result = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
256 foreach($this->children as $child) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
257 if ($child instanceof Component) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
258 $result[] = $child;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
259 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
260 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
261
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
262 return $result;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
263
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
264 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
265
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
266 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
267 * Validates the node for correctness.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
268 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
269 * The following options are supported:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
270 * - Node::REPAIR - If something is broken, and automatic repair may
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
271 * be attempted.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
272 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
273 * An array is returned with warnings.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
274 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
275 * Every item in the array has the following properties:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
276 * * level - (number between 1 and 3 with severity information)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
277 * * message - (human readable message)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
278 * * node - (reference to the offending node)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
279 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
280 * @param int $options
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
281 * @return array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
282 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
283 public function validate($options = 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
284
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
285 $result = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
286 foreach($this->children as $child) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
287 $result = array_merge($result, $child->validate($options));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
288 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
289 return $result;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
290
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
291 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
292
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
293 /* Magic property accessors {{{ */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
294
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
295 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
296 * Using 'get' you will either get a property or component,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
297 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
298 * If there were no child-elements found with the specified name,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
299 * null is returned.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
300 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
301 * @param string $name
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
302 * @return Property
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
303 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
304 public function __get($name) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
305
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
306 $matches = $this->select($name);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
307 if (count($matches)===0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
308 return null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
309 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
310 $firstMatch = current($matches);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
311 /** @var $firstMatch Property */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
312 $firstMatch->setIterator(new ElementList(array_values($matches)));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
313 return $firstMatch;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
314 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
315
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
316 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
317
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
318 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
319 * This method checks if a sub-element with the specified name exists.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
320 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
321 * @param string $name
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
322 * @return bool
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
323 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
324 public function __isset($name) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
325
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
326 $matches = $this->select($name);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
327 return count($matches)>0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
328
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
329 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
330
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
331 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
332 * Using the setter method you can add properties or subcomponents
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
333 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
334 * You can either pass a Component, Property
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
335 * object, or a string to automatically create a Property.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
336 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
337 * If the item already exists, it will be removed. If you want to add
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
338 * a new item with the same name, always use the add() method.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
339 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
340 * @param string $name
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
341 * @param mixed $value
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
342 * @return void
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
343 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
344 public function __set($name, $value) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
345
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
346 $matches = $this->select($name);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
347 $overWrite = count($matches)?key($matches):null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
348
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
349 if ($value instanceof Component || $value instanceof Property) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
350 $value->parent = $this;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
351 if (!is_null($overWrite)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
352 $this->children[$overWrite] = $value;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
353 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
354 $this->children[] = $value;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
355 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
356 } elseif (is_scalar($value)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
357 $property = Property::create($name,$value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
358 $property->parent = $this;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
359 if (!is_null($overWrite)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
360 $this->children[$overWrite] = $property;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
361 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
362 $this->children[] = $property;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
363 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
364 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
365 throw new \InvalidArgumentException('You must pass a \\Sabre\\VObject\\Component, \\Sabre\\VObject\\Property or scalar type');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
366 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
367
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
368 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
369
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
370 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
371 * Removes all properties and components within this component.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
372 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
373 * @param string $name
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
374 * @return void
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
375 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
376 public function __unset($name) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
377
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
378 $matches = $this->select($name);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
379 foreach($matches as $k=>$child) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
380
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
381 unset($this->children[$k]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
382 $child->parent = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
383
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
384 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
385
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
386 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
387
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
388 /* }}} */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
389
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
390 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
391 * This method is automatically called when the object is cloned.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
392 * Specifically, this will ensure all child elements are also cloned.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
393 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
394 * @return void
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
395 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
396 public function __clone() {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
397
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
398 foreach($this->children as $key=>$child) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
399 $this->children[$key] = clone $child;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
400 $this->children[$key]->parent = $this;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
401 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
402
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
403 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
404
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
405 }