1 | <?php |
||
5 | class TreeParser |
||
6 | { |
||
7 | private $indentation = 2; |
||
8 | |||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | private $tree; |
||
13 | |||
14 | /** |
||
15 | * @var int |
||
16 | */ |
||
17 | private $initialSpaces; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $structure; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $orderedNodes; |
||
28 | |||
29 | /** |
||
30 | * TreeParser constructor. |
||
31 | * @param string $tree |
||
32 | * |
||
33 | * <<<TREE |
||
34 | * Root |
||
35 | * |- Level 1 - Order 1 |
||
36 | * |- Level 2 - Order 2 |
||
37 | * |- Level 3 - Order 3 |
||
38 | * |- Level 3 - Order 4 |
||
39 | * |- Level 2 - Order 5 |
||
40 | * |- Level 1 - Order 6 |
||
41 | * |- Level 2 - Order 7 |
||
42 | * |- Level 3 - Order 8 |
||
43 | * |- Level 4 - Order 9 |
||
44 | * TREE; |
||
45 | */ |
||
46 | 12 | public function __construct($tree) |
|
50 | |||
51 | 12 | public function parse() |
|
73 | |||
74 | public function setTree($tree) |
||
78 | |||
79 | public function getTree() |
||
83 | |||
84 | /** |
||
85 | * @return array |
||
86 | */ |
||
87 | 3 | public function getOrderedNodes() |
|
91 | |||
92 | /** |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getStructure() |
||
99 | |||
100 | /** |
||
101 | * @param int $indentation |
||
102 | * @return TreeParser |
||
103 | */ |
||
104 | 3 | public function setIndentation($indentation) |
|
105 | { |
||
106 | 3 | $this->indentation = $indentation; |
|
107 | 3 | return $this; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param TreeNode $node |
||
112 | * @return TreeNode|null |
||
113 | */ |
||
114 | 9 | public function getParentForNode(TreeNode $node) |
|
134 | |||
135 | /** |
||
136 | * @param $line |
||
137 | * @return int |
||
138 | */ |
||
139 | 12 | private function numberOfSpaces($line) |
|
143 | |||
144 | /** |
||
145 | * @param $numberOfSpaces |
||
146 | * @return int |
||
147 | * @throws InvalidNumberOfSpaces |
||
148 | */ |
||
149 | 12 | private function spacesToLevel($numberOfSpaces) |
|
150 | { |
||
151 | 12 | if ($numberOfSpaces === $this->initialSpaces) { |
|
152 | 12 | return 0; |
|
153 | } |
||
154 | |||
155 | 12 | if (($numberOfSpaces - $this->initialSpaces) % $this->indentation !== 0) { |
|
156 | 3 | throw new InvalidNumberOfSpaces( |
|
157 | 3 | "Make sure children's leading spaces being a multiple of {$this->indentation}" |
|
158 | 3 | ); |
|
159 | } |
||
160 | |||
161 | 9 | return (int) ($numberOfSpaces - $this->initialSpaces) / $this->indentation; |
|
162 | } |
||
163 | |||
164 | 12 | private function lineToNode($line) |
|
176 | |||
177 | 12 | private function setStructureAndOrderedNodes(array $lines) |
|
202 | } |
||
203 |