3 namespace Sabre\VObject;
8 * This class represents a VCALENDAR/VCARD component. A component is for example
9 * VEVENT, VTODO and also VCALENDAR. It starts with BEGIN:COMPONENTNAME and
10 * 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 Component extends Element {
19 * Name, for example VEVENT
26 * Children properties and components
30 public $children = array();
33 * The following constants are used by the validate() method.
38 * If components are added to this map, they will be automatically mapped
39 * to their respective classes, if parsed by the reader or constructed with
40 * the 'create' method.
44 static public $classMap = array(
45 'VALARM' => 'Sabre\\VObject\\Component\\VAlarm',
46 'VCALENDAR' => 'Sabre\\VObject\\Component\\VCalendar',
47 'VCARD' => 'Sabre\\VObject\\Component\\VCard',
48 'VEVENT' => 'Sabre\\VObject\\Component\\VEvent',
49 'VJOURNAL' => 'Sabre\\VObject\\Component\\VJournal',
50 'VTODO' => 'Sabre\\VObject\\Component\\VTodo',
54 * Creates the new component by name, but in addition will also see if
55 * there's a class mapped to the property name.
58 * @param string $value
61 static public function create($name, $value = null) {
63 $name = strtoupper($name);
65 if (isset(self::$classMap[$name])) {
66 return new self::$classMap[$name]($name, $value);
68 return new self($name, $value);
74 * Creates a new component.
76 * By default this object will iterate over its own children, but this can
77 * be overridden with the iterator argument
80 * @param ElementList $iterator
82 public function __construct($name, ElementList $iterator = null) {
84 $this->name = strtoupper($name);
85 if (!is_null($iterator)) $this->iterator = $iterator;
90 * Turns the object back into a serialized blob.
94 public function serialize() {
96 $str = "BEGIN:" . $this->name . "\r\n";
99 * Gives a component a 'score' for sorting purposes.
101 * This is solely used by the childrenSort method.
103 * A higher score means the item will be lower in the list.
104 * To avoid score collisions, each "score category" has a reasonable
105 * space to accomodate elements. The $key is added to the $score to
106 * preserve the original relative order of elements.
109 * @param array $array
112 $sortScore = function($key, $array) {
114 if ($array[$key] instanceof Component) {
116 // We want to encode VTIMEZONE first, this is a personal
118 if ($array[$key]->name === 'VTIMEZONE') {
126 // Properties get encoded first
127 // VCARD version 4.0 wants the VERSION property to appear first
128 if ($array[$key] instanceof Property) {
129 if ($array[$key]->name === 'VERSION') {
133 // All other properties
142 $tmp = $this->children;
143 uksort($this->children, function($a, $b) use ($sortScore, $tmp) {
145 $sA = $sortScore($a, $tmp);
146 $sB = $sortScore($b, $tmp);
148 if ($sA === $sB) return 0;
150 return ($sA < $sB) ? -1 : 1;
154 foreach($this->children as $child) $str.=$child->serialize();
155 $str.= "END:" . $this->name . "\r\n";
162 * Adds a new component or element
164 * You can call this method with the following syntaxes:
166 * add(Element $element)
167 * add(string $name, $value, array $parameters = array())
169 * The first version adds an Element
170 * The second adds a property as a string.
173 * @param mixed $itemValue
176 public function add($item, $itemValue = null, array $parameters = array()) {
178 if ($item instanceof Element) {
179 if (!is_null($itemValue)) {
180 throw new \InvalidArgumentException('The second argument must not be specified, when passing a VObject');
182 $item->parent = $this;
183 $this->children[] = $item;
184 } elseif(is_string($item)) {
186 if (!is_scalar($itemValue)) {
187 throw new \InvalidArgumentException('The second argument must be scalar');
189 $item = Property::create($item,$itemValue, $parameters);
190 $item->parent = $this;
191 $this->children[] = $item;
195 throw new \InvalidArgumentException('The first argument must either be a \\Sabre\\VObject\\Element or a string');
202 * Returns an iterable list of children
204 * @return ElementList
206 public function children() {
208 return new ElementList($this->children);
213 * Returns an array with elements that match the specified name.
215 * This function is also aware of MIME-Directory groups (as they appear in
216 * vcards). This means that if a property is grouped as "HOME.EMAIL", it
217 * will also be returned when searching for just "EMAIL". If you want to
218 * search for a property in a specific group, you can select on the entire
219 * string ("HOME.EMAIL"). If you want to search on a specific property that
220 * has not been assigned a group, specify ".EMAIL".
222 * Keys are retained from the 'children' array, which may be confusing in
225 * @param string $name
228 public function select($name) {
231 $name = strtoupper($name);
232 if (strpos($name,'.')!==false) {
233 list($group,$name) = explode('.', $name, 2);
237 foreach($this->children as $key=>$child) {
240 strtoupper($child->name) === $name &&
241 (is_null($group) || ( $child instanceof Property && strtoupper($child->group) === $group))
244 $result[$key] = $child;
255 * This method only returns a list of sub-components. Properties are
260 public function getComponents() {
263 foreach($this->children as $child) {
264 if ($child instanceof Component) {
274 * Validates the node for correctness.
276 * The following options are supported:
277 * - Component::REPAIR - If something is broken, and automatic repair may
280 * An array is returned with warnings.
282 * Every item in the array has the following properties:
283 * * level - (number between 1 and 3 with severity information)
284 * * message - (human readable message)
285 * * node - (reference to the offending node)
287 * @param int $options
290 public function validate($options = 0) {
293 foreach($this->children as $child) {
294 $result = array_merge($result, $child->validate());
300 /* Magic property accessors {{{ */
303 * Using 'get' you will either get a property or component,
305 * If there were no child-elements found with the specified name,
308 * @param string $name
311 public function __get($name) {
313 $matches = $this->select($name);
314 if (count($matches)===0) {
317 $firstMatch = current($matches);
318 /** @var $firstMatch Property */
319 $firstMatch->setIterator(new ElementList(array_values($matches)));
326 * This method checks if a sub-element with the specified name exists.
328 * @param string $name
331 public function __isset($name) {
333 $matches = $this->select($name);
334 return count($matches)>0;
339 * Using the setter method you can add properties or subcomponents
341 * You can either pass a Component, Property
342 * object, or a string to automatically create a Property.
344 * If the item already exists, it will be removed. If you want to add
345 * a new item with the same name, always use the add() method.
347 * @param string $name
348 * @param mixed $value
351 public function __set($name, $value) {
353 $matches = $this->select($name);
354 $overWrite = count($matches)?key($matches):null;
356 if ($value instanceof Component || $value instanceof Property) {
357 $value->parent = $this;
358 if (!is_null($overWrite)) {
359 $this->children[$overWrite] = $value;
361 $this->children[] = $value;
363 } elseif (is_scalar($value)) {
364 $property = Property::create($name,$value);
365 $property->parent = $this;
366 if (!is_null($overWrite)) {
367 $this->children[$overWrite] = $property;
369 $this->children[] = $property;
372 throw new \InvalidArgumentException('You must pass a \\Sabre\\VObject\\Component, \\Sabre\\VObject\\Property or scalar type');
378 * Removes all properties and components within this component.
380 * @param string $name
383 public function __unset($name) {
385 $matches = $this->select($name);
386 foreach($matches as $k=>$child) {
388 unset($this->children[$k]);
389 $child->parent = null;
398 * This method is automatically called when the object is cloned.
399 * Specifically, this will ensure all child elements are also cloned.
403 public function __clone() {
405 foreach($this->children as $key=>$child) {
406 $this->children[$key] = clone $child;
407 $this->children[$key]->parent = $this;