Skip to content

Commit 87de76a

Browse files
committed
Let the Graph exporter work with Node instead of ValueNode.
1 parent 3b9b4f7 commit 87de76a

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

src/Exporter/Ascii.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace drupol\phptree\Exporter;
66

7-
use drupol\phptree\Node\ValueNodeInterface;
7+
use drupol\phptree\Node\NodeInterface;
88

99
/**
1010
* Class Ascii
@@ -14,7 +14,7 @@ class Ascii implements ExporterInterface
1414
/**
1515
* {@inheritdoc}
1616
*/
17-
public function export(ValueNodeInterface $node): string
17+
public function export(NodeInterface $node): string
1818
{
1919
$tree = new \RecursiveTreeIterator(
2020
new \RecursiveArrayIterator(
@@ -51,7 +51,7 @@ public function export(ValueNodeInterface $node): string
5151
* @return array
5252
* The tree exported into an array.
5353
*/
54-
private function doExportAsArray(ValueNodeInterface $node): array
54+
private function doExportAsArray(NodeInterface $node): array
5555
{
5656
$children = [];
5757
/** @var ValueNodeInterface $child */

src/Exporter/ExporterInterface.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace drupol\phptree\Exporter;
66

7-
use drupol\phptree\Node\ValueNodeInterface;
7+
use drupol\phptree\Node\NodeInterface;
88

99
/**
1010
* Interface ExporterInterface
@@ -14,11 +14,11 @@ interface ExporterInterface
1414
/**
1515
* Export a node into something.
1616
*
17-
* @param \drupol\phptree\Node\ValueNodeInterface $node
17+
* @param NodeInterface $node
1818
* The node.
1919
*
2020
* @return mixed
2121
* The node exported.
2222
*/
23-
public function export(ValueNodeInterface $node);
23+
public function export(NodeInterface $node);
2424
}

src/Exporter/Graph.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getTraverser(): TraverserInterface
6161
/**
6262
* {@inheritdoc}
6363
*/
64-
public function export(ValueNodeInterface $node): OriginalGraph
64+
public function export(NodeInterface $node): OriginalGraph
6565
{
6666
foreach ($this->getTraverser()->traverse($node) as $node_visited) {
6767
/** @var int $vertexId */
@@ -95,9 +95,18 @@ protected function createVertex(NodeInterface $node): Vertex
9595
/** @var int $vertexId */
9696
$vertexId = $this->createVertexId($node);
9797

98-
return false === $this->getGraph()->hasVertex($vertexId) ?
99-
$this->getGraph()->createVertex($vertexId):
100-
$vertex = $this->getGraph()->getVertex($vertexId);
98+
if (false === $this->getGraph()->hasVertex($vertexId)) {
99+
$vertex = $this->getGraph()->createVertex($vertexId);
100+
101+
$label = NULL;
102+
if (method_exists($node, 'getValue')) {
103+
$label = $node->getValue();
104+
}
105+
106+
$vertex->setAttribute('graphviz.label', $label);
107+
}
108+
109+
return $this->getGraph()->getVertex($vertexId);
101110
}
102111

103112
/**

src/Exporter/SimpleArray.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace drupol\phptree\Exporter;
66

7-
use drupol\phptree\Node\ValueNodeInterface;
7+
use drupol\phptree\Node\NodeInterface;
88

99
/**
1010
* Class SimpleArray
@@ -14,7 +14,7 @@ class SimpleArray implements ExporterInterface
1414
/**
1515
* {@inheritdoc}
1616
*/
17-
public function export(ValueNodeInterface $node)
17+
public function export(NodeInterface $node)
1818
{
1919
$children = [];
2020
/** @var ValueNodeInterface $child */

src/Exporter/Text.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace drupol\phptree\Exporter;
66

7-
use drupol\phptree\Node\ValueNodeInterface;
7+
use drupol\phptree\Node\NodeInterface;
88

99
/**
1010
* Class Text
@@ -14,7 +14,7 @@ class Text implements ExporterInterface
1414
/**
1515
* {@inheritdoc}
1616
*/
17-
public function export(ValueNodeInterface $node): string
17+
public function export(NodeInterface $node): string
1818
{
1919
$children = [];
2020
/** @var ValueNodeInterface $child */

0 commit comments

Comments
 (0)