4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Matcher\Dumper;
15 * Collection of routes.
17 * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
21 class DumperCollection implements \IteratorAggregate
24 * @var DumperCollection|null
29 * @var DumperCollection[]|DumperRoute[]
31 private $children = array();
36 private $attributes = array();
39 * Returns the children routes and collections.
41 * @return self[]|DumperRoute[]
45 return $this->children;
49 * Adds a route or collection.
51 * @param DumperRoute|DumperCollection The route or collection
53 public function add($child)
55 if ($child instanceof self) {
56 $child->setParent($this);
58 $this->children[] = $child;
64 * @param array $children The children
66 public function setAll(array $children)
68 foreach ($children as $child) {
69 if ($child instanceof self) {
70 $child->setParent($this);
73 $this->children = $children;
77 * Returns an iterator over the children.
79 * @return \Iterator|DumperCollection[]|DumperRoute[] The iterator
81 public function getIterator()
83 return new \ArrayIterator($this->children);
87 * Returns the root of the collection.
89 * @return self The root collection
91 public function getRoot()
93 return (null !== $this->parent) ? $this->parent->getRoot() : $this;
97 * Returns the parent collection.
99 * @return self|null The parent collection or null if the collection has no parent
101 protected function getParent()
103 return $this->parent;
107 * Sets the parent collection.
109 protected function setParent(self $parent)
111 $this->parent = $parent;
115 * Returns true if the attribute is defined.
117 * @param string $name The attribute name
119 * @return bool true if the attribute is defined, false otherwise
121 public function hasAttribute($name)
123 return array_key_exists($name, $this->attributes);
127 * Returns an attribute by name.
129 * @param string $name The attribute name
130 * @param mixed $default Default value is the attribute doesn't exist
132 * @return mixed The attribute value
134 public function getAttribute($name, $default = null)
136 return $this->hasAttribute($name) ? $this->attributes[$name] : $default;
140 * Sets an attribute by name.
142 * @param string $name The attribute name
143 * @param mixed $value The attribute value
145 public function setAttribute($name, $value)
147 $this->attributes[$name] = $value;
151 * Sets multiple attributes.
153 * @param array $attributes The attributes
155 public function setAttributes($attributes)
157 $this->attributes = $attributes;