File tree Expand file tree Collapse file tree 3 files changed +88
-1
lines changed Expand file tree Collapse file tree 3 files changed +88
-1
lines changed Original file line number Diff line number Diff line change @@ -210,6 +210,41 @@ public function getChild($id)
210210 return $ this ->children [$ id ]['node ' ];
211211 }
212212
213+ /**
214+ * Returns a new array of child nodes
215+ *
216+ * @return array
217+ */
218+ public function getChildren ()
219+ {
220+ $ nodes = [];
221+ try
222+ {
223+ $ child = $ this ->firstChild ();
224+ do
225+ {
226+ $ nodes [] = $ child ;
227+ $ child = $ this ->nextChild ($ child ->id ());
228+ } while ( ! is_null ($ child ));
229+ }
230+ catch (ChildNotFoundException $ e )
231+ {
232+ // we are done looking for children
233+ }
234+
235+ return $ nodes ;
236+ }
237+
238+ /**
239+ * Counts children
240+ *
241+ * @return int
242+ */
243+ public function countChildren ()
244+ {
245+ return count ($ this ->children );
246+ }
247+
213248 /**
214249 * Adds a child node to this node and returns the id of the child for this
215250 * parent.
Original file line number Diff line number Diff line change 1+ <?php
2+ namespace PHPHtmlParser \Dom ;
3+
4+ use Countable ;
5+ use ArrayIterator ;
6+ use IteratorAggregate ;
7+ use PHPHtmlParser \Exceptions \IncorrectChildMethodException ;
8+
9+ /**
10+ * Dom node object which will allow users to use it as
11+ * an array.
12+ */
13+ abstract class ArrayNode extends AbstractNode implements IteratorAggregate, Countable {
14+
15+ /**
16+ * Returns the array to be used the the iterator.
17+ *
18+ * @return array
19+ */
20+ abstract protected function getIteratorArray ();
21+
22+ /**
23+ * Gets the iterator
24+ *
25+ * @return ArrayIterator
26+ */
27+ public function getIterator ()
28+ {
29+ return new ArrayIterator ($ this ->getIteratorArray ());
30+ }
31+
32+ /**
33+ * Returns the count of the iterator array.
34+ *
35+ * @return int
36+ */
37+ public function count ()
38+ {
39+ return count ($ this ->getIteratorArray ());
40+ }
41+
42+ }
Original file line number Diff line number Diff line change 44use PHPHtmlParser \Exceptions \UnknownChildTypeException ;
55use PHPHtmlParser \Exceptions \ChildNotFoundException ;
66
7- class HtmlNode extends AbstractNode {
7+ class HtmlNode extends ArrayNode {
88
99 /**
1010 * Remembers what the innerHtml was if it was scaned previously.
@@ -202,4 +202,14 @@ protected function clear()
202202 $ this ->outerHtml = null ;
203203 $ this ->text = null ;
204204 }
205+
206+ /**
207+ * Returns all children of this html node.
208+ *
209+ * @return array
210+ */
211+ protected function getIteratorArray ()
212+ {
213+ return $ this ->getChildren ();
214+ }
205215}
You can’t perform that action at this time.
0 commit comments