Recursive DOM Iterator
Posted: Fri Oct 05, 2007 10:42 pm
I'm trying to get SPL's RecursiveIterator to play nicely with DOM, but so far I've been unsuccessful. Anyone know why?
Code: Select all
<?php
class DOMIterator implements RecursiveIterator
{
protected $node;
protected $index = 0;
public function __construct(DOMNode $node) {
$this->node = $node;
}
public function key() {
return $this->index;
}
public function current() {var_dump($this);
if (!$this->valid()) return false;
return $this->node->childNodes->item($this->index);
}
public function next() {
if ($this->valid()) {
return $this->node->childNodes->item(++$this->index);
} else {
return false;
}
}
public function rewind() {
$this->index = 0;
if ($this->valid()) return false;
else return $this->current();
}
public function valid() {
return $this->node->childNodes->length > $this->index;
}
public function hasChildren() {
return (bool) $this->node->childNodes->length;
}
public function getChildren() {
return new DOMIterator($this->current());
}
}