1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the BreadcrumbsBundle. |
5
|
|
|
* |
6
|
|
|
* (c) Yonel Ceruto <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Yceruto\Bundle\BreadcrumbsBundle; |
13
|
|
|
|
14
|
|
|
class Breadcrumbs implements \Countable, \IteratorAggregate, \ArrayAccess |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var BreadcrumbsNode[] |
18
|
|
|
*/ |
19
|
|
|
private $nodes = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Add a node. |
23
|
|
|
* |
24
|
|
|
* @param string $path |
25
|
|
|
* @param string $label |
26
|
|
|
* |
27
|
|
|
* @return BreadcrumbsNode |
28
|
|
|
*/ |
29
|
|
|
public function add($path, $label) |
30
|
|
|
{ |
31
|
|
|
$node = new BreadcrumbsNode($path, $label); |
32
|
|
|
$this->addNode($node); |
33
|
|
|
|
34
|
|
|
return $node; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param BreadcrumbsNode $node |
39
|
|
|
* |
40
|
|
|
* @return Breadcrumbs |
41
|
|
|
*/ |
42
|
|
|
public function addNode(BreadcrumbsNode $node) |
43
|
|
|
{ |
44
|
|
|
if (!$this->containsNode($node)) { |
45
|
|
|
$this->nodes[] = $node; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function remove($index) |
55
|
|
|
{ |
56
|
|
|
if (!isset($this->nodes[$index]) && !array_key_exists($index, $this->nodes)) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$removed = $this->nodes[$index]; |
61
|
|
|
unset($this->nodes[$index]); |
62
|
|
|
|
63
|
|
|
return $removed; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param BreadcrumbsNode $node |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function removeNode(BreadcrumbsNode $node) |
72
|
|
|
{ |
73
|
|
|
$index = array_search($node, $this->nodes, true); |
74
|
|
|
|
75
|
|
|
if ($index === false) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
unset($this->nodes[$index]); |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param BreadcrumbsNode $node |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function containsNode(BreadcrumbsNode $node) |
90
|
|
|
{ |
91
|
|
|
return in_array($node, $this->nodes, true); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return BreadcrumbsNode[] |
96
|
|
|
*/ |
97
|
|
|
public function getNodes() |
98
|
|
|
{ |
99
|
|
|
return $this->nodes; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function count() |
106
|
|
|
{ |
107
|
|
|
return count($this->nodes); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return BreadcrumbsNode|bool |
112
|
|
|
*/ |
113
|
|
|
public function first() |
114
|
|
|
{ |
115
|
|
|
return reset($this->nodes); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return BreadcrumbsNode|bool |
120
|
|
|
*/ |
121
|
|
|
public function last() |
122
|
|
|
{ |
123
|
|
|
return end($this->nodes); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return BreadcrumbsNode|bool |
128
|
|
|
*/ |
129
|
|
|
public function current() |
130
|
|
|
{ |
131
|
|
|
return current($this->nodes); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return BreadcrumbsNode|bool |
136
|
|
|
*/ |
137
|
|
|
public function next() |
138
|
|
|
{ |
139
|
|
|
return next($this->nodes); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function getIterator() |
146
|
|
|
{ |
147
|
|
|
return new \ArrayIterator($this->nodes); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function offsetExists($index) |
154
|
|
|
{ |
155
|
|
|
return isset($this->nodes[$index]) || array_key_exists($index, $this->nodes); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function offsetGet($index) |
162
|
|
|
{ |
163
|
|
|
return isset($this->nodes[$index]) ? $this->nodes[$index] : null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function offsetSet($index, $value) |
170
|
|
|
{ |
171
|
|
|
if (isset($index)) { |
172
|
|
|
$this->nodes[$index] = $value; |
173
|
|
|
} else { |
174
|
|
|
$this->addNode($value); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
public function offsetUnset($offset) |
182
|
|
|
{ |
183
|
|
|
return $this->remove($offset); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|