1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2019 Sebastian Böttger <[email protected]> |
5
|
|
|
* You may use, distribute and modify this code under the |
6
|
|
|
* terms of the MIT license. |
7
|
|
|
* |
8
|
|
|
* You should have received a copy of the MIT license with |
9
|
|
|
* this file. If not, please visit: https://opensource.org/licenses/mit-license.php |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Seboettg\Forest; |
13
|
|
|
|
14
|
|
|
use Countable; |
15
|
|
|
use ErrorException; |
16
|
|
|
use Seboettg\Collection\Stack; |
17
|
|
|
use Seboettg\Forest\General\TreeNode; |
18
|
|
|
use Seboettg\Forest\General\TreeNodeInterface; |
19
|
|
|
use Seboettg\Forest\General\TreeTraversalInterface; |
20
|
|
|
use Seboettg\Forest\General\TreeTraversalTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Builder |
24
|
|
|
* @package Seboettg\Forest |
25
|
|
|
*/ |
26
|
|
|
class GeneralTree implements Countable, TreeTraversalInterface |
27
|
|
|
{ |
28
|
|
|
use TreeTraversalTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Stack |
32
|
|
|
*/ |
33
|
|
|
private $treeNodeStack; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $itemType; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
private $elementCount = 0; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var TreeNodeInterface |
47
|
|
|
*/ |
48
|
|
|
private $root; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* GeneralTree constructor. |
52
|
|
|
* @param string $itemType |
53
|
|
|
* @throws ErrorException |
54
|
|
|
*/ |
55
|
4 |
|
public function __construct(string $itemType) |
56
|
|
|
{ |
57
|
4 |
|
if (!class_exists($itemType)) { |
58
|
|
|
throw new ErrorException("Could not find class $itemType"); |
59
|
|
|
} |
60
|
4 |
|
$this->itemType = $itemType; |
61
|
4 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param mixed $value |
65
|
|
|
* @return GeneralTree |
66
|
|
|
*/ |
67
|
4 |
|
public function root($value) |
68
|
|
|
{ |
69
|
4 |
|
$this->treeNodeStack = new Stack(); |
70
|
4 |
|
$this->root = $this->nodeFactory($value); |
71
|
4 |
|
$this->treeNodeStack->push($this->root); |
72
|
4 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Add a child to the node enter in its scope |
77
|
|
|
* |
78
|
|
|
* @param mixed $value |
79
|
|
|
* @return GeneralTree |
80
|
|
|
*/ |
81
|
4 |
|
public function subTree($value) |
82
|
|
|
{ |
83
|
4 |
|
$node = $this->nodeFactory($value); |
84
|
4 |
|
$this->treeNodeStack->peek()->addChild($node); |
85
|
4 |
|
$this->treeNodeStack->push($node); |
86
|
4 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param mixed $value |
91
|
|
|
* @return GeneralTree |
92
|
|
|
*/ |
93
|
4 |
|
public function child($value) |
94
|
|
|
{ |
95
|
4 |
|
$node = $this->nodeFactory($value); |
96
|
4 |
|
$this->treeNodeStack->peek()->addChild($node); |
97
|
4 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return GeneralTree |
102
|
|
|
*/ |
103
|
4 |
|
public function endSubTree() |
104
|
|
|
{ |
105
|
4 |
|
$this->treeNodeStack->pop(); |
106
|
4 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param mixed $value |
111
|
|
|
* @return TreeNodeInterface |
112
|
|
|
*/ |
113
|
4 |
|
protected function nodeFactory($value): TreeNodeInterface |
114
|
|
|
{ |
115
|
4 |
|
++$this->elementCount; |
116
|
4 |
|
if (is_object($value) && get_class($value) === $this->itemType) { |
117
|
1 |
|
return new TreeNode($value); |
118
|
|
|
} |
119
|
4 |
|
$item = new $this->itemType($value); |
120
|
4 |
|
return new TreeNode($item); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return TreeNodeInterface |
125
|
|
|
*/ |
126
|
2 |
|
public function getNode(): ?TreeNodeInterface |
127
|
|
|
{ |
128
|
2 |
|
return $this->treeNodeStack->peek(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return TreeNodeInterface |
133
|
|
|
*/ |
134
|
|
|
final public function getRoot(): ?TreeNodeInterface |
135
|
|
|
{ |
136
|
|
|
return $this->root; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritDoc |
141
|
|
|
*/ |
142
|
1 |
|
final public function count(): int |
143
|
|
|
{ |
144
|
1 |
|
return $this->elementCount; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|