Skip to content

Commit 9959a59

Browse files
committed
New functions
1 parent 36d945d commit 9959a59

File tree

3 files changed

+158
-20
lines changed

3 files changed

+158
-20
lines changed

src/Kalnoy/Nestedset/Node.php

Lines changed: 134 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function children()
7676
/**
7777
* Get query for descendants of the node.
7878
*
79-
* @return \Illuminate\Database\Eloquent\Builder
79+
* @return \Kalnoy\Nestedset\QueryBuilder
8080
*/
8181
public function descendants()
8282
{
@@ -90,35 +90,38 @@ public function descendants()
9090
*
9191
* @param self::AFTER|self::BEFORE|null $dir
9292
*
93-
* @return \Illuminate\Database\Eloquent\Builder
93+
* @return \Kalnoy\Nestedset\QueryBuilder
9494
*/
9595
public function siblings($dir = null)
9696
{
97-
$query = $this->newQuery();
98-
99-
$query->where(static::PARENT_ID, '=', $this->getParentId());
100-
10197
switch ($dir)
10298
{
10399
case self::AFTER:
104-
$query->where(static::LFT, '>', $this->getRgt());
100+
$query = $this->next();
101+
105102
break;
106103

107104
case self::BEFORE:
108-
$query->where(static::LFT, '<', $this->getLft());
105+
$query = $this->prev();
106+
109107
break;
110108

111109
default:
112-
$query->where($this->getKeyName(), '<>', $this->getKey());
110+
$query = $this->newQuery()
111+
->where($this->getKeyName(), '<>', $this->getKey());
112+
113+
break;
113114
}
114115

116+
$query->where(static::PARENT_ID, '=', $this->getParentId());
117+
115118
return $query;
116119
}
117120

118121
/**
119122
* Get query for siblings after the node.
120123
*
121-
* @return \Illuminate\Database\Eloquent\Builder
124+
* @return \Kalnoy\Nestedset\QueryBuilder
122125
*/
123126
public function nextSiblings()
124127
{
@@ -128,17 +131,40 @@ public function nextSiblings()
128131
/**
129132
* Get query for siblings before the node.
130133
*
131-
* @return \Illuminate\Database\Eloquent\Builder
134+
* @return \Kalnoy\Nestedset\QueryBuilder
132135
*/
133136
public function prevSiblings()
134137
{
135138
return $this->siblings(self::BEFORE);
136139
}
137140

141+
/**
142+
* Get query for nodes after current node.
143+
*
144+
* @return \Kalnoy\Nestedset\QueryBuilder
145+
*/
146+
public function next()
147+
{
148+
return $this->newQuery()
149+
->where(static::LFT, '>', $this->attributes[static::LFT]);
150+
}
151+
152+
/**
153+
* Get query for nodes before current node in reversed order.
154+
*
155+
* @return \Kalnoy\Nestedset\QueryBuilder
156+
*/
157+
public function prev()
158+
{
159+
return $this->newQuery()
160+
->where(static::LFT, '<', $this->attributes[static::LFT])
161+
->reversed();
162+
}
163+
138164
/**
139165
* Get query for ancestors to the node not including the node itself.
140166
*
141-
* @return \Illuminate\Database\Eloquent\Builder
167+
* @return \Kalnoy\Nestedset\QueryBuilder
142168
*/
143169
public function ancestors()
144170
{
@@ -683,4 +709,100 @@ public function getParentId()
683709
{
684710
return $this->attributes[static::PARENT_ID];
685711
}
712+
713+
/**
714+
* Shorthand for next()
715+
*
716+
* @param array $columns
717+
*
718+
* @return \Kalnoy\Nestedset\Node
719+
*/
720+
public function getNext(array $columns = array('*'))
721+
{
722+
return $this->next()->first($columns);
723+
}
724+
725+
/**
726+
* Shorthand for prev()
727+
*
728+
* @param array $columns
729+
*
730+
* @return \Kalnoy\Nestedset\Node
731+
*/
732+
public function getPrev(array $columns = array('*'))
733+
{
734+
return $this->prev()->first($columns);
735+
}
736+
737+
/**
738+
* Shorthand for ancestors()
739+
*
740+
* @param array $columns
741+
*
742+
* @return \Kalnoy\Nestedset\Collection
743+
*/
744+
public function getAncestors(array $columns = array('*'))
745+
{
746+
return $this->ancestors()->get($columns);
747+
}
748+
749+
/**
750+
* Shorthand for descendants()
751+
*
752+
* @param array $columns
753+
*
754+
* @return \Kalnoy\Nestedset\Collection
755+
*/
756+
public function getDescendants(array $columns = array('*'))
757+
{
758+
return $this->descendants()->get($columns);
759+
}
760+
761+
/**
762+
* Shorthand for nextSiblings().
763+
*
764+
* @param array $columns
765+
*
766+
* @return \Kalnoy\Nestedset\Collection
767+
*/
768+
public function getNextSiblings(array $columns = array('*'))
769+
{
770+
return $this->nextSiblings()->get($columns);
771+
}
772+
773+
/**
774+
* Shorthand for prevSiblings().
775+
*
776+
* @param array $columns
777+
*
778+
* @return \Kalnoy\Nestedset\Collection
779+
*/
780+
public function getPrevSiblings(array $columns = array('*'))
781+
{
782+
return $this->prevSiblings()->get($columns);
783+
}
784+
785+
/**
786+
* Get next sibling.
787+
*
788+
* @param array $columns
789+
*
790+
* @return \Kalnoy\Nestedset\Node
791+
*/
792+
public function getNextSibling(array $columns = array('*'))
793+
{
794+
return $this->nextSiblings()->first($columns);
795+
}
796+
797+
/**
798+
* Get previous sibling.
799+
*
800+
* @param array $columns
801+
*
802+
* @return \Kalnoy\Nestedset\Node
803+
*/
804+
public function getPrevSibling(array $columns = array('*'))
805+
{
806+
return $this->prevSiblings()->reversed()->first($columns);
807+
}
686808
}

tests/NodeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,20 @@ public function testToTreeBuildsWithRootItemIdProvided()
380380
$this->assertEquals('samsung', $root->name);
381381
$this->assertEquals(1, count($root->children));
382382
}
383+
384+
public function testRetrievesNextNode()
385+
{
386+
$node = $this->findCategory('apple');
387+
$next = $node->next()->first();
388+
389+
$this->assertEquals('lenovo', $next->name);
390+
}
391+
392+
public function testRetrievesPrevNode()
393+
{
394+
$node = $this->findCategory('apple');
395+
$next = $node->prev()->first();
396+
397+
$this->assertEquals('notebooks', $next->name);
398+
}
383399
}

