-
Notifications
You must be signed in to change notification settings - Fork 16
/
recursive-caching-iterator.php
199 lines (160 loc) · 5.16 KB
/
recursive-caching-iterator.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
191
192
193
194
195
196
197
198
199
<?php
/**
* This is an example of using the recursive caching iterator to do a lookahead
* for the last element in a multi-dimensional "navigation" array so we can
* accurately set classes for "last". The other functionality of generating
* a full unordered list is just a bonus.
*
* Please note:
* No safety measures have been taken to sanitize the output.
*
* @author Corey Ballou
*/
// example navigation array
$nav = array(
'Home' => '/home',
'Fake' => array(
'Double Fake' => array(
'Nested Double Fake' => '/fake/double/nested',
'Doubly Nested Double Fake' => '/fake/double/doubly'
),
'Triple Fake' => '/fake/tripe'
),
'Products' => array(
'Product 1' => '/products/1',
'Product 2' => '/products/2',
'Product 3' => '/products/3',
'Nested Product' => array(
'Nested 1' => '/products/nested/1',
'Nested 2' => '/products/nested/2'
)
),
'Company' => '/company',
'Privacy Policy' => '/privacy-policy'
);
// storage of output
$output = new ArrayIterator();
try {
// create the caching iterator of the nav array
$it = new RecursiveIteratorIterator(
new RecursiveCachingIterator(
new RecursiveArrayIterator($nav)
),
RecursiveIteratorIterator::SELF_FIRST
);
// child flag
$depth = 0;
// generate the nav
foreach ($it as $name => $url) {
// set the current depth
$curDepth = $it->getDepth();
// store the difference in depths
$diff = abs($curDepth - $depth);
// close previous nested levels
if ($curDepth < $depth) {
$output->append(str_repeat('</ul></li>', $diff));
}
// check if we have the last nav item
if ($it->hasNext()) {
$output->append('<li><a href="' . $url . '">' . $name . '</a>');
} else {
$output->append('<li class="last"><a href="' . $url . '">' . $name . '</a>');
}
// either add a subnav or close the list item
if ($it->hasChildren()) {
$output->append('<ul>');
} else {
$output->append('</li>');
}
// cache the depth
$depth = $curDepth;
}
// if we have values, output the unordered list
if ($output->count()) {
echo '<ul id="nav">' . "\n" . implode("\n", (array) $output) . "\n" . '</ul>';
}
} catch (Exception $e) {
die($e->getMessage());
}
echo PHP_EOL . PHP_EOL . 'CLASS EXAMPLE' . PHP_EOL . PHP_EOL;
/**
* Below is the same example, but prettified in a nice, extensible class
* allowing you to reuse it for nav, subnav, or any time you need to
* determine the last element of an array.
*/
class NavBuilder extends RecursiveIteratorIterator {
// stores the previous depth
private $_depth = 0;
// stores the current iteration's depth
private $_curDepth = 0;
// store the iterator
protected $_it;
/**
* Constructor.
*
* @access public
* @param Traversable $it
* @param int $mode
* @param int $flags
*/
public function __construct(Traversable $it, $mode = RecursiveIteratorIterator::SELF_FIRST, $flags = 0)
{
parent::__construct($it, $mode, $flags);
// store the caching iterator
$this->_it = $it;
}
/**
* Override the return values.
*
* @access public
*/
public function current()
{
// the return output string
$output = '';
// set the current depth
$this->_curDepth = parent::getDepth();
// store the difference in depths
$diff = abs($this->_curDepth - $this->_depth);
// get the name and url of the nav item
$name = parent::key();
$url = parent::current();
// close previous nested levels
if ($this->_curDepth < $this->_depth) {
$output .= str_repeat('</ul></li>', $diff);
}
// check if we have the last nav item
if ($this->hasNext()) {
$output .= '<li><a href="' . $url . '">' . $name . '</a>';
} else {
$output .= '<li class="last"><a href="' . $url . '">' . $name . '</a>';
}
// either add a subnav or close the list item
if ($this->hasChildren()) {
$output .= '<ul>';
} else {
$output .= '</li>';
}
// cache the depth
$this->_depth = $this->_curDepth;
// return the output ( we could've also overridden current())
return $output;
}
}
//======
// usage
//======
try {
// generate the recursive caching iterator
$it = new RecursiveCachingIterator(new RecursiveArrayIterator($nav));
// build the navigation with the iterator
$it = new NavBuilder($it, RecursiveIteratorIterator::SELF_FIRST);
// display the resulting navigation
echo '<ul id="nav">' . PHP_EOL;
foreach ($it as $value) {
echo $value . "\n";
}
echo '</ul>' . PHP_EOL;
} catch (Exception $e) {
var_dump($e); die;
}