2012-06-25 13:03:14 +00:00
|
|
|
<?php
|
|
|
|
class NodeIterator {
|
2012-07-02 16:55:13 +00:00
|
|
|
/*---------------------------
|
|
|
|
The NodeIterator can be used just like a foreach() loop to iterate
|
|
|
|
arrays.
|
|
|
|
|
|
|
|
Sample:
|
|
|
|
$iter = new NodeIterator(array());
|
|
|
|
while($iter->hasNext()) {
|
|
|
|
$curr = $iter->getNext();
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
---------------------------*/
|
2012-07-08 16:11:25 +00:00
|
|
|
private $node;
|
|
|
|
#private $curr;
|
2012-06-25 13:03:14 +00:00
|
|
|
|
2012-07-08 16:11:25 +00:00
|
|
|
function NodeIterator($node) {
|
|
|
|
$this->node = $node;
|
|
|
|
#$this->curr = 0;
|
2012-06-25 13:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hasNext() {
|
2012-07-08 16:11:25 +00:00
|
|
|
#$tmp = array_keys($this->nodes);
|
|
|
|
#return isset($this->nodes[$tmp[$this->curr]]);
|
|
|
|
if($this->node == null) {
|
|
|
|
#echo "empty";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if($this->node->getChild() == null) {
|
|
|
|
# return false;
|
|
|
|
#}
|
2012-06-25 13:03:14 +00:00
|
|
|
|
2012-07-08 16:11:25 +00:00
|
|
|
#echo "true";
|
|
|
|
|
|
|
|
return true;
|
2012-06-25 13:03:14 +00:00
|
|
|
}
|
|
|
|
|
2012-07-08 16:11:25 +00:00
|
|
|
function getNext() {
|
|
|
|
#$tmp = array_keys($this->nodes);
|
|
|
|
#return $this->nodes[$tmp[$this->curr++]];
|
|
|
|
$n = $this->node;
|
|
|
|
$this->node = $this->node->getChild();
|
|
|
|
return $n->data;
|
2012-06-25 13:03:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|