tests/data/categories.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
return array(
44
array('id' => 1, 'name' => 'store', '_lft' => 1, '_rgt' => 18, 'parent_id' => null),
5-
array('id' => 2, 'name' => 'notebooks', '_lft' => 2, '_rgt' => 7, 'parent_id' => 1),
6-
array('id' => 3, 'name' => 'apple', '_lft' => 3, '_rgt' => 4, 'parent_id' => 2),
7-
array('id' => 4, 'name' => 'lenovo', '_lft' => 5, '_rgt' => 6, 'parent_id' => 2),
8-
array('id' => 5, 'name' => 'mobile', '_lft' => 8, '_rgt' => 17, 'parent_id' => 1),
9-
array('id' => 6, 'name' => 'nokia', '_lft' => 9, '_rgt' => 10, 'parent_id' => 5),
10-
array('id' => 7, 'name' => 'samsung', '_lft' => 11, '_rgt' => 14, 'parent_id' => 5),
11-
array('id' => 8, 'name' => 'galaxy', '_lft' => 12, '_rgt' => 13, 'parent_id' => 7),
12-
array('id' => 9, 'name' => 'sony', '_lft' => 15, '_rgt' => 16, 'parent_id' => 5),
5+
array('id' => 2, 'name' => 'notebooks', '_lft' => 2, '_rgt' => 7, 'parent_id' => 1),
6+
array('id' => 3, 'name' => 'apple', '_lft' => 3, '_rgt' => 4, 'parent_id' => 2),
7+
array('id' => 4, 'name' => 'lenovo', '_lft' => 5, '_rgt' => 6, 'parent_id' => 2),
8+
array('id' => 5, 'name' => 'mobile', '_lft' => 8, '_rgt' => 17, 'parent_id' => 1),
9+
array('id' => 6, 'name' => 'nokia', '_lft' => 9, '_rgt' => 10, 'parent_id' => 5),
10+
array('id' => 7, 'name' => 'samsung', '_lft' => 11, '_rgt' => 14, 'parent_id' => 5),
11+
array('id' => 8, 'name' => 'galaxy', '_lft' => 12, '_rgt' => 13, 'parent_id' => 7),
12+
array('id' => 9, 'name' => 'sony', '_lft' => 15, '_rgt' => 16, 'parent_id' => 5),
1313
);

0 commit comments

Comments
 (0)