-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace spec\drupol\phptree\tests; | ||
|
||
use drupol\phptree\Node\ValueNode; | ||
use drupol\phptree\tests\TestGraphViz; | ||
use drupol\phptree\Visitor\BreadthFirstVisitor; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class TestGraphVizSpec extends ObjectBehavior | ||
{ | ||
public function it_is_initializable() | ||
{ | ||
$visitor = new BreadthFirstVisitor(); | ||
|
||
$this->beConstructedWith($visitor); | ||
|
||
$this->shouldHaveType(TestGraphViz::class); | ||
} | ||
|
||
public function it_can_create_a_graph() | ||
{ | ||
$this->beConstructedWith(new BreadthFirstVisitor()); | ||
|
||
$tree = new ValueNode('root', 2); | ||
|
||
$nodes = []; | ||
foreach (\range('A', 'F') as $letter) { | ||
$nodes[] = new ValueNode($letter, 2); | ||
} | ||
|
||
$tree->add(...$nodes); | ||
|
||
$result = <<< EOF | ||
digraph G { | ||
"root" -> "A" | ||
"root" -> "B" | ||
"A" -> "C" | ||
"A" -> "D" | ||
"B" -> "E" | ||
"B" -> "F" | ||
} | ||
EOF; | ||
|
||
$this | ||
->render($tree) | ||
->shouldReturn($result . PHP_EOL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace drupol\phptree\tests; | ||
|
||
use drupol\phptree\Node\NodeInterface; | ||
use drupol\phptree\Render\GraphViz; | ||
|
||
/** | ||
* Class TestGraphViz | ||
*/ | ||
class TestGraphViz extends GraphViz | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function hash(NodeInterface $node) | ||
{ | ||
return $node->getValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace drupol\phptree\Render; | ||
|
||
use drupol\phptree\Node\NodeInterface; | ||
use drupol\phptree\Visitor\VisitorInterface; | ||
use Fhaculty\Graph\Graph; | ||
|
||
/** | ||
* Class GraphViz | ||
*/ | ||
class GraphViz implements RendererInterface | ||
{ | ||
/** | ||
* @var \drupol\phptree\Visitor\VisitorInterface | ||
*/ | ||
private $visitor; | ||
|
||
/** | ||
* @var \Fhaculty\Graph\Graph | ||
*/ | ||
private $graph; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $nodes; | ||
|
||
/** | ||
* @var \Graphp\GraphViz\GraphViz | ||
*/ | ||
private $graphviz; | ||
|
||
/** | ||
* GraphViz constructor. | ||
* | ||
* @param \drupol\phptree\Visitor\VisitorInterface $visitor | ||
*/ | ||
public function __construct(VisitorInterface $visitor) | ||
{ | ||
$this->visitor = $visitor; | ||
$this->graph = new Graph(); | ||
$this->graphviz = new \Graphp\GraphViz\GraphViz(); | ||
$this->nodes = []; | ||
} | ||
|
||
/** | ||
* @param \drupol\phptree\Node\NodeInterface $node | ||
* | ||
* @return string | ||
*/ | ||
public function render(NodeInterface $node): string | ||
{ | ||
foreach ($this->visitor->traverse($node) as $child) { | ||
$hash_parent = $this->hash($child); | ||
|
||
if (!isset($this->nodes[$hash_parent])) { | ||
$this->nodes[$hash_parent] = $this->graph->createVertex($hash_parent); | ||
} | ||
|
||
if (null === $parent = $child->getParent()) { | ||
continue; | ||
} | ||
|
||
$hash = $this->hash($parent); | ||
|
||
if (!isset($this->nodes[$hash])) { | ||
$this->nodes[$hash] = $this->graph->createVertex($hash); | ||
} | ||
|
||
$this->nodes[$hash]->createEdgeTo($this->nodes[$hash_parent]); | ||
} | ||
|
||
return $this->graphviz->createScript($this->graph); | ||
} | ||
|
||
/** | ||
* @param \drupol\phptree\Node\NodeInterface $node | ||
* | ||
* @return int | ||
*/ | ||
protected function hash(NodeInterface $node) | ||
{ | ||
return (int) \spl_object_hash($node); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace drupol\phptree\Render; | ||
|
||
use drupol\phptree\Node\NodeInterface; | ||
|
||
/** | ||
* Interface RendererInterface | ||
*/ | ||
interface RendererInterface | ||
{ | ||
/** | ||
* @param \drupol\phptree\Node\NodeInterface $node | ||
*/ | ||
public function render(NodeInterface $node); | ||
} |