Skip to content

Commit

Permalink
Add new withChildren() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 10, 2018
1 parent 4295b3d commit 9aedb95
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 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 @@ -183,4 +183,23 @@ public function it_can_get_its_sibblings()
->getSibblings()
->shouldReturn([$node2, $node3]);
}

public function it_can_use_withChildren()
{
$this
->withChildren()
->shouldNotReturn($this);

$child = new Node();

$this
->withChildren($child)
->children()
->shouldReturn([$child]);

$this
->withChildren()
->children()
->shouldReturn([]);
}
}
15 changes: 15 additions & 0 deletions src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,19 @@ function ($carry, $node) {
0
);
}

/**
* {@inheritdoc}
*/
public function withChildren(NodeInterface ...$nodes): NodeInterface
{
$clone = clone $this;
$clone->storage['children'] = [];

foreach ($nodes as $node) {
$clone->add($node);
}

return $clone;
}
}

0 comments on commit 9aedb95

Please sign in to comment.