Skip to content

Commit 6215ceb

Browse files
committed
Improve the Ascii exporter.
1 parent 36f7be7 commit 6215ceb

File tree

3 files changed

+24
-58
lines changed

3 files changed

+24
-58
lines changed

spec/drupol/phptree/Exporter/AsciiSpec.php

-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace spec\drupol\phptree\Exporter;
66

77
use drupol\phptree\Exporter\Ascii;
8-
use drupol\phptree\Node\Node;
98
use drupol\phptree\Node\ValueNode;
109
use PhpSpec\ObjectBehavior;
1110

@@ -96,21 +95,6 @@ public function it_can_export_to_ascii()
9695
->shouldReturn($expected);
9796
}
9897

99-
public function it_can_throw_an_error_when_tree_is_not_a_valuenode()
100-
{
101-
$tree = new Node();
102-
103-
foreach (\range('A', 'Z') as $key => $value) {
104-
$nodes[$value] = new Node();
105-
}
106-
107-
$tree->add(...\array_values($nodes));
108-
109-
$this
110-
->shouldThrow(\InvalidArgumentException::class)
111-
->during('export', [$tree]);
112-
}
113-
11498
public function it_is_initializable()
11599
{
116100
$this->shouldHaveType(Ascii::class);

src/Exporter/Ascii.php

+19-33
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function export(NodeInterface $node): string
2121
new \RecursiveArrayIterator(
2222
$this->doExportAsArray($node)
2323
),
24-
\RecursiveTreeIterator::BYPASS_KEY,
24+
\RecursiveTreeIterator::SELF_FIRST,
2525
\CachingIterator::CATCH_GET_CHILD,
2626
\RecursiveTreeIterator::SELF_FIRST
2727
);
@@ -46,6 +46,24 @@ 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 $node->getValue();
62+
}
63+
64+
return \sha1(\spl_object_hash($node));
65+
}
66+
4967
/**
5068
* Export the tree in an array.
5169
*
@@ -57,10 +75,6 @@ public function export(NodeInterface $node): string
5775
*/
5876
private function doExportAsArray(NodeInterface $node): array
5977
{
60-
if (!$this->isValidNode($node)) {
61-
throw new \InvalidArgumentException('Must implements ValueNodeInterface');
62-
}
63-
6478
$children = [];
6579
/** @var ValueNodeInterface $child */
6680
foreach ($node->children() as $child) {
@@ -71,32 +85,4 @@ private function doExportAsArray(NodeInterface $node): array
7185
[$this->getNodeRepresentation($node)] :
7286
[$this->getNodeRepresentation($node), $children];
7387
}
74-
75-
/**
76-
* Check if a node is valid for being exported.
77-
*
78-
* @param \drupol\phptree\Node\NodeInterface $node
79-
* The node.
80-
*
81-
* @return bool
82-
* True if it's valid, false otherwise.
83-
*/
84-
protected function isValidNode(NodeInterface $node): bool
85-
{
86-
return $node instanceof ValueNodeInterface;
87-
}
88-
89-
/**
90-
* Get a string representation of the node.
91-
*
92-
* @param \drupol\phptree\Node\NodeInterface $node
93-
* The node.
94-
*
95-
* @return string
96-
* The node representation.
97-
*/
98-
protected function getNodeRepresentation(NodeInterface $node): string
99-
{
100-
return $node->getValue();
101-
}
10288
}

src/Exporter/Gv.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ public function export(NodeInterface $node): string
3434
{
3535
$attributes = \array_map(
3636
function ($key, $data) {
37-
if (\is_string($data)) {
38-
return \sprintf(
39-
' %s = %s',
40-
$key,
41-
$data
42-
);
43-
}
44-
4537
if (\is_array($data)) {
4638
return \sprintf(
4739
' %s %s',
@@ -50,7 +42,11 @@ function ($key, $data) {
5042
);
5143
}
5244

53-
return null;
45+
return \sprintf(
46+
' %s = %s',
47+
$key,
48+
$data
49+
);
5450
},
5551
\array_keys($this->attributes),
5652
$this->attributes

0 commit comments

Comments
 (0)