Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ require_once 'vendor/autoload.php';
$graph = new Graphp\Graph\Graph();

// create some cities
$rome = $graph->createVertex()->setAttribute('name', 'Rome');
$madrid = $graph->createVertex()->setAttribute('name', 'Madrid');
$cologne = $graph->createVertex()->setAttribute('name', 'Cologne');
$rome = $graph->createVertex(array('name' => 'Rome'));
$madrid = $graph->createVertex(array('name' => 'Madrid'));
$cologne = $graph->createVertex(array('name' => 'Cologne'));

// build some roads
$graph->createEdgeDirected($cologne, $madrid);
Expand Down
8 changes: 5 additions & 3 deletions src/EdgeDirected.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ class EdgeDirected extends Edge
/**
* [Internal] Create a new directed Edge from Vertex $from to Vertex $to
*
* @param Vertex $from start/source Vertex
* @param Vertex $to end/target Vertex
* @param Vertex $from start/source Vertex
* @param Vertex $to end/target Vertex
* @param array $attributes
* @see Graph::createEdgeDirected() to create directed edges
* @see Graph::createEdgeUndirected() to create undirected edges
* @internal
*/
public function __construct(Vertex $from, Vertex $to)
public function __construct(Vertex $from, Vertex $to, array $attributes = array())
{
if ($from->getGraph() !== $to->getGraph()) {
throw new InvalidArgumentException('Vertices have to be within the same graph');
}

$this->from = $from;
$this->to = $to;
$this->attributes = $attributes;

$from->getGraph()->addEdge($this);
$from->addEdge($this);
Expand Down
4 changes: 3 additions & 1 deletion src/EdgeUndirected.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ class EdgeUndirected extends Edge
*
* @param Vertex $a
* @param Vertex $b
* @param array $attributes
* @see Graph::createEdgeUndirected() to create undirected edges
* @see Graph::createEdgeDirected() to create directed edges
* @internal
*/
public function __construct(Vertex $a, Vertex $b)
public function __construct(Vertex $a, Vertex $b, array $attributes = array())
{
if ($a->getGraph() !== $b->getGraph()) {
throw new InvalidArgumentException('Vertices have to be within the same graph');
}

$this->a = $a;
$this->b = $b;
$this->attributes = $attributes;

$a->getGraph()->addEdge($this);
$a->addEdge($this);
Expand Down
35 changes: 20 additions & 15 deletions src/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ class Graph implements DualAggregate, AttributeAware
protected $edgesStorage = array();
protected $edges;

protected $attributes = array();
protected $attributes;

public function __construct()
/**
* @param array $attributes
*/
public function __construct(array $attributes = array())
{
$this->vertices = Vertices::factoryArrayReference($this->verticesStorage);
$this->edges = Edges::factoryArrayReference($this->edgesStorage);
$this->attributes = $attributes;
}

/**
Expand All @@ -49,45 +53,48 @@ public function getEdges()
/**
* create a new Vertex in the Graph
*
* @param array $attributes
* @return Vertex
*/
public function createVertex()
public function createVertex(array $attributes = array())
{
return new Vertex($this);
return new Vertex($this, $attributes);
}

/**
* Creates a new undirected (bidirectional) edge between the given two vertices.
*
* @param Vertex $a
* @param Vertex $b
* @param array $attributes
* @return EdgeUndirected
* @throws InvalidArgumentException
*/
public function createEdgeUndirected(Vertex $a, Vertex $b)
public function createEdgeUndirected(Vertex $a, Vertex $b, array $attributes = array())
{
if ($a->getGraph() !== $this) {
throw new InvalidArgumentException('Vertices have to be within this graph');
}

return new EdgeUndirected($a, $b);
return new EdgeUndirected($a, $b, $attributes);
}

/**
* Creates a new directed edge from the given start vertex to given target vertex
*
* @param Vertex $source source vertex
* @param Vertex $target target vertex
* @param Vertex $source source vertex
* @param Vertex $target target vertex
* @param array $attributes
* @return EdgeDirected
* @throws InvalidArgumentException
*/
public function createEdgeDirected(Vertex $source, Vertex $target)
public function createEdgeDirected(Vertex $source, Vertex $target, array $attributes = array())
{
if ($source->getGraph() !== $this) {
throw new InvalidArgumentException('Vertices have to be within this graph');
}

return new EdgeDirected($source, $target);
return new EdgeDirected($source, $target, $attributes);
}

/**
Expand Down Expand Up @@ -171,8 +178,7 @@ public function __clone()
foreach ($vertices as $originalVertex) {
\assert($originalVertex instanceof Vertex);

$vertex = new Vertex($this);
$vertex->getAttributeBag()->setAttributes($originalVertex->getAttributeBag()->getAttributes());
$vertex = new Vertex($this, $originalVertex->getAttributeBag()->getAttributes());

// create map with old vertex hash to new vertex object
$map[\spl_object_hash($originalVertex)] = $vertex;
Expand All @@ -188,11 +194,10 @@ public function __clone()

// recreate edge and assign attributes
if ($originalEdge instanceof EdgeUndirected) {
$edge = $this->createEdgeUndirected($v1, $v2);
$this->createEdgeUndirected($v1, $v2, $originalEdge->getAttributeBag()->getAttributes());
} else {
$edge = $this->createEdgeDirected($v1, $v2);
$this->createEdgeDirected($v1, $v2, $originalEdge->getAttributeBag()->getAttributes());
}
$edge->getAttributeBag()->setAttributes($originalEdge->getAttributeBag()->getAttributes());
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/Vertex.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ class Vertex implements EdgesAggregate, AttributeAware
*/
private $graph;

private $attributes = array();
private $attributes;

/**
* Create a new Vertex
*
* @param Graph $graph graph to be added to
* @param Graph $graph graph to be added to
* @param array $attributes
* @see Graph::createVertex() to create new vertices
*/
public function __construct(Graph $graph)
public function __construct(Graph $graph, array $attributes = array())
{
$this->graph = $graph;
$this->attributes = $attributes;

$graph->addVertex($this);
}
Expand Down
20 changes: 17 additions & 3 deletions tests/EdgeBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class EdgeBaseTest extends AbstractAttributeAwareTest
*/
protected $edge;

abstract protected function createEdgeUndirected();
abstract protected function createEdge(array $attributes = array());

/**
* @return Edge
Expand All @@ -42,7 +42,21 @@ public function setUp()
$this->v1 = $this->graph->createVertex();
$this->v2 = $this->graph->createVertex();

$this->edge = $this->createEdgeUndirected();
$this->edge = $this->createEdge();
}

public function testEdgeConstructorDefaultHasNoAttributes()
{
$this->assertNull($this->edge->getAttribute('hello'));
$this->assertEquals('default', $this->edge->getAttribute('hello', 'default'));
$this->assertEquals(array(), $this->edge->getAttributeBag()->getAttributes());
}

public function testEdgeConstructorWithAttributeReturnsAttributes()
{
$edge = $this->createEdge(array('hello' => 'wörld'));
$this->assertEquals('wörld', $edge->getAttribute('hello'));
$this->assertEquals(array('hello' => 'wörld'), $edge->getAttributeBag()->getAttributes());
}

public function testEdgeVertices()
Expand Down Expand Up @@ -105,6 +119,6 @@ public function testRemoveWithLoop()

protected function createAttributeAware()
{
return $this->createEdgeUndirected();
return $this->createEdge();
}
}
4 changes: 2 additions & 2 deletions tests/EdgeDirectedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

class EdgeDirectedTest extends EdgeBaseTest
{
protected function createEdgeUndirected()
protected function createEdge(array $attributes = array())
{
// 1 -> 2
return $this->graph->createEdgeDirected($this->v1, $this->v2);
return $this->graph->createEdgeDirected($this->v1, $this->v2, $attributes);
}

protected function createEdgeLoop()
Expand Down
4 changes: 2 additions & 2 deletions tests/EdgeUndirectedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

class EdgeUndirectedTest extends EdgeBaseTest
{
protected function createEdgeUndirected()
protected function createEdge(array $attributes = array())
{
// 1 -- 2
return $this->graph->createEdgeUndirected($this->v1, $this->v2);
return $this->graph->createEdgeUndirected($this->v1, $this->v2, $attributes);
}

protected function createEdgeLoop()
Expand Down
22 changes: 18 additions & 4 deletions tests/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@

class GraphTest extends AbstractAttributeAwareTest
{
public function testEmptyGraphHasNoAttributes()
{
$graph = new Graph();
$this->assertNull($graph->getAttribute('hello'));
$this->assertEquals('default', $graph->getAttribute('hello', 'default'));
$this->assertEquals(array(), $graph->getAttributeBag()->getAttributes());
}

public function testEmptyGraphWithAttributeReturnsAttributes()
{
$graph = new Graph(array('hello' => 'wörld'));
$this->assertEquals('wörld', $graph->getAttribute('hello'));
$this->assertEquals(array('hello' => 'wörld'), $graph->getAttributeBag()->getAttributes());
}

public function testCanCreateVertex()
{
$graph = new Graph();
Expand Down Expand Up @@ -186,10 +201,9 @@ public function testGraphCloneLoopGraphWithAttributes()
// 1 -\
// ^ |
// \--/
$graph = new Graph();
$graph->setAttribute('color', 'grey');
$v = $graph->createVertex()->setAttribute('color', 'blue');
$graph->createEdgeDirected($v, $v)->setAttribute('color', 'red');
$graph = new Graph(array('color' => 'grey'));
$v = $graph->createVertex(array('color' => 'blue'));
$graph->createEdgeDirected($v, $v, array('color' => 'red'));

$newgraph = clone $graph;

Expand Down
14 changes: 13 additions & 1 deletion tests/VertexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ public function testPrecondition()
$this->assertEquals(array($this->vertex), $this->graph->getVertices()->getVector());
}

public function testConstructor()
public function testConstructorWithoutAttributesHasNoAttributes()
{
$v2 = new Vertex($this->graph);

$this->assertCount(2, $this->graph->getVertices());
$this->assertEquals(array($this->vertex, $v2), $this->graph->getVertices()->getVector());

$this->assertNull($v2->getAttribute('hello'));
$this->assertEquals('default', $v2->getAttribute('hello', 'default'));
$this->assertEquals(array(), $v2->getAttributeBag()->getAttributes());
}

public function testConstructorWithAttributesReturnsAttributes()
{
$v2 = new Vertex($this->graph, array('hello' => 'wörld'));

$this->assertEquals('wörld', $v2->getAttribute('hello'));
$this->assertEquals(array('hello' => 'wörld'), $v2->getAttributeBag()->getAttributes());
}

public function testEdges()
Expand Down