annotate vendor/sabre/vobject/lib/Component.php @ 7:430dbd5346f7

vendor sabre as distributed
author Charlie Root
date Sat, 13 Jan 2018 09:06:10 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
1 <?php
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
2
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
3 namespace Sabre\VObject;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
4
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
5 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
6 * Component
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
7 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
8 * A component represents a group of properties, such as VCALENDAR, VEVENT, or
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
9 * VCARD.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
10 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
11 * @copyright Copyright (C) 2011-2015 fruux GmbH (https://fruux.com/).
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
12 * @author Evert Pot (http://evertpot.com/)
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
13 * @license http://sabre.io/license/ Modified BSD License
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
14 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
15 class Component extends Node {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
16
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
17 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
18 * Component name.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
19 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
20 * This will contain a string such as VEVENT, VTODO, VCALENDAR, VCARD.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
21 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
22 * @var string
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
23 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
24 public $name;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
25
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
26 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
27 * A list of properties and/or sub-components.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
28 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
29 * @var array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
30 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
31 public $children = array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
32
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
33 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
34 * Creates a new component.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
35 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
36 * You can specify the children either in key=>value syntax, in which case
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
37 * properties will automatically be created, or you can just pass a list of
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
38 * Component and Property object.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
39 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
40 * By default, a set of sensible values will be added to the component. For
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
41 * an iCalendar object, this may be something like CALSCALE:GREGORIAN. To
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
42 * ensure that this does not happen, set $defaults to false.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
43 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
44 * @param Document $root
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
45 * @param string $name such as VCALENDAR, VEVENT.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
46 * @param array $children
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
47 * @param bool $defaults
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
48 * @return void
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
49 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
50 function __construct(Document $root, $name, array $children = array(), $defaults = true) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
51
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
52 $this->name = strtoupper($name);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
53 $this->root = $root;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
54
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
55 if ($defaults) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
56 // This is a terribly convoluted way to do this, but this ensures
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
57 // that the order of properties as they are specified in both
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
58 // defaults and the childrens list, are inserted in the object in a
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
59 // natural way.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
60 $list = $this->getDefaults();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
61 $nodes = array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
62 foreach($children as $key=>$value) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
63 if ($value instanceof Node) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
64 if (isset($list[$value->name])) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
65 unset($list[$value->name]);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
66 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
67 $nodes[] = $value;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
68 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
69 $list[$key] = $value;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
70 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
71 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
72 foreach($list as $key=>$value) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
73 $this->add($key, $value);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
74 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
75 foreach($nodes as $node) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
76 $this->add($node);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
77 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
78 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
79 foreach($children as $k=>$child) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
80 if ($child instanceof Node) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
81
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
82 // Component or Property
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
83 $this->add($child);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
84 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
85
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
86 // Property key=>value
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
87 $this->add($k, $child);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
88 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
89 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
90 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
91
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
92 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
93
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
94 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
95 * Adds a new property or component, and returns the new item.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
96 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
97 * This method has 3 possible signatures:
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
98 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
99 * add(Component $comp) // Adds a new component
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
100 * add(Property $prop) // Adds a new property
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
101 * add($name, $value, array $parameters = array()) // Adds a new property
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
102 * add($name, array $children = array()) // Adds a new component
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
103 * by name.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
104 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
105 * @return Node
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
106 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
107 function add($a1, $a2 = null, $a3 = null) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
108
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
109 if ($a1 instanceof Node) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
110 if (!is_null($a2)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
111 throw new \InvalidArgumentException('The second argument must not be specified, when passing a VObject Node');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
112 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
113 $a1->parent = $this;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
114 $this->children[] = $a1;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
115
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
116 return $a1;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
117
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
118 } elseif(is_string($a1)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
119
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
120 $item = $this->root->create($a1, $a2, $a3);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
121 $item->parent = $this;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
122 $this->children[] = $item;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
123
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
124 return $item;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
125
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
126 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
127
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
128 throw new \InvalidArgumentException('The first argument must either be a \\Sabre\\VObject\\Node or a string');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
129
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
130 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
131
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
132 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
133
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
134 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
135 * This method removes a component or property from this component.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
136 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
137 * You can either specify the item by name (like DTSTART), in which case
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
138 * all properties/components with that name will be removed, or you can
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
139 * pass an instance of a property or component, in which case only that
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
140 * exact item will be removed.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
141 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
142 * The removed item will be returned. In case there were more than 1 items
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
143 * removed, only the last one will be returned.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
144 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
145 * @param mixed $item
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
146 * @return void
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
147 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
148 function remove($item) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
149
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
150 if (is_string($item)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
151 $children = $this->select($item);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
152 foreach($children as $k=>$child) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
153 unset($this->children[$k]);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
154 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
155 return $child;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
156 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
157 foreach($this->children as $k => $child) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
158 if ($child===$item) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
159 unset($this->children[$k]);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
160 return $child;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
161 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
162 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
163
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
164 throw new \InvalidArgumentException('The item you passed to remove() was not a child of this component');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
165
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
166 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
167
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
168 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
169
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
170 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
171 * Returns an iterable list of children
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
172 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
173 * @return array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
174 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
175 function children() {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
176
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
177 return $this->children;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
178
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
179 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
180
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
181 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
182 * This method only returns a list of sub-components. Properties are
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
183 * ignored.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
184 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
185 * @return array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
186 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
187 function getComponents() {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
188
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
189 $result = array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
190 foreach($this->children as $child) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
191 if ($child instanceof Component) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
192 $result[] = $child;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
193 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
194 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
195
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
196 return $result;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
197
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
198 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
199
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
200 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
201 * Returns an array with elements that match the specified name.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
202 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
203 * This function is also aware of MIME-Directory groups (as they appear in
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
204 * vcards). This means that if a property is grouped as "HOME.EMAIL", it
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
205 * will also be returned when searching for just "EMAIL". If you want to
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
206 * search for a property in a specific group, you can select on the entire
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
207 * string ("HOME.EMAIL"). If you want to search on a specific property that
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
208 * has not been assigned a group, specify ".EMAIL".
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
209 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
210 * Keys are retained from the 'children' array, which may be confusing in
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
211 * certain cases.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
212 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
213 * @param string $name
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
214 * @return array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
215 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
216 function select($name) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
217
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
218 $group = null;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
219 $name = strtoupper($name);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
220 if (strpos($name,'.')!==false) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
221 list($group,$name) = explode('.', $name, 2);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
222 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
223
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
224 $result = array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
225 foreach($this->children as $key=>$child) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
226
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
227 if (
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
228 (
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
229 strtoupper($child->name) === $name
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
230 && (is_null($group) || ( $child instanceof Property && strtoupper($child->group) === $group))
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
231 )
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
232 ||
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
233 (
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
234 $name === '' && $child instanceof Property && strtoupper($child->group) === $group
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
235 )
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
236 ) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
237
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
238 $result[$key] = $child;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
239
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
240 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
241 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
242
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
243 reset($result);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
244 return $result;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
245
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
246 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
247
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
248 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
249 * Turns the object back into a serialized blob.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
250 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
251 * @return string
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
252 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
253 function serialize() {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
254
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
255 $str = "BEGIN:" . $this->name . "\r\n";
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
256
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
257 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
258 * Gives a component a 'score' for sorting purposes.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
259 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
260 * This is solely used by the childrenSort method.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
261 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
262 * A higher score means the item will be lower in the list.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
263 * To avoid score collisions, each "score category" has a reasonable
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
264 * space to accomodate elements. The $key is added to the $score to
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
265 * preserve the original relative order of elements.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
266 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
267 * @param int $key
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
268 * @param array $array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
269 * @return int
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
270 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
271 $sortScore = function($key, $array) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
272
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
273 if ($array[$key] instanceof Component) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
274
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
275 // We want to encode VTIMEZONE first, this is a personal
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
276 // preference.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
277 if ($array[$key]->name === 'VTIMEZONE') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
278 $score=300000000;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
279 return $score+$key;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
280 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
281 $score=400000000;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
282 return $score+$key;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
283 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
284 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
285 // Properties get encoded first
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
286 // VCARD version 4.0 wants the VERSION property to appear first
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
287 if ($array[$key] instanceof Property) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
288 if ($array[$key]->name === 'VERSION') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
289 $score=100000000;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
290 return $score+$key;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
291 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
292 // All other properties
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
293 $score=200000000;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
294 return $score+$key;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
295 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
296 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
297 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
298
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
299 };
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
300
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
301 $tmp = $this->children;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
302 uksort(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
303 $this->children,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
304 function($a, $b) use ($sortScore, $tmp) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
305
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
306 $sA = $sortScore($a, $tmp);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
307 $sB = $sortScore($b, $tmp);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
308
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
309 return $sA - $sB;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
310
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
311 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
312 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
313
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
314 foreach($this->children as $child) $str.=$child->serialize();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
315 $str.= "END:" . $this->name . "\r\n";
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
316
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
317 return $str;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
318
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
319 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
320
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
321 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
322 * This method returns an array, with the representation as it should be
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
323 * encoded in json. This is used to create jCard or jCal documents.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
324 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
325 * @return array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
326 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
327 function jsonSerialize() {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
328
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
329 $components = array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
330 $properties = array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
331
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
332 foreach($this->children as $child) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
333 if ($child instanceof Component) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
334 $components[] = $child->jsonSerialize();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
335 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
336 $properties[] = $child->jsonSerialize();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
337 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
338 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
339
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
340 return array(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
341 strtolower($this->name),
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
342 $properties,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
343 $components
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
344 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
345
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
346 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
347
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
348 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
349 * This method should return a list of default property values.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
350 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
351 * @return array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
352 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
353 protected function getDefaults() {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
354
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
355 return array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
356
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
357 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
358
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
359 /* Magic property accessors {{{ */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
360
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
361 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
362 * Using 'get' you will either get a property or component.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
363 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
364 * If there were no child-elements found with the specified name,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
365 * null is returned.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
366 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
367 * To use this, this may look something like this:
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
368 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
369 * $event = $calendar->VEVENT;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
370 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
371 * @param string $name
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
372 * @return Property
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
373 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
374 function __get($name) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
375
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
376 $matches = $this->select($name);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
377 if (count($matches)===0) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
378 return null;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
379 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
380 $firstMatch = current($matches);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
381 /** @var $firstMatch Property */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
382 $firstMatch->setIterator(new ElementList(array_values($matches)));
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
383 return $firstMatch;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
384 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
385
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
386 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
387
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
388 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
389 * This method checks if a sub-element with the specified name exists.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
390 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
391 * @param string $name
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
392 * @return bool
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
393 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
394 function __isset($name) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
395
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
396 $matches = $this->select($name);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
397 return count($matches)>0;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
398
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
399 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
400
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
401 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
402 * Using the setter method you can add properties or subcomponents
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
403 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
404 * You can either pass a Component, Property
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
405 * object, or a string to automatically create a Property.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
406 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
407 * If the item already exists, it will be removed. If you want to add
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
408 * a new item with the same name, always use the add() method.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
409 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
410 * @param string $name
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
411 * @param mixed $value
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
412 * @return void
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
413 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
414 function __set($name, $value) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
415
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
416 $matches = $this->select($name);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
417 $overWrite = count($matches)?key($matches):null;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
418
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
419 if ($value instanceof Component || $value instanceof Property) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
420 $value->parent = $this;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
421 if (!is_null($overWrite)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
422 $this->children[$overWrite] = $value;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
423 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
424 $this->children[] = $value;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
425 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
426 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
427 $property = $this->root->create($name,$value);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
428 $property->parent = $this;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
429 if (!is_null($overWrite)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
430 $this->children[$overWrite] = $property;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
431 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
432 $this->children[] = $property;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
433 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
434 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
435 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
436
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
437 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
438 * Removes all properties and components within this component with the
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
439 * specified name.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
440 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
441 * @param string $name
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
442 * @return void
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
443 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
444 function __unset($name) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
445
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
446 $matches = $this->select($name);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
447 foreach($matches as $k=>$child) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
448
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
449 unset($this->children[$k]);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
450 $child->parent = null;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
451
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
452 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
453
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
454 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
455
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
456 /* }}} */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
457
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
458 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
459 * This method is automatically called when the object is cloned.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
460 * Specifically, this will ensure all child elements are also cloned.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
461 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
462 * @return void
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
463 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
464 function __clone() {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
465
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
466 foreach($this->children as $key=>$child) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
467 $this->children[$key] = clone $child;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
468 $this->children[$key]->parent = $this;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
469 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
470
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
471 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
472
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
473 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
474 * A simple list of validation rules.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
475 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
476 * This is simply a list of properties, and how many times they either
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
477 * must or must not appear.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
478 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
479 * Possible values per property:
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
480 * * 0 - Must not appear.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
481 * * 1 - Must appear exactly once.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
482 * * + - Must appear at least once.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
483 * * * - Can appear any number of times.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
484 * * ? - May appear, but not more than once.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
485 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
486 * It is also possible to specify defaults and severity levels for
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
487 * violating the rule.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
488 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
489 * See the VEVENT implementation for getValidationRules for a more complex
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
490 * example.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
491 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
492 * @var array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
493 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
494 function getValidationRules() {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
495
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
496 return array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
497
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
498 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
499
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
500 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
501 * Validates the node for correctness.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
502 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
503 * The following options are supported:
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
504 * Node::REPAIR - May attempt to automatically repair the problem.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
505 * Node::PROFILE_CARDDAV - Validate the vCard for CardDAV purposes.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
506 * Node::PROFILE_CALDAV - Validate the iCalendar for CalDAV purposes.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
507 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
508 * This method returns an array with detected problems.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
509 * Every element has the following properties:
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
510 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
511 * * level - problem level.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
512 * * message - A human-readable string describing the issue.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
513 * * node - A reference to the problematic node.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
514 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
515 * The level means:
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
516 * 1 - The issue was repaired (only happens if REPAIR was turned on).
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
517 * 2 - A warning.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
518 * 3 - An error.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
519 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
520 * @param int $options
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
521 * @return array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
522 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
523 function validate($options = 0) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
524
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
525 $rules = $this->getValidationRules();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
526 $defaults = $this->getDefaults();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
527
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
528 $propertyCounters = array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
529
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
530 $messages = array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
531
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
532 foreach($this->children as $child) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
533 $name = strtoupper($child->name);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
534 if (!isset($propertyCounters[$name])) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
535 $propertyCounters[$name] = 1;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
536 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
537 $propertyCounters[$name]++;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
538 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
539 $messages = array_merge($messages, $child->validate($options));
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
540 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
541
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
542 foreach($rules as $propName => $rule) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
543
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
544 switch($rule) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
545 case '0' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
546 if (isset($propertyCounters[$propName])) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
547 $messages[] = array(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
548 'level' => 3,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
549 'message' => $propName . ' MUST NOT appear in a ' . $this->name . ' component',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
550 'node' => $this,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
551 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
552 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
553 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
554 case '1' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
555 if (!isset($propertyCounters[$propName]) || $propertyCounters[$propName]!==1) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
556 $repaired = false;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
557 if ($options & self::REPAIR && isset($defaults[$propName])) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
558 $this->add($propName, $defaults[$propName]);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
559 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
560 $messages[] = array(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
561 'level' => $repaired?1:3,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
562 'message' => $propName . ' MUST appear exactly once in a ' . $this->name . ' component',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
563 'node' => $this,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
564 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
565 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
566 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
567 case '+' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
568 if (!isset($propertyCounters[$propName]) || $propertyCounters[$propName] < 1) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
569 $messages[] = array(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
570 'level' => 3,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
571 'message' => $propName . ' MUST appear at least once in a ' . $this->name . ' component',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
572 'node' => $this,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
573 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
574 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
575 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
576 case '*' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
577 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
578 case '?' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
579 if (isset($propertyCounters[$propName]) && $propertyCounters[$propName] > 1) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
580 $messages[] = array(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
581 'level' => 3,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
582 'message' => $propName . ' MUST NOT appear more than once in a ' . $this->name . ' component',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
583 'node' => $this,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
584 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
585 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
586 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
587
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
588 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
589
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
590 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
591 return $messages;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
592
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
593 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
594
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
595 }