-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTree.php
More file actions
321 lines (294 loc) · 8.16 KB
/
Copy pathTree.php
File metadata and controls
321 lines (294 loc) · 8.16 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
namespace ezTreeBuilder;
use ezTreeBuilder\Base\Branch;
use ezTreeBuilder\Base\Adapter;
/**
* Tree builder class - generates the enire tree based on \TreeBuilder\Branch
*
* @package ezTreeBuilder
* @author Adrian Tilita <adrian@tilita.ro>
* @version 1.0
*/
class Tree
{
/**
* Set the build mode simple and add a depth field for branches
* @const int
*/
const BUILD_MODE_DEPTH = 0;
/**
* Builds simple mode and add left->right fields
* @const int
*/
const BUILD_MODE_LEFT_RIGHT = 1;
/**
* Builds the full type off tree, containing all off the above
* @const int
*/
const BUILD_MODE_COMPLETE = 2;
/**
* Default parent id from where to start referencing
* @const int
*/
const ROOT_PARENT_ID = 0;
/**
* Wheather to log debug details
* @var boolean
*/
private $debug = false;
/**
* Collected debug data if enabled
* @var string
*/
private $debugData;
/**
* "Dirty" flag - weather to build the tree if it wasn't compiled allready
* @var boolean
*/
private $compiled = false;
/**
* Store all tree items
* @var array - array([ID] => TreeBuilder\Branch, [ID] => TreeBuilder\Branch)
*/
private $branches = array();
/**
* Stores the compiled tree
* @var array
*/
private $compiledTree = array();
/**
* A delagated adapter for the output
* @var Adapter
*/
private $delegatedAdapter;
/**
* Defines the build mode off the tree
* @var int
*/
private $buildMode;
/**
* Enables debug mode
*/
public function enableDebug()
{
$this->debug = true;
}
/**
* Show debug state
* @return boolean
*/
public function isDebugEnabled()
{
return $this->debug;
}
/**
* Get debug data
* @return string
* @throws \Exception
*/
public function getDebugData()
{
if ($this->isDebugEnabled() === false) {
throw new \Exception('Debug is not enabled!');
}
return $this->debugData;
}
/**
* Delegate an adapter for the wanted output
* @param Adapter $adapter
* @return \TreeBuilder\Tree
*/
public function registerAdapter(Adapter $adapter)
{
$this->delegatedAdapter = $adapter;
return $this;
}
/**
* Set the build mode identified by local constants
* @param int $value
* @return \TreeBuilder\Tree
*/
public function setBuildMode($value)
{
$this->buildMode = $value;
return $this;
}
/**
* Add a new TreeItem
* @param Branch $item
*/
public function addBranch(Branch $item)
{
$this->branches[$item->getId()] = $item;
$this->compiled = false;
}
/**
* Returns the builded tree
* @return array
*/
public function getTree()
{
if ($this->compiled === false) {
$this->buildTree();
}
if ($this->delegatedAdapter !== null) {
$this->delegatedAdapter->setRawData($this->branches);
$this->delegatedAdapter->setTree($this->compiledTree);
return $this->delegatedAdapter->adapt();
}
return $this->compiledTree;
}
/**
* Return a branch by it's id
* @param int $branchId
* @return \TreeBuilder\Base\Branch
* @throws \Exception
*/
public function getBranchById($branchId)
{
if (isset($this->branches[$branchId])) {
if ($this->delegatedAdapter !== null) {
$this->delegatedAdapter->setRawData($this->branches);
$this->delegatedAdapter->setTree(array($this->branches[$branchId]));
$adaptedData = $this->delegatedAdapter->adapt();
return isset($adaptedData[0]) ? $adaptedData[0] : $adaptedData;
}
return $this->branches[$branchId];
}
throw new \Exception("Could not find branch with id {$branchId}!");
}
/**
* Return all leaf branches
* @return array
*/
public function getLeafs()
{
$items = array();
foreach ($this->branches as $item) {
if ($item->isLeaf() === true) {
$items[] = $item;
}
}
return $items;
}
/**
* Build the tree
*/
public function buildTree()
{
// reset compiled tree
$this->compiledTree = array();
if ($this->isDebugEnabled()) {
list($startUsec, $startSec) = explode(" ", microtime());
}
// build for simple mode
foreach ($this->branches as $id => $item) {
$this->buildBranchReference($id, $item);
}
// build according to the required build mode
if (!empty($this->compiledTree)) {
switch ($this->buildMode) {
case (self::BUILD_MODE_DEPTH):
$this->addDepth($this->compiledTree);
break;
case (self::BUILD_MODE_LEFT_RIGHT):
$this->addLeftRight($this->compiledTree);
break;
case (self::BUILD_MODE_COMPLETE):
$this->addDepth($this->compiledTree);
$this->addLeftRight($this->compiledTree);
$this->markLeafs($this->compiledTree);
break;
}
}
if ($this->isDebugEnabled()) {
if (count($this->compiledTree)) {
$this->logDebug("No root branch declared!");
}
list($endUsec, $endSec) = explode(" ", microtime());
$diffSec = intval($endSec) - intval($startSec);
$diffUsec = floatval($endUsec) - floatval($startUsec);
$this->logDebug("Compiled tree in " . floatval($diffSec) + $diffUsec);
}
$this->compiled = true;
}
/**
* Build the tree parent->child references
* @param int $branchId
* @param Branch $item
*/
private function buildBranchReference($branchId, Branch $item)
{
if ($item->getParentId() !== self::ROOT_PARENT_ID) {
if (isset($this->branches[$item->getParentId()])) {
$this->branches[$branchId] = $item->setParent($this->branches[$item->getParentId()]);
$this->branches[$item->getParentId()]->addChild($item);
} else {
if ($this->isDebugEnabled()) {
$this->logDebug("Parent of {$item->getId()} with id {$item->getParentId()} is not added."
. "Skipping adding the current branch");
}
}
} else {
$this->compiledTree[] = $item;
}
}
/**
* Set the depth filed for items
* @param array $items
* @param int $depth
*/
private function addDepth(array $items, $depth = 0)
{
foreach ($items as $item) {
$item->setDepth($depth);
if ($item->hasChildren()) {
$this->addDepth($item->getChildren(), $depth + 1);
}
}
}
/**
* Add left->right fields to tree
* @param array $items
* @param int $left
* @return int
*/
private function addLeftRight(array $items, $left = 1)
{
$right = $left+1;
foreach ($items as $item) {
$item->setLeft($left);
if ($item->hasChildren()) {
$right = $this->addLeftRight($item->getChildren(), $left + 1);
$right++;
} else {
$right = $left + 1;
}
$item->setRight($right);
$left = $right + 1;
}
return $right;
}
/**
* Mark leaf branches
* @param array $items
*/
private function markLeafs($items)
{
foreach ($items as $item) {
if ($item->hasChildren()) {
$this->markLeafs($item->getChildren());
} else {
$item->setIsLeaf(true);
}
}
}
/**
* Add to debug data
* @param string $string
*/
private function logDebug($string)
{
$this->debugData .= $string . '<br/>';
}
}