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 | 10 | function __construct(Driver $driver, Locator $locator, Node $parent) |
|
|
|||
46 | { |
||
47 | 10 | $this->_driver = $driver; |
|
48 | 10 | $this->_locator = $locator; |
|
49 | 10 | $this->_parent = $parent; |
|
50 | $this->_node = new Node($driver, $parent); |
||
51 | } |
||
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 | $nodes[] = $node->html(); |
||
64 | } |
||
65 | 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 | public function current() |
||
84 | { |
||
85 | $ids = $this->list_ids(); |
||
86 | return $this->_load($ids[$this->_current]); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Implementation of the Iterator interface |
||
91 | * @return int |
||
92 | */ |
||
93 | public function key() |
||
94 | { |
||
95 | return $this->_current; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Implementation of the Iterator interface |
||
100 | * @return Nodelist |
||
101 | */ |
||
102 | public function next() |
||
103 | { |
||
104 | ++$this->_current; |
||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Implementation of the Iterator interface |
||
110 | * |
||
111 | * @return boolean |
||
112 | */ |
||
113 | public function valid() |
||
114 | { |
||
115 | return $this->offsetExists($this->_current); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Implementation of the Countable interface |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | public function count() |
||
124 | { |
||
125 | return count($this->list_ids()); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Implementation of SeekableIterator |
||
130 | * |
||
131 | * @param mixed $offset |
||
132 | * @return boolean |
||
133 | */ |
||
134 | 1 | public function seek($offset) |
|
135 | { |
||
136 | if ($this->offsetExists($offset)) |
||
137 | { |
||
138 | $this->_current = $offset; |
||
139 | return TRUE; |
||
140 | } |
||
141 | 1 | return FALSE; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * ArrayAccess: offsetExists |
||
146 | * |
||
147 | * @param mixed $offset |
||
148 | * @return boolean |
||
149 | */ |
||
150 | public function offsetExists($offset) |
||
151 | { |
||
152 | return ($offset >= 0 AND $offset < $this->count()); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * ArrayAccess: offsetGet |
||
157 | * |
||
158 | * @param mixed $offset |
||
159 | * @return Node |
||
160 | */ |
||
161 | public function offsetGet($offset) |
||
162 | { |
||
163 | if ( ! $this->offsetExists($offset)) |
||
164 | return NULL; |
||
165 | $ids = $this->list_ids(); |
||
166 | 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 | public function offsetSet($offset, $value) |
||
178 | { |
||
179 | throw new Exception('Cannot modify Nodelist'); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * ArrayAccess: offsetUnset |
||
184 | * |
||
185 | * @throws Kohana_Exception |
||
186 | * @param mixed $offset |
||
187 | * @return void |
||
188 | */ |
||
189 | public function offsetUnset($offset) |
||
190 | { |
||
191 | throw new Exception('Cannot modify Nodelist'); |
||
192 | } |
||
193 | |||
194 | protected function _load($id) |
||
195 | { |
||
196 | return $this->_node->load_new_id($id); |
||
197 | } |
||
198 | |||
199 | 4 | protected function list_ids() |
|
200 | { |
||
201 | 4 | if ($this->_list_ids === NULL) |
|
202 | { |
||
203 | $this->_list_ids = (array) $this->_driver->all($this->_locator->xpath(), $this->_parent->id()); |
||
204 | |||
205 | if ($this->_locator->filters()) |
||
206 | { |
||
207 | 1 | foreach ($this->_list_ids as $offset => $id) |
|
208 | { |
||
209 | if ( ! $this->_locator->is_filtered($this->_load($id), $offset)) |
||
210 | { |
||
211 | unset($this->_list_ids[$offset]); |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | |||
217 | 4 | return $this->_list_ids; |
|
218 | } |
||
219 | |||
220 | 1 | public function locator() |
|
224 | |||
225 | 1 | public function driver() |
|
229 | |||
230 | 3 | public function first() |
|
231 | { |
||
232 | $ids = $this->list_ids(); |
||
233 | |||
234 | if (count($ids) <= 0) |
||
235 | 3 | return NULL; |
|
236 | |||
237 | return $this->_load(reset($ids)); |
||
238 | } |
||
239 | |||
240 | public function last() |
||
241 | { |
||
242 | $ids = $this->list_ids(); |
||
243 | |||
244 | if (count($ids) <= 0) |
||
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.