Skip to content

Commit 3057801

Browse files
committed
Remove the Renderer and add the Converter.
1 parent 66e1108 commit 3057801

File tree

6 files changed

+65
-150
lines changed

6 files changed

+65
-150
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace spec\drupol\phptree\Converter;
6+
7+
use drupol\phptree\Converter\Graph;
8+
use drupol\phptree\Node\Node;
9+
use PhpSpec\ObjectBehavior;
10+
11+
class GraphSpec extends ObjectBehavior
12+
{
13+
public function it_is_initializable()
14+
{
15+
$this->shouldHaveType(Graph::class);
16+
}
17+
18+
public function it_can_generate_a_graph()
19+
{
20+
$root = new Node();
21+
$level1 = new Node();
22+
$level2 = new Node();
23+
24+
$root
25+
->add($level1, $level2);
26+
27+
$this
28+
->convert($root)
29+
->shouldReturnAnInstanceOf(\Fhaculty\Graph\Graph::class);
30+
}
31+
}

spec/drupol/phptree/tests/TestGraphVizSpec.php

-81
This file was deleted.

spec/src/TestGraphViz.php

-22
This file was deleted.

src/Converter/ConverterInterface.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace drupol\phptree\Converter;
6+
7+
use drupol\phptree\Node\NodeInterface;
8+
9+
/**
10+
* Interface ConverterInterface.
11+
*/
12+
interface ConverterInterface
13+
{
14+
/**
15+
* @param \drupol\phptree\Node\NodeInterface $node
16+
*
17+
* @return mixed
18+
*/
19+
public function convert(NodeInterface $node);
20+
}

src/Render/GraphViz.php renamed to src/Converter/Graph.php

+14-29
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,46 @@
22

33
declare(strict_types = 1);
44

5-
namespace drupol\phptree\Render;
5+
namespace drupol\phptree\Converter;
66

77
use drupol\phptree\Node\NodeInterface;
8+
use drupol\phptree\Visitor\BreadthFirstVisitor;
89
use drupol\phptree\Visitor\VisitorInterface;
9-
use Fhaculty\Graph\Graph;
10-
use Graphp\GraphViz\GraphViz as OriginalGraphViz;
10+
use Fhaculty\Graph\Graph as OriginalGraph;
1111

1212
/**
13-
* Class GraphViz
13+
* Class Graph
1414
*/
15-
class GraphViz implements RendererInterface
15+
class Graph implements ConverterInterface
1616
{
17-
/**
18-
* @var \drupol\phptree\Visitor\VisitorInterface
19-
*/
20-
private $visitor;
21-
2217
/**
2318
* @var \Fhaculty\Graph\Graph
2419
*/
2520
private $graph;
2621

2722
/**
28-
* @var \Graphp\GraphViz\GraphViz
29-
*/
30-
private $graphviz;
31-
32-
/**
33-
* GraphViz constructor.
34-
*
35-
* @param \drupol\phptree\Visitor\VisitorInterface $visitor
23+
* @var \drupol\phptree\Visitor\VisitorInterface
3624
*/
37-
public function __construct(VisitorInterface $visitor, Graph $graph, OriginalGraphViz $graphViz)
38-
{
39-
$this->visitor = $visitor;
40-
$this->graph = $graph;
41-
$this->graphviz = $graphViz;
42-
}
25+
private $visitor;
4326

4427
/**
45-
* @param \drupol\phptree\Node\NodeInterface $node
28+
* Graph constructor.
4629
*
47-
* @return string
30+
* @param \Fhaculty\Graph\Graph $graph
31+
* @param \drupol\phptree\Visitor\VisitorInterface|null $visitor
4832
*/
49-
public function render(NodeInterface $node): string
33+
public function __construct(OriginalGraph $graph = null, VisitorInterface $visitor = null)
5034
{
51-
return $this->graphviz->createScript($this->getGraph($node));
35+
$this->graph = $graph ?? new OriginalGraph();
36+
$this->visitor = $visitor ?? new BreadthFirstVisitor();
5237
}
5338

5439
/**
5540
* @param \drupol\phptree\Node\NodeInterface $node
5641
*
5742
* @return \Fhaculty\Graph\Graph
5843
*/
59-
public function getGraph(NodeInterface $node): Graph
44+
public function convert(NodeInterface $node): OriginalGraph
6045
{
6146
foreach ($this->visitor->traverse($node) as $node_visited) {
6247
/** @var int $hash */

src/Render/RendererInterface.php

-18
This file was deleted.

0 commit comments

Comments
 (0)