Skip to content

Commit

Permalink
Remove the Renderer and add the Converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 11, 2018
1 parent 66e1108 commit 3057801
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 150 deletions.
31 changes: 31 additions & 0 deletions spec/drupol/phptree/Converter/GraphSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types = 1);

namespace spec\drupol\phptree\Converter;

use drupol\phptree\Converter\Graph;
use drupol\phptree\Node\Node;
use PhpSpec\ObjectBehavior;

class GraphSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType(Graph::class);
}

public function it_can_generate_a_graph()
{
$root = new Node();
$level1 = new Node();
$level2 = new Node();

$root
->add($level1, $level2);

$this
->convert($root)
->shouldReturnAnInstanceOf(\Fhaculty\Graph\Graph::class);
}
}
81 changes: 0 additions & 81 deletions spec/drupol/phptree/tests/TestGraphVizSpec.php

This file was deleted.

22 changes: 0 additions & 22 deletions spec/src/TestGraphViz.php

This file was deleted.

20 changes: 20 additions & 0 deletions src/Converter/ConverterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types = 1);

namespace drupol\phptree\Converter;

use drupol\phptree\Node\NodeInterface;

/**
* Interface ConverterInterface.
*/
interface ConverterInterface
{
/**
* @param \drupol\phptree\Node\NodeInterface $node
*
* @return mixed
*/
public function convert(NodeInterface $node);
}
43 changes: 14 additions & 29 deletions src/Render/GraphViz.php → src/Converter/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,46 @@

declare(strict_types = 1);

namespace drupol\phptree\Render;
namespace drupol\phptree\Converter;

use drupol\phptree\Node\NodeInterface;
use drupol\phptree\Visitor\BreadthFirstVisitor;
use drupol\phptree\Visitor\VisitorInterface;
use Fhaculty\Graph\Graph;
use Graphp\GraphViz\GraphViz as OriginalGraphViz;
use Fhaculty\Graph\Graph as OriginalGraph;

/**
* Class GraphViz
* Class Graph
*/
class GraphViz implements RendererInterface
class Graph implements ConverterInterface
{
/**
* @var \drupol\phptree\Visitor\VisitorInterface
*/
private $visitor;

/**
* @var \Fhaculty\Graph\Graph
*/
private $graph;

/**
* @var \Graphp\GraphViz\GraphViz
*/
private $graphviz;

/**
* GraphViz constructor.
*
* @param \drupol\phptree\Visitor\VisitorInterface $visitor
* @var \drupol\phptree\Visitor\VisitorInterface
*/
public function __construct(VisitorInterface $visitor, Graph $graph, OriginalGraphViz $graphViz)
{
$this->visitor = $visitor;
$this->graph = $graph;
$this->graphviz = $graphViz;
}
private $visitor;

/**
* @param \drupol\phptree\Node\NodeInterface $node
* Graph constructor.
*
* @return string
* @param \Fhaculty\Graph\Graph $graph
* @param \drupol\phptree\Visitor\VisitorInterface|null $visitor
*/
public function render(NodeInterface $node): string
public function __construct(OriginalGraph $graph = null, VisitorInterface $visitor = null)
{
return $this->graphviz->createScript($this->getGraph($node));
$this->graph = $graph ?? new OriginalGraph();
$this->visitor = $visitor ?? new BreadthFirstVisitor();
}

/**
* @param \drupol\phptree\Node\NodeInterface $node
*
* @return \Fhaculty\Graph\Graph
*/
public function getGraph(NodeInterface $node): Graph
public function convert(NodeInterface $node): OriginalGraph
{
foreach ($this->visitor->traverse($node) as $node_visited) {
/** @var int $hash */
Expand Down
18 changes: 0 additions & 18 deletions src/Render/RendererInterface.php

This file was deleted.

0 comments on commit 3057801

Please sign in to comment.