Skip to content

Commit 692f70c

Browse files
author
Christian Lück
committed
Support extending Edge by registering to its vertices within ctor
1 parent 2f54bd0 commit 692f70c

3 files changed

Lines changed: 32 additions & 21 deletions

File tree

lib/Fhaculty/Graph/Edge/Directed.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Directed extends Base
2525
private $to;
2626

2727
/**
28-
* creats a new Edge (MUST NOT BE CALLED MANUALLY!)
28+
* create a new directed Edge from Vertex $from to Vertex $to
2929
*
3030
* @param Vertex $from start/source Vertex
3131
* @param Vertex $to end/target Vertex
@@ -34,8 +34,16 @@ class Directed extends Base
3434
*/
3535
public function __construct(Vertex $from, Vertex $to)
3636
{
37+
if ($from->getGraph() !== $to->getGraph()) {
38+
throw new InvalidArgumentException('Vertices have to be within the same graph');
39+
}
40+
3741
$this->from = $from;
3842
$this->to = $to;
43+
44+
$from->getGraph()->addEdge($this);
45+
$from->addEdge($this);
46+
$to->addEdge($this);
3947
}
4048

4149
public function getVerticesTarget()

lib/Fhaculty/Graph/Edge/Undirected.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,24 @@ class Undirected extends Base
2323
private $b;
2424

2525
/**
26-
* create new undirected edge between given vertices (MUST NOT BE CALLED MANUALLY!)
26+
* create a new undirected edge between given vertices
2727
*
2828
* @param Vertex $a
2929
* @param Vertex $b
3030
* @see Vertex::createEdge() instead
3131
*/
3232
public function __construct(Vertex $a, Vertex $b)
3333
{
34+
if ($a->getGraph() !== $b->getGraph()) {
35+
throw new InvalidArgumentException('Vertices have to be within the same graph');
36+
}
37+
3438
$this->a = $a;
3539
$this->b = $b;
40+
41+
$a->getGraph()->addEdge($this);
42+
$a->addEdge($this);
43+
$b->addEdge($this);
3644
}
3745

3846
public function getVerticesTarget()

lib/Fhaculty/Graph/Vertex.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,7 @@ public function getId()
133133
*/
134134
public function createEdgeTo(Vertex $vertex)
135135
{
136-
if ($vertex->getGraph() !== $this->graph) {
137-
throw new InvalidArgumentException('Target vertex has to be within the same graph');
138-
}
139-
140-
$edge = new EdgeDirected($this, $vertex);
141-
$this->edges []= $edge;
142-
$vertex->edges []= $edge;
143-
$this->graph->addEdge($edge);
144-
145-
return $edge;
136+
return new EdgeDirected($this, $vertex);
146137
}
147138

148139
/**
@@ -155,16 +146,20 @@ public function createEdgeTo(Vertex $vertex)
155146
*/
156147
public function createEdge(Vertex $vertex)
157148
{
158-
if ($vertex->getGraph() !== $this->graph) {
159-
throw new InvalidArgumentException('Target vertex has to be within the same graph');
160-
}
161-
162-
$edge = new EdgeUndirected($this, $vertex);
163-
$this->edges []= $edge;
164-
$vertex->edges []= $edge;
165-
$this->graph->addEdge($edge);
149+
return new EdgeUndirected($this, $vertex);
150+
}
166151

167-
return $edge;
152+
/**
153+
* add the given edge to list of connected edges (MUST NOT be called manually)
154+
*
155+
* @param Edge $edge
156+
* @return void
157+
* @private
158+
* @see self::createEdge() instead!
159+
*/
160+
public function addEdge(Edge $edge)
161+
{
162+
$this->edges[] = $edge;
168163
}
169164

170165
/**

0 commit comments

Comments
 (0)