1 | <?php |
||
5 | class NodeWalker |
||
6 | { |
||
7 | /** |
||
8 | * @var Node |
||
9 | */ |
||
10 | private $root; |
||
11 | |||
12 | /** |
||
13 | * @var Node |
||
14 | */ |
||
15 | private $current; |
||
16 | |||
17 | /** |
||
18 | * @var bool |
||
19 | */ |
||
20 | private $entering; |
||
21 | |||
22 | /** |
||
23 | * @param Node $root |
||
24 | */ |
||
25 | 1941 | public function __construct(Node $root) |
|
31 | |||
32 | /** |
||
33 | * Returns an event which contains node and entering flag |
||
34 | * (entering is true when we enter a Node from a parent or sibling, |
||
35 | * and false when we reenter it from child) |
||
36 | * |
||
37 | * @return NodeWalkerEvent|null |
||
38 | */ |
||
39 | 1941 | public function next() |
|
40 | { |
||
41 | 1941 | $current = $this->current; |
|
42 | 1941 | $entering = $this->entering; |
|
43 | 1941 | if (null === $current) { |
|
44 | 1941 | return; |
|
45 | } |
||
46 | |||
47 | 1941 | if ($entering && $current->isContainer()) { |
|
48 | 1941 | if ($current->firstChild()) { |
|
49 | 1935 | $this->current = $current->firstChild(); |
|
50 | 1935 | $this->entering = true; |
|
51 | } else { |
||
52 | 1941 | $this->entering = false; |
|
53 | } |
||
54 | 1941 | } elseif ($current === $this->root) { |
|
55 | 1941 | $this->current = null; |
|
56 | 1935 | } elseif (null === $current->next()) { |
|
57 | 1935 | $this->current = $current->parent(); |
|
58 | 1935 | $this->entering = false; |
|
59 | } else { |
||
60 | 504 | $this->current = $current->next(); |
|
61 | 504 | $this->entering = true; |
|
62 | } |
||
63 | |||
64 | 1941 | return new NodeWalkerEvent($current, $entering); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Resets the iterator to resume at the specified node |
||
69 | * |
||
70 | * @param Node $node |
||
71 | * @param bool $entering |
||
72 | */ |
||
73 | public function resumeAt(Node $node, $entering = true) |
||
78 | } |
||
79 |