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