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
32 changes: 18 additions & 14 deletions src/Edge.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,36 @@

namespace Graphp\Graph;

use Graphp\Graph\Set\Vertices;
use Graphp\Graph\Set\VerticesAggregate;

/**
* Abstract base for `EdgeUndirected` and `EdgeDirected` containing common interfaces and behavior for all edges.
*
* @see EdgeUndirected
* @see EdgeDirected
*/
abstract class Edge extends Entity implements VerticesAggregate
abstract class Edge extends Entity
{
protected $attributes = array();

/**
* get Vertices that are a target of this edge
* get vertices that are a target of this edge
*
* @return Vertices
* @psalm-return list<Vertex>
* @return Vertex[]
*/
abstract public function getVerticesTarget();

/**
* get Vertices that are the start of this edge
* get vertices that are the start of this edge
*
* @return Vertices
* @psalm-return list<Vertex>
* @return Vertex[]
*/
abstract public function getVerticesStart();

/**
* return true if this edge is an outgoing edge of the given vertex (i.e. the given vertex is a valid start vertex of this edge)
*
* @param Vertex $startVertex
* @param Vertex $startVertex
* @return bool
* @uses Vertex::getVertexToFrom()
*/
Expand All @@ -41,7 +40,7 @@ abstract public function hasVertexStart(Vertex $startVertex);
/**
* return true if this edge is an ingoing edge of the given vertex (i . e. the given vertex is a valid end vertex of this edge)
*
* @param Vertex $targetVertex
* @param Vertex $targetVertex
* @return bool
* @uses Vertex::getVertexFromTo()
*/
Expand Down Expand Up @@ -77,11 +76,12 @@ abstract public function getVertexToFrom(Vertex $startVertex);
abstract public function getVertexFromTo(Vertex $endVertex);

/**
* get set of all Vertices this edge connects
* get list of all vertices this edge connects
*
* @return Vertices
* @psalm-return list<Vertex>
* @return Vertex[]
*/
//abstract public function getVertices();
abstract public function getVertices();

/**
* get graph instance this edge is attached to
Expand All @@ -90,7 +90,11 @@ abstract public function getVertexFromTo(Vertex $endVertex);
*/
public function getGraph()
{
return $this->getVertices()->getVertexFirst()->getGraph();
$vertices = $this->getVertices();
$vertex = \reset($vertices);
\assert($vertex instanceof Vertex);

return $vertex->getGraph();
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/EdgeDirected.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Graphp\Graph;

use Graphp\Graph\Set\Vertices;

class EdgeDirected extends Edge
{
/**
Expand Down Expand Up @@ -47,17 +45,17 @@ public function __construct(Vertex $from, Vertex $to, array $attributes = array(

public function getVerticesTarget()
{
return new Vertices(array($this->to));
return array($this->to);
}

public function getVerticesStart()
{
return new Vertices(array($this->from));
return array($this->from);
}

public function getVertices()
{
return new Vertices(array($this->from, $this->to));
return array($this->from, $this->to);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/EdgeUndirected.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Graphp\Graph;

use Graphp\Graph\Set\Vertices;

class EdgeUndirected extends Edge
{
/**
Expand Down Expand Up @@ -47,17 +45,17 @@ public function __construct(Vertex $a, Vertex $b, array $attributes = array())

public function getVerticesTarget()
{
return new Vertices(array($this->b, $this->a));
return array($this->b, $this->a);
}

public function getVerticesStart()
{
return new Vertices(array($this->a, $this->b));
return array($this->a, $this->b);
}

public function getVertices()
{
return new Vertices(array($this->a, $this->b));
return array($this->a, $this->b);
}

public function isConnection(Vertex $from, Vertex $to)
Expand Down
36 changes: 17 additions & 19 deletions src/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

namespace Graphp\Graph;

use Graphp\Graph\Set\DualAggregate;
use Graphp\Graph\Set\Edges;
use Graphp\Graph\Set\Vertices;

class Graph extends Entity implements DualAggregate
class Graph extends Entity
{
protected $vertices = array();
protected $edges = array();
Expand All @@ -20,23 +16,25 @@ public function __construct(array $attributes = array())
}

/**
* return set of Vertices added to this graph
* return list of all vertices added to this graph
*
* @return Vertices
* @psalm-return list<Vertex>
* @return Vertex[]
*/
public function getVertices()
{
return new Vertices($this->vertices);
return $this->vertices;
}

/**
* return set of ALL Edges added to this graph
* return list of all edges added to this graph
*
* @return Edges
* @psalm-return list<Edge>
* @return Edge[]
*/
public function getEdges()
{
return new Edges($this->edges);
return $this->edges;
}

/**
Expand Down Expand Up @@ -97,7 +95,7 @@ public function createEdgeDirected(Vertex $source, Vertex $target, array $attrib
*/
public function withoutVertex(Vertex $vertex)
{
return $this->withoutVertices(new Vertices(array($vertex)));
return $this->withoutVertices(array($vertex));
}

/**
Expand All @@ -107,10 +105,10 @@ public function withoutVertex(Vertex $vertex)
* silently be ignored. If neither of the vertices can be found in this graph,
* the returned graph will be identical.
*
* @param Vertices $vertices
* @param Vertex[] $vertices
* @return self
*/
public function withoutVertices(Vertices $vertices)
public function withoutVertices(array $vertices)
{
// keep copy of original vertices and edges and temporarily remove all $vertices and their adjacent edges
$originalEdges = $this->edges;
Expand All @@ -133,7 +131,7 @@ public function withoutVertices(Vertices $vertices)

// clone graph with vertices/edges temporarily removed, then restore
$clone = clone $this;
$this->edges= $originalEdges;
$this->edges = $originalEdges;
$this->vertices = $originalVertices;

return $clone;
Expand All @@ -150,7 +148,7 @@ public function withoutVertices(Vertices $vertices)
*/
public function withoutEdge(Edge $edge)
{
return $this->withoutEdges(new Edges(array($edge)));
return $this->withoutEdges(array($edge));
}

/**
Expand All @@ -160,10 +158,10 @@ public function withoutEdge(Edge $edge)
* silently be ignored. If neither of the edges can be found in this graph,
* the returned graph will be identical.
*
* @param Edges $edges
* @param Edge[] $edges
* @return self
*/
public function withoutEdges(Edges $edges)
public function withoutEdges(array $edges)
{
// keep copy of original edges and temporarily remove all $edges
$original = $this->edges;
Expand Down Expand Up @@ -236,7 +234,7 @@ public function __clone()
\assert($originalEdge instanceof Edge);

// use map to match old vertex hashes to new vertex objects
$vertices = $originalEdge->getVertices()->getVector();
$vertices = $originalEdge->getVertices();
$v1 = $map[\spl_object_hash($vertices[0])];
$v2 = $map[\spl_object_hash($vertices[1])];

Expand Down
27 changes: 0 additions & 27 deletions src/Set/DualAggregate.php

This file was deleted.

Loading