Skip to content

Commit 4295b3d

Browse files
committed
Update GraphViz renderer.
1 parent 0727216 commit 4295b3d

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

spec/drupol/phptree/tests/TestGraphVizSpec.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,30 @@
77
use drupol\phptree\Node\ValueNode;
88
use drupol\phptree\tests\TestGraphViz;
99
use drupol\phptree\Visitor\BreadthFirstVisitor;
10+
use Fhaculty\Graph\Graph;
11+
use Graphp\GraphViz\GraphViz;
1012
use PhpSpec\ObjectBehavior;
1113

1214
class TestGraphVizSpec extends ObjectBehavior
1315
{
1416
public function it_is_initializable()
1517
{
1618
$visitor = new BreadthFirstVisitor();
19+
$graph = new Graph();
20+
$graphviz = new GraphViz();
1721

18-
$this->beConstructedWith($visitor);
22+
$this->beConstructedWith($visitor, $graph, $graphviz);
1923

2024
$this->shouldHaveType(TestGraphViz::class);
2125
}
2226

2327
public function it_can_create_a_graph()
2428
{
25-
$this->beConstructedWith(new BreadthFirstVisitor());
29+
$visitor = new BreadthFirstVisitor();
30+
$graph = new Graph();
31+
$graphviz = new GraphViz();
32+
33+
$this->beConstructedWith($visitor, $graph, $graphviz);
2634

2735
$tree = new ValueNode('root', 2);
2836

src/Render/GraphViz.php

+40-21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use drupol\phptree\Node\NodeInterface;
88
use drupol\phptree\Visitor\VisitorInterface;
99
use Fhaculty\Graph\Graph;
10+
use Graphp\GraphViz\GraphViz as OriginalGraphViz;
1011

1112
/**
1213
* Class GraphViz
@@ -23,11 +24,6 @@ class GraphViz implements RendererInterface
2324
*/
2425
private $graph;
2526

26-
/**
27-
* @var array
28-
*/
29-
private $nodes;
30-
3127
/**
3228
* @var \Graphp\GraphViz\GraphViz
3329
*/
@@ -38,12 +34,11 @@ class GraphViz implements RendererInterface
3834
*
3935
* @param \drupol\phptree\Visitor\VisitorInterface $visitor
4036
*/
41-
public function __construct(VisitorInterface $visitor)
37+
public function __construct(VisitorInterface $visitor, Graph $graph, OriginalGraphViz $graphViz)
4238
{
4339
$this->visitor = $visitor;
44-
$this->graph = new Graph();
45-
$this->graphviz = new \Graphp\GraphViz\GraphViz();
46-
$this->nodes = [];
40+
$this->graph = $graph;
41+
$this->graphviz = $graphViz;
4742
}
4843

4944
/**
@@ -53,36 +48,60 @@ public function __construct(VisitorInterface $visitor)
5348
*/
5449
public function render(NodeInterface $node): string
5550
{
56-
foreach ($this->visitor->traverse($node) as $child) {
57-
$hash_parent = $this->hash($child);
51+
return $this->graphviz->createScript($this->getGraph($node));
52+
}
53+
54+
/**
55+
* @param \drupol\phptree\Node\NodeInterface $node
56+
*
57+
* @return \Fhaculty\Graph\Graph
58+
*/
59+
public function getGraph(NodeInterface $node): Graph
60+
{
61+
foreach ($this->visitor->traverse($node) as $node_visited) {
62+
/** @var int $hash */
63+
$hash = $this->hash($node_visited);
5864

59-
if (!isset($this->nodes[$hash_parent])) {
60-
$this->nodes[$hash_parent] = $this->graph->createVertex($hash_parent);
65+
if (false === $this->graph->hasVertex($hash)) {
66+
$this->graph->createVertex($hash);
6167
}
6268

63-
if (null === $parent = $child->getParent()) {
69+
if (null === $parent = $node_visited->getParent()) {
6470
continue;
6571
}
6672

67-
$hash = $this->hash($parent);
73+
/** @var int $hash_parent */
74+
$hash_parent = $this->hash($parent);
6875

69-
if (!isset($this->nodes[$hash])) {
70-
$this->nodes[$hash] = $this->graph->createVertex($hash);
76+
if (false === $this->graph->hasVertex($hash_parent)) {
77+
$this->graph->createVertex($hash_parent);
7178
}
7279

73-
$this->nodes[$hash]->createEdgeTo($this->nodes[$hash_parent]);
80+
$this->graph->getVertex($hash_parent)->createEdgeTo($this->graph->getVertex($hash));
7481
}
7582

76-
return $this->graphviz->createScript($this->graph);
83+
return $this->graph;
84+
}
85+
86+
/**
87+
* @param \drupol\phptree\Node\NodeInterface $node
88+
*
89+
* @return $this
90+
*/
91+
public function display(NodeInterface $node): RendererInterface
92+
{
93+
$this->graphviz->display($this->getGraph($node));
94+
95+
return $this;
7796
}
7897

7998
/**
8099
* @param \drupol\phptree\Node\NodeInterface $node
81100
*
82-
* @return int
101+
* @return int|null|string
83102
*/
84103
protected function hash(NodeInterface $node)
85104
{
86-
return (int) \spl_object_hash($node);
105+
return \spl_object_hash($node);
87106
}
88107
}

0 commit comments

Comments
 (0)