Total Complexity | 8 |
Total Lines | 58 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
9 | class Tree |
||
10 | { |
||
11 | private $nextNodeId = 1; |
||
12 | |||
13 | /** |
||
14 | * @var Node[] |
||
15 | */ |
||
16 | private $nodeMap = []; |
||
17 | |||
18 | private $rootNodeId; |
||
19 | |||
20 | public function createNode(string $name, int $id = null): Node |
||
21 | { |
||
22 | $node = new Node($id ?? $this->getNextNodeId(), $name); |
||
23 | $this->nodeMap[$node->getId()] = $node; |
||
24 | return $node; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @param int $id |
||
29 | * @return Node |
||
30 | * @throws Exception |
||
31 | */ |
||
32 | public function getNode(int $id): Node |
||
33 | { |
||
34 | if (!isset($this->nodeMap[$id])) { |
||
35 | throw new Exception("Node {$id} is not defined in syntax tree"); |
||
36 | } |
||
37 | return $this->nodeMap[$id]; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param Node $node |
||
42 | * @throws Exception |
||
43 | */ |
||
44 | public function setRootNode(Node $node): void |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return Node |
||
54 | * @throws Exception |
||
55 | */ |
||
56 | public function getRootNode(): Node |
||
62 | } |
||
63 | |||
64 | private function getNextNodeId(): int |
||
67 | } |
||
68 | } |
||
69 |