Skip to content

Commit

Permalink
Implement the NodeInterface::remove() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 7, 2018
1 parent a94abdd commit 7420179
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
19 changes: 19 additions & 0 deletions spec/drupol/phptree/Node/NodeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ public function it_can_add(NodeInterface $node)
->shouldReturn($this);
}

public function it_can_remove()
{
$node1 = new Node();
$node2 = new Node();

$this
->add($node1, $node2);

$this
->remove($node2);

$this->children()[0]->shouldReturn($node1);

$this
->remove($node1);

$this->children()->shouldReturn([]);
}

public function it_can_get_the_size()
{
$nodes = [];
Expand Down
17 changes: 16 additions & 1 deletion src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(NodeInterface $parent = null)
/**
* {@inheritdoc}
*/
public function add(NodeInterface ...$nodes)
public function add(NodeInterface ...$nodes): NodeInterface
{
foreach ($nodes as $node) {
$this->storage['children'][] = $node->setParent($this);
Expand All @@ -39,6 +39,21 @@ public function add(NodeInterface ...$nodes)
return $this;
}

/**
* {@inheritdoc}
*/
public function remove(NodeInterface ...$nodes): NodeInterface
{
$this->storage['children'] = \array_filter(
$this->storage['children'],
function ($child) use ($nodes) {
return !\in_array($child, $nodes, true);
}
);

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
11 changes: 9 additions & 2 deletions src/Node/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ public function setParent(NodeInterface $node): NodeInterface;
/**
* @param \drupol\phptree\Node\NodeInterface ...$node
*
* @return mixed
* @return \drupol\phptree\Node\NodeInterface
*/
public function add(NodeInterface ...$node): NodeInterface;

/**
* @param \drupol\phptree\Node\NodeInterface ...$node
*
* @return \drupol\phptree\Node\NodeInterface
*/
public function add(NodeInterface ...$node);
public function remove(NodeInterface ...$node): NodeInterface;

/**
* @return NodeInterface[]
Expand Down

0 comments on commit 7420179

Please sign in to comment.