Skip to content

Commit

Permalink
Issue #8: Remove the space between elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 15, 2019
1 parent a969acd commit 0c903bb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
18 changes: 1 addition & 17 deletions spec/drupol/phptree/Exporter/TextSpec.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\Text;
use drupol\phptree\Node\Node;
use drupol\phptree\Node\ValueNode;
use PhpSpec\ObjectBehavior;

Expand All @@ -28,22 +27,7 @@ public function it_can_export_to_text()

$this
->export($tree)
->shouldReturn('[root [A [C [G] [H]] [D [I] [J]]] [B [E] [F]]]');
}

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]);
->shouldReturn('[root[A[C[G][H]][D[I][J]]][B[E][F]]]');
}

public function it_is_initializable()
Expand Down
10 changes: 5 additions & 5 deletions spec/drupol/phptree/Node/ValueNodeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public function it_can_be_manipulated_like_an_array()

$tree[25] = new ValueNode('Foo');

$export = '[root [a] [b] [c] [d] [e] [f] [g] [h] [i] [j] [k] [l] [m] [n] [o] [p] [q] [r] [s] [t] [u] [v] [w] [x] [y] [Foo]]';
$export = '[root[a][b][c][d][e][f][g][h][i][j][k][l][m][n][o][p][q][r][s][t][u][v][w][x][y][Foo]]';
$this->shouldHaveSameTextExport($export);

unset($tree[25]);
$export = '[root [a] [b] [c] [d] [e] [f] [g] [h] [i] [j] [k] [l] [m] [n] [o] [p] [q] [r] [s] [t] [u] [v] [w] [x] [y]]';
$export = '[root[a][b][c][d][e][f][g][h][i][j][k][l][m][n][o][p][q][r][s][t][u][v][w][x][y]]';
$this->shouldHaveSameTextExport($export);

$this->shouldThrow(\OutOfBoundsException::class)->during('offsetSet', [30, new ValueNode('out')]);
Expand Down Expand Up @@ -103,7 +103,7 @@ public function it_can_have_children()
$tree->add(new ValueNode($value, 3));
}

$export = '[root [A [C [I] [J] [K]] [D [L] [M] [N]] [E [O] [P] [Q]]] [B [F [R] [S] [T]] [G [U] [V] [W]] [H [X] [Y] [Z]]]]';
$export = '[root[A[C[I][J][K]][D[L][M][N]][E[O][P][Q]]][B[F[R][S][T]][G[U][V][W]][H[X][Y][Z]]]]';

$this->shouldHaveSameTextExport($export);
$this->shouldHaveSameGraph($tree);
Expand All @@ -121,12 +121,12 @@ public function it_can_remove()
$this
->add($node1, $node2);

$this->shouldHaveSameTextExport('[root [A] [B]]');
$this->shouldHaveSameTextExport('[root[A][B]]');

$this
->remove($node2);

$this->shouldHaveSameTextExport('[root [A]]');
$this->shouldHaveSameTextExport('[root[A]]');
$this
->count()
->shouldReturn(1);
Expand Down
25 changes: 19 additions & 6 deletions src/Exporter/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,31 @@ class Text implements ExporterInterface
*/
public function export(NodeInterface $node): string
{
if (!($node instanceof ValueNodeInterface)) {
throw new \InvalidArgumentException('Must implements ValueNodeInterface');
}

$children = [];

/** @var ValueNodeInterface $child */
foreach ($node->children() as $child) {
$children[] = $this->export($child);
}

return [] === $children ?
\sprintf('[%s]', $node->getValue()) :
\sprintf('[%s %s]', $node->getValue(), \implode(' ', $children));
\sprintf('[%s]', $this->getNodeValue($node)) :
\sprintf('[%s%s]', $this->getNodeValue($node), \implode('', $children));
}

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

0 comments on commit 0c903bb

Please sign in to comment.