| Total Complexity | 9 |
| Total Lines | 99 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | class NodeStack |
||
| 6 | { |
||
| 7 | |||
| 8 | /** @var Node[] */ |
||
| 9 | protected $stack = []; |
||
| 10 | |||
| 11 | /** @var int index to the top of the stack */ |
||
| 12 | protected $stacklength = -1; |
||
| 13 | |||
| 14 | /** @var Node the root node */ |
||
| 15 | protected $doc; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * NodeStack constructor. |
||
| 19 | */ |
||
| 20 | public function __construct() |
||
| 21 | { |
||
| 22 | $node = new Node('doc'); |
||
| 23 | $this->doc = $node; |
||
| 24 | $this->top($node); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Get the current node (the one at the top of the stack) |
||
| 29 | * |
||
| 30 | * @return Node |
||
| 31 | */ |
||
| 32 | public function current() |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get the document (top most level) node |
||
| 39 | * |
||
| 40 | * @return Node |
||
| 41 | */ |
||
| 42 | public function doc() |
||
| 43 | { |
||
| 44 | return $this->doc; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Make the given node the current one |
||
| 49 | * |
||
| 50 | * @param Node $node |
||
| 51 | */ |
||
| 52 | protected function top(Node $node) |
||
| 53 | { |
||
| 54 | $this->stack[] = $node; |
||
| 55 | $this->stacklength++; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Add a new child node to the current node and make it the new current node |
||
| 60 | * |
||
| 61 | * @param Node $node |
||
| 62 | */ |
||
| 63 | public function addTop(Node $node) |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Pop the current node off the stack |
||
| 71 | * |
||
| 72 | * @param string $type The type of node that is expected. A RuntimeException is thrown if the current nod does not |
||
| 73 | * match |
||
| 74 | * |
||
| 75 | * @return Node |
||
| 76 | */ |
||
| 77 | public function drop($type) |
||
| 78 | { |
||
| 79 | /** @var Node $node */ |
||
| 80 | $node = array_pop($this->stack); |
||
| 81 | $this->stacklength--; |
||
| 82 | if ($node->getType() != $type) { |
||
| 83 | throw new \RuntimeException("Expected the current node to be of type $type found " . $node->getType() . " instead."); |
||
| 84 | } |
||
| 85 | return $node; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Add a new child node to the current node |
||
| 90 | * |
||
| 91 | * @param Node $node |
||
| 92 | */ |
||
| 93 | public function add(Node $node) |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Check if there have been any nodes added to the document |
||
| 100 | */ |
||
| 101 | public function isEmpty() |
||
| 106 |