Skip to content

Commit

Permalink
Update the Graph exporter base class.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 9, 2019
1 parent b7d6b90 commit 1334ba8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 89 deletions.
47 changes: 5 additions & 42 deletions spec/drupol/phptree/Exporter/GraphSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use drupol\phptree\Exporter\Graph;
use drupol\phptree\Node\ValueNode;
use drupol\phptree\Traverser\BreadthFirst;
use PhpSpec\ObjectBehavior;

class GraphSpec extends ObjectBehavior
Expand All @@ -17,6 +16,9 @@ public function it_can_generate_a_graph()
$child1 = new ValueNode('child1');
$child2 = new ValueNode('child2');
$child3 = new ValueNode('child3');
$child4 = new ValueNode('child3');
$child1->add($child4);

$tree
->add($child1, $child2, $child3);

Expand All @@ -25,47 +27,8 @@ public function it_can_generate_a_graph()
->shouldReturnAnInstanceOf(\Fhaculty\Graph\Graph::class);

$this
->getGraph()
->getVertices()
->shouldHaveCount(4);

$this
->getGraph()
->getEdges()
->shouldHaveCount(3);

$traverser = new BreadthFirst();

$nodes = \iterator_to_array($traverser->traverse($tree));

for ($i = 0; \count($nodes) - 1 > $i; ++$i) {
$node0 = $nodes[0];
$node1 = $nodes[$i + 1];

$this
->getGraph()
->getVertices()
->getVertexId(\spl_object_hash($node0))
->hasEdgeTo($this->getGraph()->getVertices()->getVertexId(\spl_object_hash($node1)))
->shouldReturn(true);
}
}

public function it_can_use_constructor_parameters()
{
$graph = new \Fhaculty\Graph\Graph();
$traverser = new BreadthFirst();

$this
->beConstructedWith($graph, $traverser);

$this
->getGraph()
->shouldReturn($graph);

$this
->getTraverser()
->shouldReturn($traverser);
->export($child1)
->shouldReturnAnInstanceOf(\Fhaculty\Graph\Graph::class);
}

public function it_is_initializable()
Expand Down
63 changes: 20 additions & 43 deletions src/Exporter/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace drupol\phptree\Exporter;

use drupol\phptree\Node\NodeInterface;
use drupol\phptree\Traverser\PreOrder;
use drupol\phptree\Traverser\TraverserInterface;
use drupol\phptree\Node\ValueNodeInterface;
use Fhaculty\Graph\Graph as OriginalGraph;
use Fhaculty\Graph\Vertex;

Expand All @@ -22,31 +21,16 @@ class Graph implements ExporterInterface
*/
private $graph;

/**
* The traverser.
*
* @var \drupol\phptree\Traverser\TraverserInterface
*/
private $traverser;

/**
* Graph constructor.
*
* @param \Fhaculty\Graph\Graph $graph
* @param null|\drupol\phptree\Traverser\TraverserInterface $traverser
*/
public function __construct(OriginalGraph $graph = null, TraverserInterface $traverser = null)
{
$this->graph = $graph ?? new OriginalGraph();
$this->traverser = $traverser ?? new PreOrder();
}

/**
* {@inheritdoc}
*/
public function export(NodeInterface $node): OriginalGraph
{
foreach ($this->getTraverser()->traverse($node) as $node_visited) {
$this->graph = new OriginalGraph();

$root = $node;

foreach ($node->all() as $node_visited) {
/** @var int $vertexId */
$vertexId = $this->createVertexId($node_visited);
$this->createVertex($node_visited);
Expand All @@ -55,6 +39,10 @@ public function export(NodeInterface $node): OriginalGraph
continue;
}

if ($root === $node_visited) {
continue;
}

/** @var int $hash_parent */
$hash_parent = $this->createVertexId($parent);
$this->createVertex($parent);
Expand All @@ -65,22 +53,6 @@ public function export(NodeInterface $node): OriginalGraph
return $this->getGraph();
}

/**
* @return \Fhaculty\Graph\Graph
*/
public function getGraph(): OriginalGraph
{
return $this->graph;
}

/**
* @return \drupol\phptree\Traverser\TraverserInterface
*/
public function getTraverser(): TraverserInterface
{
return $this->traverser;
}

/**
* Create a vertex.
*
Expand All @@ -98,12 +70,9 @@ protected function createVertex(NodeInterface $node): Vertex
if (false === $this->getGraph()->hasVertex($vertexId)) {
$vertex = $this->getGraph()->createVertex($vertexId);

$label = null;
if (\method_exists($node, 'getValue')) {
$label = $node->getValue();
if ($node instanceof ValueNodeInterface) {
$vertex->setAttribute('graphviz.label', $node->getValue());
}

$vertex->setAttribute('graphviz.label', $label);
}

return $this->getGraph()->getVertex($vertexId);
Expand All @@ -122,4 +91,12 @@ protected function createVertexId(NodeInterface $node)
{
return \spl_object_hash($node);
}

/**
* @return \Fhaculty\Graph\Graph
*/
protected function getGraph(): OriginalGraph
{
return $this->graph;
}
}
5 changes: 3 additions & 2 deletions tests/src/Exporter/KeyValueGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace drupol\phptree\tests\Exporter;

use drupol\phptree\Node\KeyValueNodeInterface;
use drupol\phptree\Node\NodeInterface;
use Fhaculty\Graph\Vertex;

Expand All @@ -19,7 +20,7 @@ protected function createVertex(NodeInterface $node): Vertex
{
$vertex = parent::createVertex($node);

if (\method_exists($node, 'getValue') && \method_exists($node, 'getKey')) {
if ($node instanceof KeyValueNodeInterface) {
$vertex->setAttribute('graphviz.label', $node->getValue());
}

Expand All @@ -31,7 +32,7 @@ protected function createVertex(NodeInterface $node): Vertex
*/
protected function createVertexId(NodeInterface $node)
{
if (\method_exists($node, 'getValue') && \method_exists($node, 'getKey')) {
if ($node instanceof KeyValueNodeInterface) {
return $node->getKey() . $node->getValue();
}

Expand Down
5 changes: 3 additions & 2 deletions tests/src/Exporter/ValueGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use drupol\phptree\Exporter\Graph;
use drupol\phptree\Node\NodeInterface;
use drupol\phptree\Node\ValueNodeInterface;
use Fhaculty\Graph\Vertex;

/**
Expand All @@ -20,7 +21,7 @@ protected function createVertex(NodeInterface $node): Vertex
{
$vertex = parent::createVertex($node);

if (\method_exists($node, 'getValue')) {
if ($node instanceof ValueNodeInterface) {
$vertex->setAttribute('value', $node->getValue());
}

Expand All @@ -32,7 +33,7 @@ protected function createVertex(NodeInterface $node): Vertex
*/
protected function createVertexId(NodeInterface $node)
{
if (\method_exists($node, 'getValue')) {
if ($node instanceof ValueNodeInterface) {
return $node->getValue();
}

Expand Down

0 comments on commit 1334ba8

Please sign in to comment.