6 * This class represents a VCALENDAR/VCARD component. A component is for example
7 * VEVENT, VTODO and also VCALENDAR. It starts with BEGIN:COMPONENTNAME and
8 * ends with END:COMPONENTNAME
12 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
13 * @author Evert Pot (http://www.rooftopsolutions.nl/)
14 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
16 class Sabre_VObject_Component extends Sabre_VObject_Element {
19 * Name, for example VEVENT
26 * Children properties and components
30 public $children = array();
33 * If components are added to this map, they will be automatically mapped
34 * to their respective classes, if parsed by the reader or constructed with
35 * the 'create' method.
39 static public $classMap = array(
40 'VCALENDAR' => 'Sabre_VObject_Component_VCalendar',
41 'VEVENT' => 'Sabre_VObject_Component_VEvent',
42 'VTODO' => 'Sabre_VObject_Component_VTodo',
43 'VJOURNAL' => 'Sabre_VObject_Component_VJournal',
44 'VALARM' => 'Sabre_VObject_Component_VAlarm',
48 * Creates the new component by name, but in addition will also see if
49 * there's a class mapped to the property name.
52 * @param string $value
53 * @return Sabre_VObject_Component
55 static public function create($name, $value = null) {
57 $name = strtoupper($name);
59 if (isset(self::$classMap[$name])) {
60 return new self::$classMap[$name]($name, $value);
62 return new self($name, $value);
68 * Creates a new component.
70 * By default this object will iterate over its own children, but this can
71 * be overridden with the iterator argument
74 * @param Sabre_VObject_ElementList $iterator
76 public function __construct($name, Sabre_VObject_ElementList $iterator = null) {
78 $this->name = strtoupper($name);
79 if (!is_null($iterator)) $this->iterator = $iterator;
84 * Turns the object back into a serialized blob.
88 public function serialize() {
90 $str = "BEGIN:" . $this->name . "\r\n";
93 * Gives a component a 'score' for sorting purposes.
95 * This is solely used by the childrenSort method.
97 * A higher score means the item will be lower in the list.
98 * To avoid score collisions, each "score category" has a reasonable
99 * space to accomodate elements. The $key is added to the $score to
100 * preserve the original relative order of elements.
103 * @param Sabre_VObject $array
106 $sortScore = function($key, $array) {
108 if ($array[$key] instanceof Sabre_VObject_Component) {
109 // We want to encode VTIMEZONE first, this is a personal
111 if ($array[$key]->name === 'VTIMEZONE') {
119 // Properties get encoded first
120 // VCARD version 4.0 wants the VERSION property to appear first
121 if ($array[$key] instanceof Sabre_VObject_Property) {
122 if ($array[$key]->name === 'VERSION') {
126 // All other properties
136 $tmp = $this->children;
137 uksort($this->children, function($a, $b) use ($sortScore, $tmp) {
139 $sA = $sortScore($a, $tmp);
140 $sB = $sortScore($b, $tmp);
142 if ($sA === $sB) return 0;
144 return ($sA < $sB) ? -1 : 1;
148 foreach($this->children as $child) $str.=$child->serialize();
149 $str.= "END:" . $this->name . "\r\n";
156 * Adds a new component or element
158 * You can call this method with the following syntaxes:
160 * add(Sabre_VObject_Element $element)
161 * add(string $name, $value)
163 * The first version adds an Element
164 * The second adds a property as a string.
167 * @param mixed $itemValue
170 public function add($item, $itemValue = null) {
172 if ($item instanceof Sabre_VObject_Element) {
173 if (!is_null($itemValue)) {
174 throw new InvalidArgumentException('The second argument must not be specified, when passing a VObject');
176 $item->parent = $this;
177 $this->children[] = $item;
178 } elseif(is_string($item)) {
180 if (!is_scalar($itemValue)) {
181 throw new InvalidArgumentException('The second argument must be scalar');
183 $item = Sabre_VObject_Property::create($item,$itemValue);
184 $item->parent = $this;
185 $this->children[] = $item;
189 throw new InvalidArgumentException('The first argument must either be a Sabre_VObject_Element or a string');
196 * Returns an iterable list of children
198 * @return Sabre_VObject_ElementList
200 public function children() {
202 return new Sabre_VObject_ElementList($this->children);
207 * Returns an array with elements that match the specified name.
209 * This function is also aware of MIME-Directory groups (as they appear in
210 * vcards). This means that if a property is grouped as "HOME.EMAIL", it
211 * will also be returned when searching for just "EMAIL". If you want to
212 * search for a property in a specific group, you can select on the entire
213 * string ("HOME.EMAIL"). If you want to search on a specific property that
214 * has not been assigned a group, specify ".EMAIL".
216 * Keys are retained from the 'children' array, which may be confusing in
219 * @param string $name
222 public function select($name) {
225 $name = strtoupper($name);
226 if (strpos($name,'.')!==false) {
227 list($group,$name) = explode('.', $name, 2);
231 foreach($this->children as $key=>$child) {
234 strtoupper($child->name) === $name &&
235 (is_null($group) || ( $child instanceof Sabre_VObject_Property && strtoupper($child->group) === $group))
238 $result[$key] = $child;
249 * This method only returns a list of sub-components. Properties are
254 public function getComponents() {
257 foreach($this->children as $child) {
258 if ($child instanceof Sabre_VObject_Component) {
268 * Validates the node for correctness.
269 * An array is returned with warnings.
271 * Every item in the array has the following properties:
272 * * level - (number between 1 and 3 with severity information)
273 * * message - (human readable message)
274 * * node - (reference to the offending node)
278 public function validate() {
281 foreach($this->children as $child) {
282 $result = array_merge($result, $child->validate());
288 /* Magic property accessors {{{ */
291 * Using 'get' you will either get a property or component,
293 * If there were no child-elements found with the specified name,
296 * @param string $name
297 * @return Sabre_VObject_Property
299 public function __get($name) {
301 $matches = $this->select($name);
302 if (count($matches)===0) {
305 $firstMatch = current($matches);
306 /** @var $firstMatch Sabre_VObject_Property */
307 $firstMatch->setIterator(new Sabre_VObject_ElementList(array_values($matches)));
314 * This method checks if a sub-element with the specified name exists.
316 * @param string $name
319 public function __isset($name) {
321 $matches = $this->select($name);
322 return count($matches)>0;
327 * Using the setter method you can add properties or subcomponents
329 * You can either pass a Sabre_VObject_Component, Sabre_VObject_Property
330 * object, or a string to automatically create a Property.
332 * If the item already exists, it will be removed. If you want to add
333 * a new item with the same name, always use the add() method.
335 * @param string $name
336 * @param mixed $value
339 public function __set($name, $value) {
341 $matches = $this->select($name);
342 $overWrite = count($matches)?key($matches):null;
344 if ($value instanceof Sabre_VObject_Component || $value instanceof Sabre_VObject_Property) {
345 $value->parent = $this;
346 if (!is_null($overWrite)) {
347 $this->children[$overWrite] = $value;
349 $this->children[] = $value;
351 } elseif (is_scalar($value)) {
352 $property = Sabre_VObject_Property::create($name,$value);
353 $property->parent = $this;
354 if (!is_null($overWrite)) {
355 $this->children[$overWrite] = $property;
357 $this->children[] = $property;
360 throw new InvalidArgumentException('You must pass a Sabre_VObject_Component, Sabre_VObject_Property or scalar type');
366 * Removes all properties and components within this component.
368 * @param string $name
371 public function __unset($name) {
373 $matches = $this->select($name);
374 foreach($matches as $k=>$child) {
376 unset($this->children[$k]);
377 $child->parent = null;
386 * This method is automatically called when the object is cloned.
387 * Specifically, this will ensure all child elements are also cloned.
391 public function __clone() {
393 foreach($this->children as $key=>$child) {
394 $this->children[$key] = clone $child;
395 $this->children[$key]->parent = $this;