1 : <?php
2 : /**
3 : * FluentDOMIterator is the Iterator class for FluentDOM objects
4 : *
5 : * @version $Id: Iterator.php 345 2009-10-19 19:51:37Z subjective $
6 : * @license http://www.opensource.org/licenses/mit-license.php The MIT License
7 : * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert
8 : *
9 : * @package FluentDOM
10 : */
11 :
12 : /**
13 : * FluentDOMIterator is the Iterator class for FluentDOM objects
14 : *
15 : * @package FluentDOM
16 : */
17 : class FluentDOMIterator implements RecursiveIterator, SeekableIterator {
18 :
19 : /**
20 : * internal position pointer variable
21 : * @var integer
22 : */
23 : private $_position = 0;
24 :
25 : /**
26 : * owner (object) of the iterator
27 : * @var FluentDOM
28 : */
29 : private $_owner = NULL;
30 :
31 : /**
32 : * Remember the owner object (the FluentDOM object this iterator interates)
33 : *
34 : * @param $owner
35 : * @access public
36 : * @return FluentDOM
37 : */
38 : public function __construct($owner) {
39 20 : $this->_owner = $owner;
40 20 : }
41 :
42 : /*
43 : * Interface - Iterator, SeekableIterator
44 : */
45 :
46 : /**
47 : * Get current iterator element
48 : *
49 : * @access public
50 : * @return object DOMNode
51 : */
52 : public function current() {
53 11 : return $this->_owner->item($this->_position);
54 : }
55 :
56 : /**
57 : * Get current iterator pointer
58 : *
59 : * @access public
60 : * @return integer
61 : */
62 : public function key() {
63 2 : return $this->_position;
64 : }
65 :
66 : /**
67 : * Move iterator pointer to next element
68 : *
69 : * @access public
70 : * @return void
71 : */
72 : public function next() {
73 11 : ++$this->_position;
74 11 : }
75 :
76 : /**
77 : * Reset iterator pointer
78 : *
79 : * @access public
80 : * @return void
81 : */
82 : public function rewind() {
83 12 : $this->_position = 0;
84 12 : }
85 :
86 : /**
87 : * Move iterator pointer to specified element
88 : *
89 : * @param integer $position
90 : * @access public
91 : * @return void
92 : */
93 : public function seek($position) {
94 2 : if (count($this->_owner) > $position) {
95 1 : $this->_position = $position;
96 1 : } else {
97 1 : throw new InvalidArgumentException('Unknown position');
98 : }
99 1 : }
100 :
101 : /**
102 : * Check if current iterator pointer contains a valid element
103 : *
104 : * @access public
105 : * @return boolean
106 : */
107 : public function valid() {
108 12 : return is_object($this->_owner->item($this->_position));
109 : }
110 :
111 : /**
112 : * Get children of the current iterator element
113 : *
114 : * @access public
115 : * @return object FluentDOM
116 : */
117 : public function getChildren() {
118 2 : return $this->_owner->eq($this->_position)->find('node()')->getIterator();
119 : }
120 :
121 : /**
122 : * Check if the current iterator element has children
123 : *
124 : * @access public
125 : * @return object FluentDOM
126 : */
127 : public function hasChildren() {
128 2 : return (count($this->_owner->eq($this->_position)->find('node()')) > 0);
129 : }
|