Skip to content

Commit b1d0d57

Browse files
committed
Resolved conflicts
2 parents 7443557 + 04647cd commit b1d0d57

File tree

6 files changed

+145
-142
lines changed

6 files changed

+145
-142
lines changed

src/Collection.php

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

33
namespace Kalnoy\Nestedset;
44

5-
use \Illuminate\Database\Eloquent\Collection as BaseCollection;
5+
use Illuminate\Database\Eloquent\Collection as BaseCollection;
6+
use Illuminate\Database\Eloquent\Model;
67

78
class Collection extends BaseCollection
89
{
@@ -19,15 +20,15 @@ public function linkNodes()
1920

2021
$groupedChildren = $this->groupBy($this->first()->getParentIdName());
2122

22-
/** @var Node $node */
23+
/** @var NodeTrait|Model $node */
2324
foreach ($this->items as $node) {
2425
if ( ! isset($node->parent)) {
2526
$node->setRelation('parent', null);
2627
}
2728

2829
$children = $groupedChildren->get($node->getKey(), [ ]);
2930

30-
/** @var Node $child */
31+
/** @var Model|NodeTrait $child */
3132
foreach ($children as $child) {
3233
$child->setRelation('parent', $node);
3334
}
@@ -46,7 +47,7 @@ public function linkNodes()
4647
* If `$rootNodeId` is provided, the tree will contain only descendants
4748
* of the node with such primary key value.
4849
*
49-
* @param int|Node|null $root
50+
* @param int|Model|null $root
5051
*
5152
* @return Collection
5253
*/
@@ -59,7 +60,7 @@ public function toTree($root = null)
5960

6061
$root = $this->getRootNodeId($root);
6162

62-
/** @var Node $node */
63+
/** @var Model|NodeTrait $node */
6364
foreach ($this->items as $node) {
6465
if ($node->getParentId() == $root) $items[] = $node;
6566
}
@@ -75,14 +76,16 @@ public function toTree($root = null)
7576
*/
7677
protected function getRootNodeId($root = null)
7778
{
78-
if ($root instanceof Node) return $root->getKey();
79+
if (NodeTrait::hasTrait($root)) {
80+
return $root->getKey();
81+
}
7982

8083
// If root node is not specified we take parent id of node with
8184
// least lft value as root node id.
8285
if ($root === null) {
8386
$leastValue = null;
8487

85-
/** @var Node $node */
88+
/** @var Model|NodeTrait $node */
8689
foreach ($this->items as $node) {
8790
if ($leastValue === null || $node->getLft() < $leastValue) {
8891
$leastValue = $node->getLft();

src/NestedSet.php

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

33
namespace Kalnoy\Nestedset;
44

5-
use Illuminate\Database\Connection;
65
use Illuminate\Database\Schema\Blueprint;
76

8-
class NestedSet {
7+
class NestedSet
8+
{
9+
/**
10+
* The name of default lft column.
11+
*/
12+
const LFT = '_lft';
13+
14+
/**
15+
* The name of default rgt column.
16+
*/
17+
const RGT = '_rgt';
18+
19+
/**
20+
* The name of default parent id column.
21+
*/
22+
const PARENT_ID = 'parent_id';
923

1024
/**
1125
* Add default nested set columns to the table. Also create an index.
1226
*
1327
* @param \Illuminate\Database\Schema\Blueprint $table
14-
* @param string $primaryKey
1528
*/
16-
public static function columns(Blueprint $table, $primaryKey = 'id')
29+
public static function columns(Blueprint $table)
1730
{
18-
$table->unsignedInteger(Node::LFT);
19-
$table->unsignedInteger(Node::RGT);
20-
$table->unsignedInteger(Node::PARENT_ID)->nullable();
31+
$table->unsignedInteger(self::LFT);
32+
$table->unsignedInteger(self::RGT);
33+
$table->unsignedInteger(self::PARENT_ID)->nullable();
2134

2235
$table->index(self::getDefaultColumns());
2336
}
@@ -37,12 +50,12 @@ public static function dropColumns(Blueprint $table)
3750

3851
/**
3952
* Get a list of default columns.
40-
*
53+
*
4154
* @return array
4255
*/
4356
public static function getDefaultColumns()
4457
{
45-
return [ Node::LFT, Node::RGT, Node::PARENT_ID ];
58+
return [ self::LFT, self::RGT, self::PARENT_ID ];
4659
}
4760

4861
}

0 commit comments

Comments
 (0)