Skip to content

Commit

Permalink
Merge pull request #361 from Einenlum/fix/add-nullable-setchildof
Browse files Browse the repository at this point in the history
Add nullable setchildof
  • Loading branch information
Albin Kerouanton committed Sep 21, 2017
2 parents 3e7ffa4 + 67660fc commit 789efc7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/Model/Tree/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,28 @@ public function isChildNodeOf(NodeInterface $node)
/**
* {@inheritdoc}
**/
public function setChildNodeOf(NodeInterface $node)
public function setChildNodeOf(NodeInterface $node = null)
{
$id = $this->getNodeId();
if (empty($id)) {
throw new \LogicException('You must provide an id for this node if you want it to be part of a tree.');
}

$path = rtrim($node->getRealMaterializedPath(), static::getMaterializedPathSeparator());
$path = null !== $node
? rtrim($node->getRealMaterializedPath(), static::getMaterializedPathSeparator())
: static::getMaterializedPathSeparator()
;
$this->setMaterializedPath($path);

if (null !== $this->parentNode) {
$this->parentNode->getChildNodes()->removeElement($this);
}

$this->parentNode = $node;
$this->parentNode->addChildNode($this);

if (null !== $node) {
$this->parentNode->addChildNode($this);
}

foreach ($this->getChildNodes() as $child) {
$child->setChildNodeOf($this);
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Tree/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public function setMaterializedPath($path);
* - Remove the this node from the children of the old parent
* - Modify the materialized path of this node and all its children, recursively
*
* @param NodeInterface $node The node to use as a parent
* @param NodeInterface | null $node The node to use as a parent
*
* @return NodeInterface $this Fluent interface
**/
public function setChildNodeOf(NodeInterface $node);
public function setChildNodeOf(NodeInterface $node = null);

/**
* @param NodeInterface $node the node to append to the children collection
Expand Down

0 comments on commit 789efc7

Please sign in to comment.