Skip to content

Commit

Permalink
added flag to addChild() and addSibling() to be able to return the ch…
Browse files Browse the repository at this point in the history
…ild or the sibling, updated the docs
  • Loading branch information
franzose committed Feb 14, 2015
1 parent 6f5dfa0 commit 2b61953
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ $page->addChild($newChild);
//you can set child position
$page->addChild($newChild, 5);

//you can get the child
$child = $page->addChild($newChild, null, true);

$page->addChildren([$newChild, $newChild2]);

$page->getChildAt(5);
Expand Down Expand Up @@ -151,6 +154,9 @@ $sibligns = $page->getSiblingsRange(0, 2);
$page->addSibling(new Page);
$page->addSibling(new Page, 3); //third position

//add and get the sibling
$sibling = $page->addSibling(new Page, null, true);

$page->addSiblings([new Page, new Page]);
$page->addSiblings([new Page, new Page], 5); //insert from fifth position
```
Expand Down
6 changes: 6 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Direct descendants (children)
//you can set child position
$page->addChild($newChild, 5);
//you can get the child
$child = $page->addChild($newChild, null, true);
$page->addChildren([$newChild, $newChild2]);
$page->getChildAt(5);
Expand Down Expand Up @@ -107,6 +110,9 @@ Siblings
$page->addSibling(new Page);
$page->addSibling(new Page, 3); //third position
//add and get the sibling
$sibling = $page->addSibling(new Page, null, true);
$page->addSiblings([new Page, new Page]);
$page->addSiblings([new Page, new Page], 5); //insert from fifth position
Expand Down
10 changes: 6 additions & 4 deletions src/Franzose/ClosureTable/Contracts/EntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ public function getLastChild(array $columns = ['*']);
*
* @param EntityInterface $child
* @param int $position
* @return $this
* @param bool $returnChild
* @return EntityInterface
*/
public function addChild(EntityInterface $child, $position = null);
public function addChild(EntityInterface $child, $position = null, $returnChild = false);

/**
* Appends multiple children to the model.
Expand Down Expand Up @@ -387,9 +388,10 @@ public function getSiblingsRange($from, $to = null, array $columns = ['*']);
*
* @param EntityInterface $sibling
* @param int|null $position
* @return $this
* @param bool $returnSibling
* @return EntityInterface
*/
public function addSibling(EntityInterface $sibling, $position = null);
public function addSibling(EntityInterface $sibling, $position = null, $returnSibling = false);

/**
* Appends multiple siblings within the current depth.
Expand Down
14 changes: 8 additions & 6 deletions src/Franzose/ClosureTable/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,10 @@ protected function getLastChildPosition()
*
* @param EntityInterface $child
* @param int $position
* @return $this
* @param bool $returnChild
* @return EntityInterface
*/
public function addChild(EntityInterface $child, $position = null)
public function addChild(EntityInterface $child, $position = null, $returnChild = false)
{
if ($this->exists)
{
Expand All @@ -700,7 +701,7 @@ public function addChild(EntityInterface $child, $position = null)
$child->moveTo($position, $this);
}

return $this;
return ($returnChild === true ? $child : $this);
}

/**
Expand Down Expand Up @@ -1024,9 +1025,10 @@ public function getSiblingsRange($from, $to = null, array $columns = ['*'])
*
* @param EntityInterface $sibling
* @param int|null $position
* @return $this
* @param bool $returnSibling
* @return EntityInterface
*/
public function addSibling(EntityInterface $sibling, $position = null)
public function addSibling(EntityInterface $sibling, $position = null, $returnSibling = false)
{
if ($this->exists)
{
Expand All @@ -1038,7 +1040,7 @@ public function addSibling(EntityInterface $sibling, $position = null)
$sibling->moveTo($position, $this->parent_id);
}

return $this;
return ($returnSibling === true ? $sibling : $this);
}

/**
Expand Down

0 comments on commit 2b61953

Please sign in to comment.