1 : <?php
2 : /**
3 : * FluentDOMIterator is the Iterator class for FluentDOMCore objects
4 : *
5 : * @version $Id: Iterator.php 431 2010-03-29 20:42:04Z 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 FluentDOMCore 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 FluentDOMCore
28 : */
29 : private $_owner = NULL;
30 :
31 : /**
32 : * Remember the owner object (the FluentDOMCore object this iterator interates)
33 : *
34 : * @param FluentDOMCore $owner
35 : * @return FluentDOMCore
36 : */
37 : public function __construct(FluentDOMCore $owner) {
38 9 : $this->_owner = $owner;
39 9 : }
40 :
41 : /*
42 : * Interface - Iterator, SeekableIterator
43 : */
44 :
45 : /**
46 : * Get current iterator element
47 : *
48 : * @return object DOMNode
49 : */
50 : public function current() {
51 1 : return $this->_owner->item($this->_position);
52 : }
53 :
54 : /**
55 : * Get current iterator pointer
56 : *
57 : * @return integer
58 : */
59 : public function key() {
60 1 : return $this->_position;
61 : }
62 :
63 : /**
64 : * Move iterator pointer to next element
65 : *
66 : * @return void
67 : */
68 : public function next() {
69 2 : ++$this->_position;
70 2 : }
71 :
72 : /**
73 : * Reset iterator pointer
74 : *
75 : * @return void
76 : */
77 : public function rewind() {
78 1 : $this->_position = 0;
79 1 : }
80 :
81 : /**
82 : * Move iterator pointer to specified element
83 : *
84 : * @param integer $position
85 : * @return void
86 : */
87 : public function seek($position) {
88 2 : if (count($this->_owner) > $position) {
89 1 : $this->_position = $position;
90 1 : } else {
91 1 : throw new InvalidArgumentException('Unknown position');
92 : }
93 1 : }
94 :
95 : /**
96 : * Check if current iterator pointer contains a valid element
97 : *
98 : * @return boolean
99 : */
100 : public function valid() {
101 1 : return is_object($this->_owner->item($this->_position));
102 : }
103 :
104 : /**
105 : * Get children of the current iterator element
106 : *
107 : * @return object FluentDOMCore
108 : */
109 : public function getChildren() {
110 1 : $fd = $this->_owner->spawn();
111 1 : $fd->push($this->_owner->item($this->_position)->childNodes);
112 1 : return new self($fd);
113 : }
114 :
115 : /**
116 : * Check if the current iterator element has children
117 : *
118 : * @return object FluentDOMCore
119 : */
120 : public function hasChildren() {
121 1 : $item = $this->_owner->item($this->_position);
122 1 : return $item->hasChildNodes();
123 : }
|