Skip to content

Commit 7420179

Browse files
committed
Implement the NodeInterface::remove() method.
1 parent a94abdd commit 7420179

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

spec/drupol/phptree/Node/NodeSpec.php

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ public function it_can_add(NodeInterface $node)
2121
->shouldReturn($this);
2222
}
2323

24+
public function it_can_remove()
25+
{
26+
$node1 = new Node();
27+
$node2 = new Node();
28+
29+
$this
30+
->add($node1, $node2);
31+
32+
$this
33+
->remove($node2);
34+
35+
$this->children()[0]->shouldReturn($node1);
36+
37+
$this
38+
->remove($node1);
39+
40+
$this->children()->shouldReturn([]);
41+
}
42+
2443
public function it_can_get_the_size()
2544
{
2645
$nodes = [];

src/Node/Node.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(NodeInterface $parent = null)
3030
/**
3131
* {@inheritdoc}
3232
*/
33-
public function add(NodeInterface ...$nodes)
33+
public function add(NodeInterface ...$nodes): NodeInterface
3434
{
3535
foreach ($nodes as $node) {
3636
$this->storage['children'][] = $node->setParent($this);
@@ -39,6 +39,21 @@ public function add(NodeInterface ...$nodes)
3939
return $this;
4040
}
4141

42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function remove(NodeInterface ...$nodes): NodeInterface
46+
{
47+
$this->storage['children'] = \array_filter(
48+
$this->storage['children'],
49+
function ($child) use ($nodes) {
50+
return !\in_array($child, $nodes, true);
51+
}
52+
);
53+
54+
return $this;
55+
}
56+
4257
/**
4358
* {@inheritdoc}
4459
*/

src/Node/NodeInterface.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@ public function setParent(NodeInterface $node): NodeInterface;
2424
/**
2525
* @param \drupol\phptree\Node\NodeInterface ...$node
2626
*
27-
* @return mixed
27+
* @return \drupol\phptree\Node\NodeInterface
28+
*/
29+
public function add(NodeInterface ...$node): NodeInterface;
30+
31+
/**
32+
* @param \drupol\phptree\Node\NodeInterface ...$node
33+
*
34+
* @return \drupol\phptree\Node\NodeInterface
2835
*/
29-
public function add(NodeInterface ...$node);
36+
public function remove(NodeInterface ...$node): NodeInterface;
3037

3138
/**
3239
* @return NodeInterface[]

0 commit comments

Comments
 (0)