1 | <?php |
||
13 | class Nodelist implements \Iterator, \Countable, \SeekableIterator, \ArrayAccess { |
||
14 | |||
15 | /** |
||
16 | * The driver for traversing this node and its children |
||
17 | * @var Driver |
||
18 | */ |
||
19 | protected $_driver; |
||
20 | |||
21 | /** |
||
22 | * The parent node, NULL if root |
||
23 | * @var Node |
||
24 | */ |
||
25 | protected $_parent; |
||
26 | |||
27 | /** |
||
28 | * Cache for the ids of this node list |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $_list_ids; |
||
32 | |||
33 | /** |
||
34 | * Single node object to be used during iteration |
||
35 | * @var Node |
||
36 | */ |
||
37 | protected $_node; |
||
38 | |||
39 | /** |
||
40 | * Implementing Iterator |
||
41 | * @var integer |
||
42 | */ |
||
43 | protected $_current = 0; |
||
44 | |||
45 | 18 | function __construct(Driver $driver, Locator $locator, Node $parent) |
|
52 | |||
53 | /** |
||
54 | * Returns a string representation of the collection. |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | 1 | public function __toString() |
|
59 | { |
||
60 | 1 | $nodes = array(); |
|
61 | 1 | foreach($this as $node) |
|
62 | { |
||
63 | 1 | $nodes[] = $node->html(); |
|
64 | } |
||
65 | 1 | return "Nodelist: \nLocator: {$this->locator()} \nContent: [\n".join("\n", $nodes)."\n]\n"; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Implementation of the Iterator interface |
||
70 | * @return Nodelist |
||
71 | */ |
||
72 | 2 | public function rewind() |
|
77 | |||
78 | /** |
||
79 | * Implementation of the Iterator interface |
||
80 | * |
||
81 | * @return Node |
||
82 | */ |
||
83 | 2 | public function current() |
|
88 | |||
89 | /** |
||
90 | * Implementation of the Iterator interface |
||
91 | * @return int |
||
92 | */ |
||
93 | 1 | public function key() |
|
97 | |||
98 | /** |
||
99 | * Implementation of the Iterator interface |
||
100 | * @return Nodelist |
||
101 | */ |
||
102 | 2 | public function next() |
|
107 | |||
108 | /** |
||
109 | * Implementation of the Iterator interface |
||
110 | * |
||
111 | * @return boolean |
||
112 | */ |
||
113 | 2 | public function valid() |
|
117 | |||
118 | /** |
||
119 | * Implementation of the Countable interface |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | 3 | public function count() |
|
127 | |||
128 | /** |
||
129 | * Implementation of SeekableIterator |
||
130 | * |
||
131 | * @param mixed $offset |
||
132 | * @return boolean |
||
133 | */ |
||
134 | 1 | public function seek($offset) |
|
135 | { |
||
136 | 1 | if ($this->offsetExists($offset)) |
|
137 | { |
||
138 | 1 | $this->_current = $offset; |
|
139 | 1 | return TRUE; |
|
140 | } |
||
141 | 1 | return FALSE; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * ArrayAccess: offsetExists |
||
146 | * |
||
147 | * @param mixed $offset |
||
148 | * @return boolean |
||
149 | */ |
||
150 | 3 | public function offsetExists($offset) |
|
154 | |||
155 | /** |
||
156 | * ArrayAccess: offsetGet |
||
157 | * |
||
158 | * @param mixed $offset |
||
159 | * @return Node |
||
160 | */ |
||
161 | 1 | public function offsetGet($offset) |
|
162 | { |
||
163 | 1 | if ( ! $this->offsetExists($offset)) |
|
164 | return NULL; |
||
165 | 1 | $ids = $this->list_ids(); |
|
166 | 1 | return $this->_load($ids[$offset]); |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * ArrayAccess: offsetSet |
||
171 | * |
||
172 | * @throws Kohana_Exception |
||
173 | * @param mixed $offset |
||
174 | * @param mixed $value |
||
175 | * @return void |
||
176 | */ |
||
177 | 1 | public function offsetSet($offset, $value) |
|
181 | |||
182 | /** |
||
183 | * ArrayAccess: offsetUnset |
||
184 | * |
||
185 | * @throws Kohana_Exception |
||
186 | * @param mixed $offset |
||
187 | * @return void |
||
188 | */ |
||
189 | 1 | public function offsetUnset($offset) |
|
193 | |||
194 | 14 | protected function _load($id) |
|
198 | |||
199 | 15 | protected function list_ids() |
|
200 | { |
||
201 | 15 | if ($this->_list_ids === NULL) |
|
202 | { |
||
203 | 15 | $this->_list_ids = (array) $this->_driver->all($this->_locator->xpath(), $this->_parent->id()); |
|
204 | |||
205 | 15 | if ($this->_locator->filters()) |
|
206 | { |
||
207 | 2 | foreach ($this->_list_ids as $offset => $id) |
|
208 | { |
||
209 | 2 | if ( ! $this->_locator->is_filtered($this->_load($id), $offset)) |
|
210 | { |
||
211 | 2 | unset($this->_list_ids[$offset]); |
|
212 | } |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | |||
217 | 15 | return $this->_list_ids; |
|
218 | } |
||
219 | |||
220 | 2 | public function locator() |
|
224 | |||
225 | 1 | public function driver() |
|
229 | |||
230 | 12 | public function first() |
|
239 | |||
240 | 1 | public function last() |
|
241 | { |
||
242 | 1 | $ids = $this->list_ids(); |
|
243 | |||
249 | |||
250 | 1 | public function as_array() |
|
259 | |||
260 | } |
||
261 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.