1
|
|
|
<?php namespace Arcanedev\LaravelNestedSet\Utilities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelNestedSet\NodeTrait; |
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class NestedSet |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\LaravelNestedSet\Utilities |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class NestedSet |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Constants |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* The name of default lft column. |
20
|
|
|
*/ |
21
|
|
|
const LFT = '_lft'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The name of default rgt column. |
25
|
|
|
*/ |
26
|
|
|
const RGT = '_rgt'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The name of default parent id column. |
30
|
|
|
*/ |
31
|
|
|
const PARENT_ID = 'parent_id'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Insert direction. |
35
|
|
|
*/ |
36
|
|
|
const BEFORE = 1; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Insert direction. |
40
|
|
|
*/ |
41
|
|
|
const AFTER = 2; |
42
|
|
|
|
43
|
|
|
/* ----------------------------------------------------------------- |
44
|
|
|
| Getters & Setters |
45
|
|
|
| ----------------------------------------------------------------- |
46
|
|
|
*/ |
47
|
|
|
/** |
48
|
|
|
* Get a list of default columns. |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
216 |
|
public static function getDefaultColumns() |
53
|
|
|
{ |
54
|
216 |
|
return [self::LFT, self::RGT, self::PARENT_ID]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/* ----------------------------------------------------------------- |
58
|
|
|
| Migration Methods |
59
|
|
|
| ----------------------------------------------------------------- |
60
|
|
|
*/ |
61
|
|
|
/** |
62
|
|
|
* Add default nested set columns to the table. Also create an index. |
63
|
|
|
* |
64
|
|
|
* @param \Illuminate\Database\Schema\Blueprint $table |
65
|
|
|
*/ |
66
|
216 |
|
public static function columns(Blueprint $table) |
67
|
|
|
{ |
68
|
216 |
|
$table->unsignedInteger(self::LFT); |
69
|
216 |
|
$table->unsignedInteger(self::RGT); |
70
|
216 |
|
$table->unsignedInteger(self::PARENT_ID)->nullable(); |
71
|
|
|
|
72
|
216 |
|
$table->index(self::getDefaultColumns()); |
73
|
216 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Drop NestedSet columns. |
77
|
|
|
* |
78
|
|
|
* @param \Illuminate\Database\Schema\Blueprint $table |
79
|
|
|
*/ |
80
|
|
|
public static function dropColumns(Blueprint $table) |
81
|
|
|
{ |
82
|
|
|
$columns = self::getDefaultColumns(); |
83
|
|
|
|
84
|
|
|
$table->dropIndex($columns); |
85
|
|
|
$table->dropColumn($columns); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/* ----------------------------------------------------------------- |
89
|
|
|
| Check Methods |
90
|
|
|
| ----------------------------------------------------------------- |
91
|
|
|
*/ |
92
|
|
|
/** |
93
|
|
|
* Replaces instanceof calls for this trait. |
94
|
|
|
* |
95
|
|
|
* @param mixed $node |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
72 |
|
public static function isNode($node) |
100
|
|
|
{ |
101
|
72 |
|
return is_object($node) && in_array(NodeTrait::class, (array) $node); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|