Skip to content

Commit 90db782

Browse files
committed
Added some assertions
1 parent fd7f68f commit 90db782

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/Node.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,8 @@ public function prependTo(Node $parent)
504504
*/
505505
public function appendOrPrependTo(Node $parent, $prepend = false)
506506
{
507-
if ( ! $parent->exists) {
508-
throw new LogicException('Cannot use non-existing node as a parent.');
509-
}
507+
$this->assertNodeExists($parent)
508+
->assertNotDescendant($parent);
510509

511510
$this->setParent($parent)->dirtyBounds();
512511

@@ -545,9 +544,8 @@ public function beforeNode(Node $node)
545544
*/
546545
public function beforeOrAfterNode(Node $node, $after = false)
547546
{
548-
if ( ! $node->exists) {
549-
throw new LogicException('Cannot insert before/after a node that does not exists.');
550-
}
547+
$this->assertNodeExists($node)
548+
->assertNotDescendant($node);
551549

552550
if ( ! $this->isSiblingOf($node)) {
553551
$this->setParent($node->getRelationValue('parent'));
@@ -1237,6 +1235,34 @@ protected static function reorderNodes(Collection $models, &$fixed,
12371235
return $cut;
12381236
}
12391237

1238+
/**
1239+
* @param Node $node
1240+
*
1241+
* @return $this
1242+
*/
1243+
protected function assertNotDescendant(Node $node)
1244+
{
1245+
if ($node == $this || $node->isDescendantOf($this)) {
1246+
throw new LogicException('Node must not be a descendant.');
1247+
}
1248+
1249+
return $this;
1250+
}
1251+
1252+
/**
1253+
* @param Node $node
1254+
*
1255+
* @return $this
1256+
*/
1257+
protected function assertNodeExists(Node $node)
1258+
{
1259+
if ( ! $node->getLft() || ! $node->getRgt()) {
1260+
throw new LogicException('Node must exists.');
1261+
}
1262+
1263+
return $this;
1264+
}
1265+
12401266
// public static function rebuildTree(array $nodes, $createNodes = true, $deleteNodes = false)
12411267
// {
12421268
// $model = new static;

0 commit comments

Comments
 (0)