|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use SleepingOwl\Admin\Display\Tree\BaumNodeType; |
|
6
|
|
|
use SleepingOwl\Admin\Display\Tree\SimpleTreeType; |
|
7
|
|
|
use SleepingOwl\Admin\Display\Tree\KalnoyNestedsetType; |
|
8
|
|
|
use SleepingOwl\Admin\Exceptions\Display\DisplayTreeException; |
|
9
|
|
|
use SleepingOwl\Admin\Contracts\Display\Tree\TreeTypeInterface; |
|
10
|
|
|
use SleepingOwl\Admin\Contracts\Repositories\TreeRepositoryInterface; |
|
11
|
|
|
|
|
12
|
|
|
class TreeRepository extends BaseRepository implements TreeRepositoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Parent field name. |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $parentField = 'parent_id'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Order field name. |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $orderField = 'order'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Root parent id value. |
|
28
|
|
|
* @var null |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $rootParentId = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var TreeTypeInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $treeType; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $treeType |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($treeType = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if (! is_null($treeType)) { |
|
43
|
|
|
$this->setTreeType($treeType); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $treeType |
|
49
|
|
|
* |
|
50
|
|
|
* @throws DisplayTreeException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setTreeType($treeType) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->treeType = new $treeType($this); |
|
55
|
|
|
|
|
56
|
|
|
if (! ($this->treeType instanceof TreeTypeInterface)) { |
|
57
|
|
|
throw new DisplayTreeException('Tree type class must be instanced of [SleepingOwl\Admin\Contracts\Display\Tree\TreeTypeInterface]'); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $class |
|
63
|
|
|
* |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setClass($class) |
|
67
|
|
|
{ |
|
68
|
|
|
parent::setClass($class); |
|
69
|
|
|
|
|
70
|
|
|
if (is_null($this->treeType)) { |
|
71
|
|
|
$this->detectType(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get tree structure. |
|
79
|
|
|
* |
|
80
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $collection |
|
81
|
|
|
* |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getTree(\Illuminate\Database\Eloquent\Collection $collection) |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->treeType->getTree($collection); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get parent field name. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getParentField() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->parentField; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $parentField |
|
101
|
|
|
* |
|
102
|
|
|
* @return $this |
|
103
|
|
|
*/ |
|
104
|
|
|
public function setParentField($parentField) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->parentField = $parentField; |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get order field name. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getOrderField() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->orderField; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $orderField |
|
123
|
|
|
* |
|
124
|
|
|
* @return $this |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setOrderField($orderField) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->orderField = $orderField; |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get or set parent field name. |
|
135
|
|
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getRootParentId() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->rootParentId; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $rootParentId |
|
145
|
|
|
* |
|
146
|
|
|
* @return $this |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setRootParentId($rootParentId) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->rootParentId = $rootParentId; |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Reorder tree by $data value. |
|
157
|
|
|
* |
|
158
|
|
|
* @param $data |
|
159
|
|
|
*/ |
|
160
|
|
|
public function reorder(array $data) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->treeType->reorder($data); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Detect tree type. |
|
167
|
|
|
* @return $this |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function detectType() |
|
170
|
|
|
{ |
|
171
|
|
|
$model = $this->getModel(); |
|
172
|
|
|
|
|
173
|
|
|
$type = SimpleTreeType::class; |
|
174
|
|
|
|
|
175
|
|
|
if ($model instanceof \Baum\Node) { |
|
|
|
|
|
|
176
|
|
|
$type = BaumNodeType::class; |
|
177
|
|
|
} elseif (class_exists('Kalnoy\Nestedset\Node') and $model instanceof \Kalnoy\Nestedset\Node) { |
|
|
|
|
|
|
178
|
|
|
$type = KalnoyNestedsetType::class; |
|
179
|
|
|
} elseif (function_exists('trait_uses_recursive') and $traits = trait_uses_recursive($model) and in_array('Kalnoy\Nestedset\NodeTrait', $traits)) { |
|
|
|
|
|
|
180
|
|
|
$type = KalnoyNestedsetType::class; |
|
181
|
|
|
} elseif ($traits = class_uses($model) and in_array('Kalnoy\Nestedset\NodeTrait', $traits)) { |
|
|
|
|
|
|
182
|
|
|
$type = KalnoyNestedsetType::class; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$this->setTreeType($type); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..