Skip to content

Commit ad76c72

Browse files
authored
Add ability to move subtree to root (#11)
1 parent 4324809 commit ad76c72

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

src/NestedSetInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ public function findParent(Node $node);
132132
*/
133133
public function getNode($id, $revision_id);
134134

135+
/**
136+
* Moves a subtree to be a new root of the tree.
137+
*
138+
* @param \PNX\NestedSet\Node $node
139+
* The node to become the new root node.
140+
*/
141+
public function moveSubTreeToRoot(Node $node);
142+
135143
/**
136144
* Moves a node and its sub-tree below the target node.
137145
*

src/Storage/DbalNestedSet.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ class DbalNestedSet extends BaseDbalStorage implements NestedSetInterface {
1515
* {@inheritdoc}
1616
*/
1717
public function addRootNode(Node $node) {
18-
$maxRight = $this->connection->fetchColumn('SELECT MAX(right_pos) FROM ' . $this->tableName);
19-
if ($maxRight === FALSE) {
20-
$maxRight = 0;
21-
}
18+
$maxRight = $this->findMaxRightPosition();
2219
return $this->doInsertNode($node->getId(), $node->getRevisionId(), $maxRight + 1, $maxRight + 2, 0);
2320
}
2421

@@ -305,6 +302,14 @@ public function deleteSubTree(Node $node) {
305302

306303
}
307304

305+
/**
306+
* {@inheritdoc}
307+
*/
308+
public function moveSubTreeToRoot(Node $node) {
309+
$root = $this->findRoot($node);
310+
$this->moveSubTreeBefore($root, $node);
311+
}
312+
308313
/**
309314
* {@inheritdoc}
310315
*/
@@ -401,4 +406,18 @@ public function getNodeAtPosition($left) {
401406
}
402407
}
403408

409+
/**
410+
* Finds the maximum right position of the tree.
411+
*
412+
* @return int
413+
* The max right position.
414+
*/
415+
protected function findMaxRightPosition() {
416+
$maxRight = $this->connection->fetchColumn('SELECT MAX(right_pos) FROM ' . $this->tableName);
417+
if ($maxRight === FALSE) {
418+
$maxRight = 0;
419+
}
420+
return $maxRight;
421+
}
422+
404423
}

tests/Functional/DbalNestedSetTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,29 @@ public function testMoveSubTreeBelow() {
346346

347347
}
348348

349+
/**
350+
* Tests moving a sub-tree to the root.
351+
*/
352+
public function testMoveSubTreeToRoot() {
353+
354+
$node = $this->nestedSet->getNode(7, 1);
355+
356+
$this->nestedSet->moveSubTreeToRoot($node);
357+
358+
// Assert we are at the root now.
359+
$newRoot = $this->nestedSet->getNode(7, 1);
360+
$this->assertEquals(1, $newRoot->getLeft());
361+
$this->assertEquals(6, $newRoot->getRight());
362+
$this->assertEquals(0, $newRoot->getDepth());
363+
364+
// Assert old root has had left and right updated.
365+
$oldRoot = $this->nestedSet->getNode(1, 1);
366+
$this->assertEquals(7, $oldRoot->getLeft());
367+
$this->assertEquals(22, $oldRoot->getRight());
368+
$this->assertEquals(0, $oldRoot->getDepth());
369+
370+
}
371+
349372
/**
350373
* Tests moving a sub-tree before a target node.
351374
*/

0 commit comments

Comments
 (0)