|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the kaloa/util package. |
|
5
|
|
|
* |
|
6
|
|
|
* For full copyright and license information, please view the LICENSE file |
|
7
|
|
|
* that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Kaloa\Util\Tree; |
|
11
|
|
|
|
|
12
|
|
|
use Closure; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A simple tree structure |
|
16
|
|
|
* |
|
17
|
|
|
* There will always be a single distinct root node |
|
18
|
|
|
*/ |
|
19
|
|
|
final class Node |
|
20
|
|
|
{ |
|
21
|
|
|
private $content = null; |
|
22
|
|
|
private $children = array(); |
|
23
|
|
|
private $parent = null; |
|
24
|
|
|
|
|
25
|
7 |
|
public function __construct($value) |
|
26
|
|
|
{ |
|
27
|
7 |
|
$this->content = $value; |
|
28
|
7 |
|
} |
|
29
|
|
|
|
|
30
|
5 |
|
public function addChild(Node $child) |
|
31
|
|
|
{ |
|
32
|
5 |
|
$this->children[] = $child; |
|
33
|
5 |
|
$child->setParent($this); |
|
34
|
5 |
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
public function hasChildren() |
|
37
|
|
|
{ |
|
38
|
3 |
|
return (count($this->children) > 0); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
5 |
|
public function setParent(Node $node) |
|
42
|
|
|
{ |
|
43
|
5 |
|
$this->parent = $node; |
|
44
|
5 |
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function getParent() |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->parent; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
public function getChildren() |
|
52
|
|
|
{ |
|
53
|
2 |
|
return $this->children; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
4 |
|
public function getContent() |
|
57
|
|
|
{ |
|
58
|
4 |
|
return $this->content; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Displays all elements from a tree in hierarchic order |
|
63
|
|
|
* |
|
64
|
|
|
* @param int $level Current level of indentation |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function display($level = 0) |
|
67
|
|
|
{ |
|
68
|
1 |
|
$value = $this->getContent(); |
|
69
|
|
|
|
|
70
|
1 |
|
if (null === $value) { |
|
71
|
1 |
|
$value = 'null'; |
|
72
|
1 |
|
} elseif (is_object($value)) { |
|
73
|
1 |
|
$value = get_class($value); |
|
74
|
1 |
|
} elseif (is_array($value)) { |
|
75
|
1 |
|
$value = 'Array'; |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
$ret = str_repeat(' ', $level * 4) . $value . "\n"; |
|
79
|
|
|
|
|
80
|
1 |
|
$children = $this->getChildren(); |
|
81
|
|
|
|
|
82
|
1 |
|
foreach ($children as $child) { |
|
83
|
1 |
|
$ret .= $child->display($level + 1); |
|
84
|
1 |
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
return $ret; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* |
|
91
|
|
|
* @param Closure $expr |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function getNodesByFilter(Closure $expr) |
|
95
|
|
|
{ |
|
96
|
1 |
|
$nodes = array(); |
|
97
|
|
|
|
|
98
|
1 |
|
foreach ($this->children as $child) { |
|
99
|
1 |
|
if ($expr($child->getContent())) { |
|
100
|
1 |
|
$nodes[] = $child; |
|
101
|
1 |
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
foreach ($child->getNodesByFilter($expr) as $node) { |
|
104
|
1 |
|
$nodes[] = $node; |
|
105
|
1 |
|
} |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
return $nodes; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|