|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Jean-François Lépine <https://twitter.com/Halleck45> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Hal\Component\Tree; |
|
11
|
|
|
|
|
12
|
|
|
class Graph implements \Countable |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $datas = array(); |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Edge[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private $edges = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param Node $node |
|
27
|
|
|
* @return $this |
|
28
|
|
|
*/ |
|
29
|
|
|
public function insert(Node $node) |
|
30
|
|
|
{ |
|
31
|
|
|
if ($this->has($node->getKey())) { |
|
32
|
|
|
throw new GraphException(sprintf('node %s is already present', $node->getKey())); |
|
33
|
|
|
} |
|
34
|
|
|
$this->datas[$node->getKey()] = $node; |
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Node $from |
|
40
|
|
|
* @param Node $to |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
public function addEdge(Node $from, Node $to) |
|
44
|
|
|
{ |
|
45
|
|
|
if (!$this->has($from->getKey())) { |
|
46
|
|
|
throw new GraphException('from is not is in the graph'); |
|
47
|
|
|
} |
|
48
|
|
|
if (!$this->has($to->getKey())) { |
|
49
|
|
|
throw new GraphException('to is not is in the graph'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$edge = new Edge($from, $to); |
|
53
|
|
|
$from->addEdge($edge); |
|
54
|
|
|
$to->addEdge($edge); |
|
55
|
|
|
array_push($this->edges, $edge); |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function asString() |
|
64
|
|
|
{ |
|
65
|
|
|
$string = ''; |
|
66
|
|
|
foreach ($this->all() as $node) { |
|
67
|
|
|
$string .= sprintf("%s;\n", $node->getKey()); |
|
68
|
|
|
} |
|
69
|
|
|
foreach ($this->getEdges() as $edge) { |
|
70
|
|
|
$string .= sprintf("%s;\n", $edge->asString()); |
|
71
|
|
|
} |
|
72
|
|
|
return $string; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return Edge[] |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getEdges() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->edges; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param $key |
|
85
|
|
|
* @return null |
|
86
|
|
|
*/ |
|
87
|
|
|
public function get($key) |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->has($key) ? $this->datas[$key] : null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param $key |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function has($key) |
|
97
|
|
|
{ |
|
98
|
|
|
return isset($this->datas[$key]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return int |
|
103
|
|
|
*/ |
|
104
|
|
|
public function count() |
|
105
|
|
|
{ |
|
106
|
|
|
return sizeof($this->datas); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return Node[] |
|
111
|
|
|
*/ |
|
112
|
|
|
public function all() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->datas; |
|
115
|
|
|
} |
|
116
|
|
|
} |