-
Notifications
You must be signed in to change notification settings - Fork 2
/
BreadcrumbItem.php
190 lines (156 loc) · 4.18 KB
/
BreadcrumbItem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Bundle\BreadcrumbBundle;
class BreadcrumbItem implements \ArrayAccess, \Countable, \IteratorAggregate
{
/**
* Properties on this breadcrumb item
*/
protected
$route = null, // the route of this breadcrumb item
$attributes = null, // an array of attributes for the ul
$childClass = null; // the child class of this breadcrumb item
/**
* Metadata on this breadcrumb item
*/
protected
$parent = null, // parent Breadcrumbs
$children = array(), // an array of BreadcrumPath children
$root = false; // whether or not this breadcrumb item have a root
/**
* The renderer used to render this breadcrumb item
*
* @var RendererInterface
*/
protected $renderer = null;
/**
* Class constructor
*
* @param string $route
* @param array $attributes
* @param string $childClass
*/
public function __construct($route, $attributes = array(), $childClass = 'Bundle\BreadcrumbBundle\BreadcrumbPath')
{
$this->route = (string) $route;
$this->attributes = $attributes;
$this->childClass = $childClass;
}
public function hasRoot()
{
return $this->root;
}
public function addRoot()
{
if (!$this->hasRoot()) {
$path = $this->createPath($this->getParent()->getRootName(), $this->getParent()->getRootUri());
$childrenTemp = array();
$childrenTemp[] = $path;
foreach($this->getChildren() as $child)
{
$childrenTemp[] = $child;
}
$this->root = true;
$this->children = $childrenTemp;
}
}
public function addPath($name, $route, $attributes = array(), $options = array(), $class = null)
{
$path = $this->createPath($name, $route, $attributes, $options, $class = null);
$this->children[] = $path;
return $this;
}
protected function createPath($name, $route, $attributes = array(), $options = array(), $class = null)
{
if (null === $class) {
$path = new $this->childClass($name, $route, $attributes, $options);
} else {
$path = new $class($name, $route, $attributes, $options);
}
$path->setParent($this);
return $path;
}
public function getAttributes()
{
return $this->attributes;
}
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
return $this;
}
public function setAttribute($id, $value)
{
$this->attributes[$id] = $value;
return $this;
}
public function getChildren()
{
return $this->children;
}
public function hasChildren()
{
return empty($this->chidlren);
}
public function getRoute()
{
return $this->route;
}
public function getParent()
{
return $this->parent;
}
public function setParent($parent)
{
$this->parent = $parent;
if ($this->parent->hasRoot()) {
$this->addPath($this->parent->getRootName(), $this->parent->getRootUri());
$this->root = true;
}
}
public function getChild($name)
{
return isset($this->children[$name]) ? $this->children[$name] : null;
}
/**
* Implements Countable
*/
public function count()
{
return count($this->children);
}
/**
* Implements IteratorAggregate
*/
public function getIterator()
{
return new \ArrayObject($this->children);
}
/**
* Implements ArrayAccess
*/
public function offsetExists($id)
{
return isset($this->children[$id]);
}
/**
* Implements ArrayAccess
*/
public function offsetGet($name)
{
return $this->getChild($name);
}
/**
* Implements ArrayAccess
*/
public function offsetSet($id, $value)
{
return $this->addChild($id)->setName($value);
}
/**
* Implements ArrayAccess
*/
public function offsetUnset($id)
{
$this->removeChild($id);
}
}