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 coponents 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 higher in the list
99 * @param Sabre_VObject_Node $n
102 $sortScore = function($n) {
104 if ($n instanceof Sabre_VObject_Component) {
105 // We want to encode VTIMEZONE first, this is a personal
107 if ($n->name === 'VTIMEZONE') {
113 // VCARD version 4.0 wants the VERSION property to appear first
114 if ($n->name === 'VERSION') {
123 usort($this->children, function($a, $b) use ($sortScore) {
125 $sA = $sortScore($a);
126 $sB = $sortScore($b);
128 if ($sA === $sB) return 0;
130 return ($sA > $sB) ? -1 : 1;
134 foreach($this->children as $child) $str.=$child->serialize();
135 $str.= "END:" . $this->name . "\r\n";
142 * Adds a new component or element
144 * You can call this method with the following syntaxes:
146 * add(Sabre_VObject_Element $element)
147 * add(string $name, $value)
149 * The first version adds an Element
150 * The second adds a property as a string.
153 * @param mixed $itemValue
156 public function add($item, $itemValue = null) {
158 if ($item instanceof Sabre_VObject_Element) {
159 if (!is_null($itemValue)) {
160 throw new InvalidArgumentException('The second argument must not be specified, when passing a VObject');
162 $item->parent = $this;
163 $this->children[] = $item;
164 } elseif(is_string($item)) {
166 if (!is_scalar($itemValue)) {
167 throw new InvalidArgumentException('The second argument must be scalar');
169 $item = Sabre_VObject_Property::create($item,$itemValue);
170 $item->parent = $this;
171 $this->children[] = $item;
175 throw new InvalidArgumentException('The first argument must either be a Sabre_VObject_Element or a string');
182 * Returns an iterable list of children
184 * @return Sabre_VObject_ElementList
186 public function children() {
188 return new Sabre_VObject_ElementList($this->children);
193 * Returns an array with elements that match the specified name.
195 * This function is also aware of MIME-Directory groups (as they appear in
196 * vcards). This means that if a property is grouped as "HOME.EMAIL", it
197 * will also be returned when searching for just "EMAIL". If you want to
198 * search for a property in a specific group, you can select on the entire
199 * string ("HOME.EMAIL"). If you want to search on a specific property that
200 * has not been assigned a group, specify ".EMAIL".
202 * Keys are retained from the 'children' array, which may be confusing in
205 * @param string $name
208 public function select($name) {
211 $name = strtoupper($name);
212 if (strpos($name,'.')!==false) {
213 list($group,$name) = explode('.', $name, 2);
217 foreach($this->children as $key=>$child) {
220 strtoupper($child->name) === $name &&
221 (is_null($group) || ( $child instanceof Sabre_VObject_Property && strtoupper($child->group) === $group))
224 $result[$key] = $child;
235 * This method only returns a list of sub-components. Properties are
240 public function getComponents() {
243 foreach($this->children as $child) {
244 if ($child instanceof Sabre_VObject_Component) {
253 /* Magic property accessors {{{ */
256 * Using 'get' you will either get a property or component,
258 * If there were no child-elements found with the specified name,
261 * @param string $name
262 * @return Sabre_VObject_Property
264 public function __get($name) {
266 $matches = $this->select($name);
267 if (count($matches)===0) {
270 $firstMatch = current($matches);
271 /** @var $firstMatch Sabre_VObject_Property */
272 $firstMatch->setIterator(new Sabre_VObject_ElementList(array_values($matches)));
279 * This method checks if a sub-element with the specified name exists.
281 * @param string $name
284 public function __isset($name) {
286 $matches = $this->select($name);
287 return count($matches)>0;
292 * Using the setter method you can add properties or subcomponents
294 * You can either pass a Sabre_VObject_Component, Sabre_VObject_Property
295 * object, or a string to automatically create a Property.
297 * If the item already exists, it will be removed. If you want to add
298 * a new item with the same name, always use the add() method.
300 * @param string $name
301 * @param mixed $value
304 public function __set($name, $value) {
306 $matches = $this->select($name);
307 $overWrite = count($matches)?key($matches):null;
309 if ($value instanceof Sabre_VObject_Component || $value instanceof Sabre_VObject_Property) {
310 $value->parent = $this;
311 if (!is_null($overWrite)) {
312 $this->children[$overWrite] = $value;
314 $this->children[] = $value;
316 } elseif (is_scalar($value)) {
317 $property = Sabre_VObject_Property::create($name,$value);
318 $property->parent = $this;
319 if (!is_null($overWrite)) {
320 $this->children[$overWrite] = $property;
322 $this->children[] = $property;
325 throw new InvalidArgumentException('You must pass a Sabre_VObject_Component, Sabre_VObject_Property or scalar type');
331 * Removes all properties and components within this component.
333 * @param string $name
336 public function __unset($name) {
338 $matches = $this->select($name);
339 foreach($matches as $k=>$child) {
341 unset($this->children[$k]);
342 $child->parent = null;
351 * This method is automatically called when the object is cloned.
352 * Specifically, this will ensure all child elements are also cloned.
356 public function __clone() {
358 foreach($this->children as $key=>$child) {
359 $this->children[$key] = clone $child;
360 $this->children[$key]->parent = $this;