Skip to content

Commit e7e9fa0

Browse files
authored
Fixes upstream issue lazychaser#538
1 parent ad464b7 commit e7e9fa0

File tree

5 files changed

+367
-4
lines changed

5 files changed

+367
-4
lines changed

src/NestedSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function getDefaultColumns()
7777
*/
7878
public static function isNode($node)
7979
{
80-
return is_object($node) && in_array(NodeTrait::class, (array)$node);
80+
return $node instanceof Node;
8181
}
8282

8383
}

src/Node.php

Lines changed: 363 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,363 @@
1+
<?php
2+
3+
namespace Kalnoy\Nestedset;
4+
5+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
6+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
8+
9+
/**
10+
* Accompanies {@link \Kalnoy\Nestedset\NodeTrait}.
11+
*
12+
* This interface declares all public methods of a node which are implemented
13+
* by {@link \Kalnoy\Nestedset\NodeTrait}.
14+
*
15+
* Every model which represents a node in a nested set, must realize this
16+
* interface.
17+
* This interface is mandatory such that
18+
* {@link \Kalnoy\Nestedset\NestedSet::isNode()} recognizes an object as a
19+
* node.
20+
*/
21+
interface Node
22+
{
23+
/**
24+
* Relation to the parent.
25+
*
26+
* @return BelongsTo
27+
*/
28+
public function parent();
29+
30+
/**
31+
* Relation to children.
32+
*
33+
* @return HasMany
34+
*/
35+
public function children();
36+
37+
/**
38+
* Get query for descendants of the node.
39+
*
40+
* @return DescendantsRelation
41+
*/
42+
public function descendants();
43+
44+
/**
45+
* Get query for siblings of the node.
46+
*
47+
* @return QueryBuilder
48+
*/
49+
public function siblings();
50+
51+
/**
52+
* Get the node siblings and the node itself.
53+
*
54+
* @return QueryBuilder
55+
*/
56+
public function siblingsAndSelf();
57+
58+
/**
59+
* Get query for the node siblings and the node itself.
60+
*
61+
* @param array $columns
62+
*
63+
* @return EloquentCollection
64+
*/
65+
public function getSiblingsAndSelf(array $columns = ['*']);
66+
67+
/**
68+
* Get query for siblings after the node.
69+
*
70+
* @return QueryBuilder
71+
*/
72+
public function nextSiblings();
73+
74+
/**
75+
* Get query for siblings before the node.
76+
*
77+
* @return QueryBuilder
78+
*/
79+
public function prevSiblings();
80+
81+
/**
82+
* Get query for nodes after current node.
83+
*
84+
* @return QueryBuilder
85+
*/
86+
public function nextNodes();
87+
88+
/**
89+
* Get query for nodes before current node in reversed order.
90+
*
91+
* @return QueryBuilder
92+
*/
93+
public function prevNodes();
94+
95+
/**
96+
* Get query ancestors of the node.
97+
*
98+
* @return AncestorsRelation
99+
*/
100+
public function ancestors();
101+
102+
/**
103+
* Make this node a root node.
104+
*
105+
* @return $this
106+
*/
107+
public function makeRoot();
108+
109+
/**
110+
* Save node as root.
111+
*
112+
* @return bool
113+
*/
114+
public function saveAsRoot();
115+
116+
/**
117+
* @param $lft
118+
* @param $rgt
119+
* @param $parentId
120+
*
121+
* @return $this
122+
*/
123+
public function rawNode($lft, $rgt, $parentId);
124+
125+
/**
126+
* Move node up given amount of positions.
127+
*
128+
* @param int $amount
129+
*
130+
* @return bool
131+
*/
132+
public function up($amount = 1);
133+
134+
/**
135+
* Move node down given amount of positions.
136+
*
137+
* @param int $amount
138+
*
139+
* @return bool
140+
*/
141+
public function down($amount = 1);
142+
143+
/**
144+
* @since 2.0
145+
*/
146+
public function newEloquentBuilder($query);
147+
148+
/**
149+
* Get a new base query that includes deleted nodes.
150+
*
151+
* @since 1.1
152+
*
153+
* @return QueryBuilder
154+
*/
155+
public function newNestedSetQuery($table = null);
156+
157+
/**
158+
* @param ?string $table
159+
*
160+
* @return QueryBuilder
161+
*/
162+
public function newScopedQuery($table = null);
163+
164+
/**
165+
* @param mixed $query
166+
* @param ?string $table
167+
*
168+
* @return mixed
169+
*/
170+
public function applyNestedSetScope($query, $table = null);
171+
172+
/**
173+
* @param array $attributes
174+
*
175+
* @return self
176+
*/
177+
public static function scoped(array $attributes);
178+
179+
public function newCollection(array $models = []);
180+
181+
/**
182+
* Get node height (rgt - lft + 1).
183+
*
184+
* @return int
185+
*/
186+
public function getNodeHeight();
187+
188+
/**
189+
* Get number of descendant nodes.
190+
*
191+
* @return int
192+
*/
193+
public function getDescendantCount();
194+
195+
/**
196+
* Set the value of model's parent id key.
197+
*
198+
* Behind the scenes node is appended to found parent node.
199+
*
200+
* @param int $value
201+
*
202+
* @throws \Exception If parent node doesn't exists
203+
*/
204+
public function setParentIdAttribute($value);
205+
206+
/**
207+
* Get whether node is root.
208+
*
209+
* @return bool
210+
*/
211+
public function isRoot();
212+
213+
/**
214+
* @return bool
215+
*/
216+
public function isLeaf();
217+
218+
/**
219+
* Get the lft key name.
220+
*
221+
* @return string
222+
*/
223+
public function getLftName();
224+
225+
/**
226+
* Get the rgt key name.
227+
*
228+
* @return string
229+
*/
230+
public function getRgtName();
231+
232+
/**
233+
* Get the parent id key name.
234+
*
235+
* @return string
236+
*/
237+
public function getParentIdName();
238+
239+
/**
240+
* Get the value of the model's lft key.
241+
*
242+
* @return int
243+
*/
244+
public function getLft();
245+
246+
/**
247+
* Get the value of the model's rgt key.
248+
*
249+
* @return int
250+
*/
251+
public function getRgt();
252+
253+
/**
254+
* Get the value of the model's parent id key.
255+
*
256+
* @return int
257+
*/
258+
public function getParentId();
259+
260+
/**
261+
* Returns node that is next to current node without constraining to siblings.
262+
*
263+
* This can be either a next sibling or a next sibling of the parent node.
264+
*
265+
* @param array $columns
266+
*
267+
* @return self
268+
*/
269+
public function getNextNode(array $columns = ['*']);
270+
271+
/**
272+
* Returns node that is before current node without constraining to siblings.
273+
*
274+
* This can be either a prev sibling or parent node.
275+
*
276+
* @param array $columns
277+
*
278+
* @return self
279+
*/
280+
public function getPrevNode(array $columns = ['*']);
281+
282+
/**
283+
* @param array $columns
284+
*
285+
* @return Collection
286+
*/
287+
public function getAncestors(array $columns = ['*']);
288+
289+
/**
290+
* @param array $columns
291+
*
292+
* @return Collection|self[]
293+
*/
294+
public function getDescendants(array $columns = ['*']);
295+
296+
/**
297+
* @param array $columns
298+
*
299+
* @return Collection|self[]
300+
*/
301+
public function getSiblings(array $columns = ['*']);
302+
303+
/**
304+
* @param array $columns
305+
*
306+
* @return Collection<self>
307+
*/
308+
public function getNextSiblings(array $columns = ['*']);
309+
310+
/**
311+
* @param array $columns
312+
*
313+
* @return Collection<self>
314+
*/
315+
public function getPrevSiblings(array $columns = ['*']);
316+
317+
/**
318+
* @param array $columns
319+
*
320+
* @return Node
321+
*/
322+
public function getNextSibling(array $columns = ['*']);
323+
324+
/**
325+
* @param array $columns
326+
*
327+
* @return Node
328+
*/
329+
public function getPrevSibling(array $columns = ['*']);
330+
331+
/**
332+
* @return array<int>
333+
*/
334+
public function getBounds();
335+
336+
/**
337+
* @param $value
338+
*
339+
* @return $this
340+
*/
341+
public function setLft($value);
342+
343+
/**
344+
* @param $value
345+
*
346+
* @return $this
347+
*/
348+
public function setRgt($value);
349+
350+
/**
351+
* @param $value
352+
*
353+
* @return $this
354+
*/
355+
public function setParentId($value);
356+
357+
/**
358+
* @param array|null $except
359+
*
360+
* @return $this
361+
*/
362+
public function replicate(array $except = null);
363+
}

tests/models/Category.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use \Illuminate\Database\Eloquent\Model;
44

5-
class Category extends Model {
5+
class Category extends Model implements \Kalnoy\Nestedset\Node {
66

77
use \Illuminate\Database\Eloquent\SoftDeletes, \Kalnoy\Nestedset\NodeTrait;
88

tests/models/DuplicateCategory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
class DuplicateCategory extends \Illuminate\Database\Eloquent\Model
3+
class DuplicateCategory extends \Illuminate\Database\Eloquent\Model implements \Kalnoy\Nestedset\Node
44
{
55
use \Kalnoy\Nestedset\NodeTrait;
66

tests/models/MenuItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33

4-
class MenuItem extends \Illuminate\Database\Eloquent\Model
4+
class MenuItem extends \Illuminate\Database\Eloquent\Model implements \Kalnoy\Nestedset\Node
55
{
66
use \Kalnoy\Nestedset\NodeTrait;
77

0 commit comments

Comments
 (0)