Skip to content

Commit e5ed8cf

Browse files
committed
Add an abstracted exporter class to reduce duplicated code and allow customizations.
1 parent b941eb9 commit e5ed8cf

File tree

6 files changed

+48
-53
lines changed

6 files changed

+48
-53
lines changed

src/Exporter/AbstractExporter.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace drupol\phptree\Exporter;
6+
7+
use drupol\phptree\Node\NodeInterface;
8+
use drupol\phptree\Node\ValueNodeInterface;
9+
10+
/**
11+
* Class AbstractExporter.
12+
*/
13+
abstract class AbstractExporter implements ExporterInterface
14+
{
15+
/**
16+
* Get a string representation of the node.
17+
*
18+
* @param \drupol\phptree\Node\NodeInterface $node
19+
* The node.
20+
*
21+
* @return string
22+
* The node representation.
23+
*/
24+
protected function getNodeRepresentation(NodeInterface $node): string
25+
{
26+
if ($node instanceof ValueNodeInterface) {
27+
return (string) $node->getValue();
28+
}
29+
30+
return \sha1(\spl_object_hash($node));
31+
}
32+
}

src/Exporter/Ascii.php

+1-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Class Ascii.
1212
*/
13-
class Ascii implements ExporterInterface
13+
class Ascii extends AbstractExporter
1414
{
1515
/**
1616
* {@inheritdoc}
@@ -46,24 +46,6 @@ public function export(NodeInterface $node): string
4646
return $output;
4747
}
4848

49-
/**
50-
* Get a string representation of the node.
51-
*
52-
* @param \drupol\phptree\Node\NodeInterface $node
53-
* The node.
54-
*
55-
* @return string
56-
* The node representation.
57-
*/
58-
protected function getNodeRepresentation(NodeInterface $node): string
59-
{
60-
if ($node instanceof ValueNodeInterface) {
61-
return (string) $node->getValue();
62-
}
63-
64-
return \sha1(\spl_object_hash($node));
65-
}
66-
6749
/**
6850
* Export the tree in an array.
6951
*

src/Exporter/Graph.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66

77
use drupol\phptree\Node\AttributeNodeInterface;
88
use drupol\phptree\Node\NodeInterface;
9-
use drupol\phptree\Node\ValueNodeInterface;
109
use Fhaculty\Graph\Graph as OriginalGraph;
1110
use Fhaculty\Graph\Vertex;
1211

1312
/**
1413
* Class Graph.
1514
*/
16-
class Graph implements ExporterInterface
15+
class Graph extends AbstractExporter
1716
{
1817
/**
1918
* The graph.
@@ -59,9 +58,10 @@ protected function createVertex(NodeInterface $node): Vertex
5958
if (false === $this->getGraph()->hasVertex($vertexId)) {
6059
$vertex = $this->getGraph()->createVertex($vertexId);
6160

62-
if ($node instanceof ValueNodeInterface) {
63-
$vertex->setAttribute('graphviz.label', $node->getValue());
64-
}
61+
$vertex->setAttribute(
62+
'graphviz.label',
63+
$this->getNodeRepresentation($node)
64+
);
6565

6666
if ($node instanceof AttributeNodeInterface) {
6767
foreach ($node->getAttributes() as $key => $value) {

src/Exporter/Gv.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66

77
use drupol\phptree\Node\AttributeNodeInterface;
88
use drupol\phptree\Node\NodeInterface;
9-
use drupol\phptree\Node\ValueNodeInterface;
109

1110
/**
1211
* Class Gv.
1312
*/
14-
class Gv implements ExporterInterface
13+
class Gv extends AbstractExporter
1514
{
1615
/**
1716
* The graph attributes.
@@ -217,11 +216,9 @@ protected function getHash(NodeInterface $node): string
217216
*/
218217
protected function getNodeAttributes(NodeInterface $node): array
219218
{
220-
$attributes = [];
221-
222-
if ($node instanceof ValueNodeInterface) {
223-
$attributes['label'] = $node->getValue();
224-
}
219+
$attributes = [
220+
'label' => $this->getNodeRepresentation($node),
221+
];
225222

226223
if ($node instanceof AttributeNodeInterface) {
227224
foreach ($node->getAttributes() as $key => $value) {

src/Exporter/SimpleArray.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Class SimpleArray.
1212
*/
13-
class SimpleArray implements ExporterInterface
13+
class SimpleArray extends AbstractExporter
1414
{
1515
/**
1616
* {@inheritdoc}
@@ -28,7 +28,7 @@ public function export(NodeInterface $node)
2828
}
2929

3030
return [] === $children ?
31-
['value' => $node->getValue()] :
32-
['value' => $node->getValue(), 'children' => $children];
31+
['value' => $this->getNodeRepresentation($node)] :
32+
['value' => $this->getNodeRepresentation($node), 'children' => $children];
3333
}
3434
}

src/Exporter/Text.php

+3-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Class Text.
1212
*/
13-
class Text implements ExporterInterface
13+
class Text extends AbstractExporter
1414
{
1515
/**
1616
* {@inheritdoc}
@@ -25,23 +25,7 @@ public function export(NodeInterface $node): string
2525
}
2626

2727
return [] === $children ?
28-
\sprintf('[%s]', $this->getNodeValue($node)) :
29-
\sprintf('[%s%s]', $this->getNodeValue($node), \implode('', $children));
30-
}
31-
32-
/**
33-
* Get a string representation of a node.
34-
*
35-
* @param \drupol\phptree\Node\NodeInterface $node
36-
* The node.
37-
*
38-
* @return string
39-
* The string representation.
40-
*/
41-
protected function getNodeValue(NodeInterface $node): string
42-
{
43-
return $node instanceof ValueNodeInterface ?
44-
(string) $node->getValue() :
45-
(string) \spl_object_hash($node);
28+
\sprintf('[%s]', $this->getNodeRepresentation($node)) :
29+
\sprintf('[%s%s]', $this->getNodeRepresentation($node), \implode('', $children));
4630
}
4731
}

0 commit comments

Comments
 (0)