Skip to content

Commit e06cc5f

Browse files
committed
Adds findParent method
1 parent aee1600 commit e06cc5f

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/NestedSetInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ public function findChildren(Node $node);
7171
*/
7272
public function findAncestors(Node $node);
7373

74+
/**
75+
* Finds the parent node.
76+
*
77+
* @param \PNX\NestedSet\Node $node
78+
* The node.
79+
*
80+
* @return Node
81+
* The parent node.
82+
*/
83+
public function findParent(Node $node);
84+
7485
/**
7586
* Gets a node for the ID and Revision ID.
7687
*

src/Storage/DbalNestedSet.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ public function findAncestors(Node $node) {
152152
return $ancestors;
153153
}
154154

155+
/**
156+
* {@inheritdoc}
157+
*/
158+
public function findParent(Node $node) {
159+
$ancestors = $this->findAncestors($node);
160+
if (count($ancestors) > 1) {
161+
// Parent is 2nd-last element.
162+
return $ancestors[count($ancestors) - 2];
163+
}
164+
return NULL;
165+
}
166+
155167
/**
156168
* {@inheritdoc}
157169
*/

tests/Functional/DbalNestedSetTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ public function testFindAncestors() {
143143

144144
}
145145

146+
/**
147+
* Tests finding the parent node.
148+
*/
149+
public function testFindParent() {
150+
151+
$node = $this->nestedSet->getNode(7, 1);
152+
153+
$parent = $this->nestedSet->findParent($node);
154+
155+
$this->assertEquals(3, $parent->getId());
156+
$this->assertEquals(1, $parent->getRevisionId());
157+
$this->assertEquals(10, $parent->getLeft());
158+
$this->assertEquals(21, $parent->getRight());
159+
160+
}
161+
146162
/**
147163
* Tests adding a node.
148164
*/

0 commit comments

Comments
 (0)