From 0c903bba360145541e86a50b8c235f545d0b0755 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sat, 15 Jun 2019 10:46:10 +0200 Subject: [PATCH] Issue #8: Remove the space between elements. --- spec/drupol/phptree/Exporter/TextSpec.php | 18 +--------------- spec/drupol/phptree/Node/ValueNodeSpec.php | 10 ++++----- src/Exporter/Text.php | 25 ++++++++++++++++------ 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/spec/drupol/phptree/Exporter/TextSpec.php b/spec/drupol/phptree/Exporter/TextSpec.php index 2c43c94..e4a7f17 100644 --- a/spec/drupol/phptree/Exporter/TextSpec.php +++ b/spec/drupol/phptree/Exporter/TextSpec.php @@ -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; @@ -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() diff --git a/spec/drupol/phptree/Node/ValueNodeSpec.php b/spec/drupol/phptree/Node/ValueNodeSpec.php index b743cc4..2d34d30 100644 --- a/spec/drupol/phptree/Node/ValueNodeSpec.php +++ b/spec/drupol/phptree/Node/ValueNodeSpec.php @@ -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')]); @@ -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); @@ -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); diff --git a/src/Exporter/Text.php b/src/Exporter/Text.php index 4f078c1..0de95cf 100644 --- a/src/Exporter/Text.php +++ b/src/Exporter/Text.php @@ -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); } }