Skip to content

Commit 46b9b6a

Browse files
author
Christian Lück
committed
Support extending Vertex by registering to Graph within its ctor
1 parent 2769dc6 commit 46b9b6a

2 files changed

Lines changed: 28 additions & 14 deletions

File tree

lib/Fhaculty/Graph/Graph.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,12 @@ public function createVertex($id = NULL, $returnDuplicate = false)
7777
// no ID given
7878
if ($id === NULL) {
7979
$id = $this->getNextId();
80-
} elseif (!is_int($id) && !is_string($id)) {
81-
throw new InvalidArgumentException('Vertex ID has to be of type integer or string');
8280
}
83-
if ($this->vertices->hasVertexId($id)) {
84-
if ($returnDuplicate) {
85-
return $this->vertices->getVertexId($id);
86-
}
87-
throw new OverflowException('ID must be unique');
81+
if ($returnDuplicate && $this->vertices->hasVertexId($id)) {
82+
return $this->vertices->getVertexId($id);
8883
}
89-
$vertex = new Vertex($id, $this);
90-
$this->verticesStorage[$id] = $vertex;
9184

92-
return $vertex;
85+
return new Vertex($id, $this);
9386
}
9487

9588
/**
@@ -110,7 +103,6 @@ public function createVertexClone(Vertex $originalVertex)
110103
$newVertex->setLayout($originalVertex->getLayout());
111104
$newVertex->setBalance($originalVertex->getBalance());
112105
$newVertex->setGroup($originalVertex->getGroup());
113-
$this->verticesStorage[$id] = $newVertex;
114106

115107
return $newVertex;
116108
}
@@ -264,7 +256,7 @@ public function createVertices($n)
264256
$vertices = array();
265257
if (is_int($n) && $n >= 0) {
266258
for ($id = $this->getNextId(), $n += $id; $id < $n; ++$id) {
267-
$vertices[$id] = $this->verticesStorage[$id] = new Vertex($id, $this);
259+
$vertices[$id] = new Vertex($id, $this);
268260
}
269261
} elseif (is_array($n)) {
270262
// array given => check to make sure all given IDs are available (atomic operation)
@@ -283,7 +275,7 @@ public function createVertices($n)
283275

284276
// actually create all requested vertices
285277
foreach ($n as $id) {
286-
$vertices[$id] = $this->verticesStorage[$id] = new Vertex($id, $this);
278+
$vertices[$id] = new Vertex($id, $this);
287279
}
288280
} else {
289281
throw new InvalidArgumentException('Invalid number of vertices given. Must be non-negative integer or an array of Vertex IDs');
@@ -349,6 +341,22 @@ public function getVertexFirst()
349341
return $this->vertices->getVertexFirst();
350342
}
351343

344+
/**
345+
* adds a new Vertex to the Graph (MUST NOT be called manually!)
346+
*
347+
* @param Vertex $vertex instance of the new Vertex
348+
* @return void
349+
* @private
350+
* @see self::createVertex() instead!
351+
*/
352+
public function addVertex(Vertex $vertex)
353+
{
354+
if (isset($this->verticesStorage[$vertex->getId()])) {
355+
throw new OverflowException('ID must be unique');
356+
}
357+
$this->verticesStorage[$vertex->getId()] = $vertex;
358+
}
359+
352360
/**
353361
* adds a new Edge to the Graph (MUST NOT be called manually!)
354362
*

lib/Fhaculty/Graph/Vertex.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,22 @@ class Vertex extends Layoutable implements EdgesAggregate
4343
private $group = 0;
4444

4545
/**
46-
* Creates a Vertex (MUST NOT BE CALLED MANUALLY!)
46+
* Create a new Vertex
4747
*
4848
* @param string|int $id identifier used to uniquely identify this vertex in the graph
4949
* @param Graph $graph graph to be added to
5050
* @see Graph::createVertex() to create new vertices
5151
*/
5252
public function __construct($id, Graph $graph)
5353
{
54+
if (!is_int($id) && !is_string($id)) {
55+
throw new InvalidArgumentException('Vertex ID has to be of type integer or string');
56+
}
57+
5458
$this->id = $id;
5559
$this->graph = $graph;
60+
61+
$graph->addVertex($this);
5662
}
5763

5864
/**

0 commit comments

Comments
 (0)