-
Notifications
You must be signed in to change notification settings - Fork 398
/
PreOrderNodeIterator.php
84 lines (65 loc) · 1.79 KB
/
PreOrderNodeIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Runtime\ActiveRecord;
/**
* Pre-order node iterator for Node objects.
*
* @author Dave Lawson <dlawson@masterytech.com>
*/
class PreOrderNodeIterator implements \Iterator
{
private $topNode = null;
private $curNode = null;
private $querydb = false;
private $con = null;
public function __construct($node, $opts)
{
$this->topNode = $node;
$this->curNode = $node;
if (isset($opts['con'])) {
$this->con = $opts['con'];
}
if (isset($opts['querydb'])) {
$this->querydb = $opts['querydb'];
}
}
public function rewind()
{
$this->curNode = $this->topNode;
}
public function valid()
{
return null !== $this->curNode;
}
public function current()
{
return $this->curNode;
}
public function key()
{
return $this->curNode->getNodePath();
}
public function next()
{
if ($this->valid()) {
$nextNode = $this->curNode->getFirstChildNode($this->querydb, $this->con);
while (null === $nextNode) {
if (null === $this->curNode || $this->curNode->equals($this->topNode)) {
break;
}
$nextNode = $this->curNode->getSiblingNode(false, $this->querydb, $this->con);
if (null === $nextNode) {
$this->curNode = $this->curNode->getParentNode($this->querydb, $this->con);
}
}
$this->curNode = $nextNode;
}
return $this->curNode;
}
}