Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #8: Remove the space between elements. #10

Merged
merged 1 commit into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}