Skip to content

Commit

Permalink
Improve the Ascii exporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 27, 2019
1 parent 36f7be7 commit 6215ceb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 58 deletions.
16 changes: 0 additions & 16 deletions spec/drupol/phptree/Exporter/AsciiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace spec\drupol\phptree\Exporter;

use drupol\phptree\Exporter\Ascii;
use drupol\phptree\Node\Node;
use drupol\phptree\Node\ValueNode;
use PhpSpec\ObjectBehavior;

Expand Down Expand Up @@ -96,21 +95,6 @@ public function it_can_export_to_ascii()
->shouldReturn($expected);
}

public function it_can_throw_an_error_when_tree_is_not_a_valuenode()
{
$tree = new Node();

foreach (\range('A', 'Z') as $key => $value) {
$nodes[$value] = new Node();
}

$tree->add(...\array_values($nodes));

$this
->shouldThrow(\InvalidArgumentException::class)
->during('export', [$tree]);
}

public function it_is_initializable()
{
$this->shouldHaveType(Ascii::class);
Expand Down
52 changes: 19 additions & 33 deletions src/Exporter/Ascii.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function export(NodeInterface $node): string
new \RecursiveArrayIterator(
$this->doExportAsArray($node)
),
\RecursiveTreeIterator::BYPASS_KEY,
\RecursiveTreeIterator::SELF_FIRST,
\CachingIterator::CATCH_GET_CHILD,
\RecursiveTreeIterator::SELF_FIRST
);
Expand All @@ -46,6 +46,24 @@ public function export(NodeInterface $node): string
return $output;
}

/**
* Get a string representation of the node.
*
* @param \drupol\phptree\Node\NodeInterface $node
* The node.
*
* @return string
* The node representation.
*/
protected function getNodeRepresentation(NodeInterface $node): string
{
if ($node instanceof ValueNodeInterface) {
return $node->getValue();
}

return \sha1(\spl_object_hash($node));
}

/**
* Export the tree in an array.
*
Expand All @@ -57,10 +75,6 @@ public function export(NodeInterface $node): string
*/
private function doExportAsArray(NodeInterface $node): array
{
if (!$this->isValidNode($node)) {
throw new \InvalidArgumentException('Must implements ValueNodeInterface');
}

$children = [];
/** @var ValueNodeInterface $child */
foreach ($node->children() as $child) {
Expand All @@ -71,32 +85,4 @@ private function doExportAsArray(NodeInterface $node): array
[$this->getNodeRepresentation($node)] :
[$this->getNodeRepresentation($node), $children];
}

/**
* Check if a node is valid for being exported.
*
* @param \drupol\phptree\Node\NodeInterface $node
* The node.
*
* @return bool
* True if it's valid, false otherwise.
*/
protected function isValidNode(NodeInterface $node): bool
{
return $node instanceof ValueNodeInterface;
}

/**
* Get a string representation of the node.
*
* @param \drupol\phptree\Node\NodeInterface $node
* The node.
*
* @return string
* The node representation.
*/
protected function getNodeRepresentation(NodeInterface $node): string
{
return $node->getValue();
}
}
14 changes: 5 additions & 9 deletions src/Exporter/Gv.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ public function export(NodeInterface $node): string
{
$attributes = \array_map(
function ($key, $data) {
if (\is_string($data)) {
return \sprintf(
' %s = %s',
$key,
$data
);
}

if (\is_array($data)) {
return \sprintf(
' %s %s',
Expand All @@ -50,7 +42,11 @@ function ($key, $data) {
);
}

return null;
return \sprintf(
' %s = %s',
$key,
$data
);
},
\array_keys($this->attributes),
$this->attributes
Expand Down

0 comments on commit 6215ceb

Please sign in to comment.