1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Routing\Matcher\Dumper;
13
14/**
15 * Collection of routes.
16 *
17 * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
18 */
19class DumperCollection implements \IteratorAggregate
20{
21    /**
22     * @var DumperCollection|null
23     */
24    private $parent;
25
26    /**
27     * @var (DumperCollection|DumperRoute)[]
28     */
29    private $children = array();
30
31    /**
32     * @var array
33     */
34    private $attributes = array();
35
36    /**
37     * Returns the children routes and collections.
38     *
39     * @return (DumperCollection|DumperRoute)[] Array of DumperCollection|DumperRoute
40     */
41    public function all()
42    {
43        return $this->children;
44    }
45
46    /**
47     * Adds a route or collection.
48     *
49     * @param DumperRoute|DumperCollection The route or collection
50     */
51    public function add($child)
52    {
53        if ($child instanceof self) {
54            $child->setParent($this);
55        }
56        $this->children[] = $child;
57    }
58
59    /**
60     * Sets children.
61     *
62     * @param array $children The children
63     */
64    public function setAll(array $children)
65    {
66        foreach ($children as $child) {
67            if ($child instanceof self) {
68                $child->setParent($this);
69            }
70        }
71        $this->children = $children;
72    }
73
74    /**
75     * Returns an iterator over the children.
76     *
77     * @return \Iterator The iterator
78     */
79    public function getIterator()
80    {
81        return new \ArrayIterator($this->children);
82    }
83
84    /**
85     * Returns the root of the collection.
86     *
87     * @return DumperCollection The root collection
88     */
89    public function getRoot()
90    {
91        return (null !== $this->parent) ? $this->parent->getRoot() : $this;
92    }
93
94    /**
95     * Returns the parent collection.
96     *
97     * @return DumperCollection|null The parent collection or null if the collection has no parent
98     */
99    protected function getParent()
100    {
101        return $this->parent;
102    }
103
104    /**
105     * Sets the parent collection.
106     *
107     * @param DumperCollection $parent The parent collection
108     */
109    protected function setParent(DumperCollection $parent)
110    {
111        $this->parent = $parent;
112    }
113
114    /**
115     * Returns true if the attribute is defined.
116     *
117     * @param string $name The attribute name
118     *
119     * @return bool true if the attribute is defined, false otherwise
120     */
121    public function hasAttribute($name)
122    {
123        return array_key_exists($name, $this->attributes);
124    }
125
126    /**
127     * Returns an attribute by name.
128     *
129     * @param string $name    The attribute name
130     * @param mixed  $default Default value is the attribute doesn't exist
131     *
132     * @return mixed The attribute value
133     */
134    public function getAttribute($name, $default = null)
135    {
136        return $this->hasAttribute($name) ? $this->attributes[$name] : $default;
137    }
138
139    /**
140     * Sets an attribute by name.
141     *
142     * @param string $name  The attribute name
143     * @param mixed  $value The attribute value
144     */
145    public function setAttribute($name, $value)
146    {
147        $this->attributes[$name] = $value;
148    }
149
150    /**
151     * Sets multiple attributes.
152     *
153     * @param array $attributes The attributes
154     */
155    public function setAttributes($attributes)
156    {
157        $this->attributes = $attributes;
158    }
159}
160