Skip to content

Commit 5d8abe5

Browse files
committed
Adds inserting a root node
1 parent c793488 commit 5d8abe5

File tree

3 files changed

+114
-29
lines changed

3 files changed

+114
-29
lines changed

src/NestedSetInterface.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface NestedSetInterface {
1818
* @return \PNX\NestedSet\Node
1919
* Returns a new node with position values set.
2020
*/
21-
public function insertNodeBelow(Node $target, Node $node);
21+
public function addNodeBelow(Node $target, Node $node);
2222

2323
/**
2424
* Inserts a node before the target node.
@@ -31,7 +31,7 @@ public function insertNodeBelow(Node $target, Node $node);
3131
* @return \PNX\NestedSet\Node
3232
* Returns a node with position values set.
3333
*/
34-
public function insertNodeBefore(Node $target, Node $node);
34+
public function addNodeBefore(Node $target, Node $node);
3535

3636
/**
3737
* Inserts a node after the target node.
@@ -44,7 +44,18 @@ public function insertNodeBefore(Node $target, Node $node);
4444
* @return \PNX\NestedSet\Node
4545
* Returns a node with position values set.
4646
*/
47-
public function insertNodeAfter(Node $target, Node $node);
47+
public function addNodeAfter(Node $target, Node $node);
48+
49+
/**
50+
* Inserts a root node.
51+
*
52+
* @param \PNX\NestedSet\Node $node
53+
* The root node.
54+
*
55+
* @return \PNX\NestedSet\Node
56+
* A new node with position values set.
57+
*/
58+
public function addRootNode(Node $node);
4859

4960
/**
5061
* Deletes a node and moves descendants up a level.

src/Storage/DbalNestedSet.php

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ public function __construct(Connection $connection) {
3232
/**
3333
* {@inheritdoc}
3434
*/
35-
public function insertNodeBelow(Node $target, Node $node) {
35+
public function addRootNode(Node $node) {
36+
$maxRight = $this->connection->fetchColumn('SELECT MAX(nested_right) FROM tree');
37+
if ($maxRight === FALSE) {
38+
$maxRight = 0;
39+
}
40+
return $this->doInsertNode($node->getId(), $node->getRevisionId(), $maxRight + 1, $maxRight + 2, 0);
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function addNodeBelow(Node $target, Node $node) {
3647
$newLeftPosition = $target->getRight();
3748
$depth = $target->getDepth() + 1;
3849
return $this->insertNodeAtPostion($newLeftPosition, $depth, $node);
@@ -41,7 +52,7 @@ public function insertNodeBelow(Node $target, Node $node) {
4152
/**
4253
* {@inheritdoc}
4354
*/
44-
public function insertNodeBefore(Node $target, Node $node) {
55+
public function addNodeBefore(Node $target, Node $node) {
4556
$newLeftPosition = $target->getLeft();
4657
$depth = $target->getDepth();
4758
return $this->insertNodeAtPostion($newLeftPosition, $depth, $node);
@@ -50,7 +61,7 @@ public function insertNodeBefore(Node $target, Node $node) {
5061
/**
5162
* {@inheritdoc}
5263
*/
53-
public function insertNodeAfter(Node $target, Node $node) {
64+
public function addNodeAfter(Node $target, Node $node) {
5465
$newLeftPosition = $target->getRight() + 1;
5566
$depth = $target->getDepth();
5667
return $this->insertNodeAtPostion($newLeftPosition, $depth, $node);
@@ -85,23 +96,8 @@ protected function insertNodeAtPostion($newLeftPosition, $depth, Node $node) {
8596
[$newLeftPosition]
8697
);
8798

88-
// Create a new node object to be returned.
89-
$newNode = new Node(
90-
$node->getId(),
91-
$node->getRevisionId(),
92-
$newLeftPosition,
93-
$newLeftPosition + 1,
94-
$depth
95-
);
96-
97-
// Insert the new node.
98-
$this->connection->insert('tree', [
99-
'id' => $newNode->getId(),
100-
'revision_id' => $newNode->getRevisionId(),
101-
'nested_left' => $newNode->getLeft(),
102-
'nested_right' => $newNode->getRight(),
103-
'depth' => $newNode->getDepth(),
104-
]);
99+
// Insert the node.
100+
$newNode = $this->doInsertNode($node->getId(), $node->getRevisionId(), $newLeftPosition, $newLeftPosition + 1, $depth);
105101

106102
$this->connection->commit();
107103
}
@@ -113,6 +109,39 @@ protected function insertNodeAtPostion($newLeftPosition, $depth, Node $node) {
113109

114110
}
115111

112+
/**
113+
* Inserts a new node by its parameters.
114+
*
115+
* @param int|string $id
116+
* The node ID.
117+
* @param int|string $revisionId
118+
* The node revision ID.
119+
* @param int $left
120+
* The left position.
121+
* @param int $right
122+
* The right position.
123+
* @param int $depth
124+
* The depth.
125+
*
126+
* @return \PNX\NestedSet\Node
127+
* The new node.
128+
*/
129+
protected function doInsertNode($id, $revisionId, $left, $right, $depth) {
130+
// Create a new node object to be returned.
131+
$newNode = new Node($id, $revisionId, $left, $right, $depth);
132+
133+
// Insert the new node.
134+
$this->connection->insert('tree', [
135+
'id' => $newNode->getId(),
136+
'revision_id' => $newNode->getRevisionId(),
137+
'nested_left' => $newNode->getLeft(),
138+
'nested_right' => $newNode->getRight(),
139+
'depth' => $newNode->getDepth(),
140+
]);
141+
142+
return $newNode;
143+
}
144+
116145
/**
117146
* {@inheritdoc}
118147
*/

tests/Functional/DbalNestedSetTest.php

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function testInsertNodeBelowWithExistingChildren() {
167167
$parent = $this->nestedSet->getNode(3, 1);
168168
$child = new Node(12, 1);
169169

170-
$newNode = $this->nestedSet->insertNodeBelow($parent, $child);
170+
$newNode = $this->nestedSet->addNodeBelow($parent, $child);
171171

172172
// Should be inserted in right-most spot.
173173
$this->assertEquals(21, $newNode->getLeft());
@@ -188,7 +188,7 @@ public function testInsertNodeBelowWithNoChildren() {
188188
$target = $this->nestedSet->getNode(6, 1);
189189
$node = new Node(13, 1);
190190

191-
$newNode = $this->nestedSet->insertNodeBelow($target, $node);
191+
$newNode = $this->nestedSet->addNodeBelow($target, $node);
192192

193193
// Should be inserted below 6 with depth 4.
194194
$this->assertEquals(7, $newNode->getLeft());
@@ -208,7 +208,7 @@ public function testInsertNodeBefore() {
208208
$parent = $this->nestedSet->getNode(6, 1);
209209
$child = new Node(14, 1);
210210

211-
$newNode = $this->nestedSet->insertNodeBefore($parent, $child);
211+
$newNode = $this->nestedSet->addNodeBefore($parent, $child);
212212

213213
// Should be inserted below 6 with depth 4.
214214
$this->assertEquals(6, $newNode->getLeft());
@@ -228,7 +228,7 @@ public function testInsertNodeAfter() {
228228
$parent = $this->nestedSet->getNode(5, 1);
229229
$child = new Node(15, 1);
230230

231-
$newNode = $this->nestedSet->insertNodeAfter($parent, $child);
231+
$newNode = $this->nestedSet->addNodeAfter($parent, $child);
232232

233233
// Should be inserted below 6 with depth 4.
234234
$this->assertEquals(6, $newNode->getLeft());
@@ -358,12 +358,57 @@ public function testMoveSubTreeBefore() {
358358

359359
}
360360

361+
/**
362+
* Tests inserting a root node to an empty tree.
363+
*/
364+
public function testAddRootNodeWhenEmpty() {
365+
366+
$rootNode = $this->nestedSet->getNode(1, 1);
367+
368+
$this->nestedSet->deleteSubTree($rootNode);
369+
370+
$node = new Node(12, 1);
371+
372+
$newNode = $this->nestedSet->addRootNode($node);
373+
374+
$this->assertEquals(1, $newNode->getLeft());
375+
$this->assertEquals(2, $newNode->getRight());
376+
$this->assertEquals(0, $newNode->getDepth());
377+
}
378+
379+
/**
380+
* Tests inserting a root node to an existing tree.
381+
*/
382+
public function testAddRootNode() {
383+
$node = new Node(12, 1);
384+
385+
$newNode = $this->nestedSet->addRootNode($node);
386+
387+
$this->assertEquals(23, $newNode->getLeft());
388+
$this->assertEquals(24, $newNode->getRight());
389+
$this->assertEquals(0, $newNode->getDepth());
390+
}
391+
392+
/**
393+
* Drops the table.
394+
*/
395+
protected function dropTable() {
396+
$schema = new Schema();
397+
$schema->dropTable("tree");
398+
foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) {
399+
$this->connection->exec($sql);
400+
}
401+
}
402+
361403
/**
362404
* Creates the table.
405+
*
406+
* @param string $tableName
407+
* The table name.
363408
*/
364-
protected function createTable() {
409+
protected function createTable($tableName = 'tree') {
365410
$schema = new Schema();
366-
$tree = $schema->createTable("tree");
411+
$tree = $schema->createTable($tableName);
367412
$tree->addColumn("id", "integer", ["unsigned" => TRUE]);
368413
$tree->addColumn("revision_id", "integer", ["unsigned" => TRUE]);
369414
$tree->addColumn("nested_left", "integer", ["unsigned" => TRUE]);

0 commit comments

Comments
 (0)