FrameTreeIterator.php
1.52 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Dompdf\Frame;
use Iterator;
use Dompdf\Frame;
/**
* Pre-order Iterator
*
* Returns frames in preorder traversal order (parent then children)
*
* @access private
* @package dompdf
*/
class FrameTreeIterator implements Iterator
{
/**
* @var Frame
*/
protected $_root;
/**
* @var array
*/
protected $_stack = array();
/**
* @var int
*/
protected $_num;
/**
* @param Frame $root
*/
public function __construct(Frame $root)
{
$this->_stack[] = $this->_root = $root;
$this->_num = 0;
}
/**
*
*/
public function rewind()
{
$this->_stack = array($this->_root);
$this->_num = 0;
}
/**
* @return bool
*/
public function valid()
{
return count($this->_stack) > 0;
}
/**
* @return int
*/
public function key()
{
return $this->_num;
}
/**
* @return Frame
*/
public function current()
{
return end($this->_stack);
}
/**
* @return Frame
*/
public function next()
{
$b = end($this->_stack);
// Pop last element
unset($this->_stack[key($this->_stack)]);
$this->_num++;
// Push all children onto the stack in reverse order
if ($c = $b->get_last_child()) {
$this->_stack[] = $c;
while ($c = $c->get_prev_sibling()) {
$this->_stack[] = $c;
}
}
return $b;
}
}