1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\nested_tree; |
4
|
|
|
|
5
|
|
|
class Actions |
6
|
|
|
{ |
7
|
|
|
protected ?Support\Options $options = null; |
8
|
|
|
|
9
|
7 |
|
public function __construct( |
10
|
|
|
protected readonly NestedSet $nestedSet, |
11
|
|
|
) { |
12
|
7 |
|
} |
13
|
|
|
|
14
|
1 |
|
public function setExtraOptions(?Support\Options $options) : void |
15
|
|
|
{ |
16
|
1 |
|
$this->options = $options; |
17
|
|
|
} |
18
|
|
|
|
19
|
1 |
|
public function fixStructure() : void |
20
|
|
|
{ |
21
|
1 |
|
$this->nestedSet->rebuild($this->getOptions()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create new node in connected table |
26
|
|
|
* |
27
|
|
|
* @param Support\Node $node |
28
|
|
|
* @return Support\Node |
29
|
|
|
*/ |
30
|
1 |
|
public function create(Support\Node $node) : Support\Node |
31
|
|
|
{ |
32
|
1 |
|
$result = $this->nestedSet->add($node, $this->getOptions()); |
33
|
1 |
|
$this->nestedSet->rebuild($this->getOptions()); |
34
|
|
|
|
35
|
1 |
|
return $result; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Update node's data |
40
|
|
|
* |
41
|
|
|
* @param Support\Node $node |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
1 |
|
public function update(Support\Node $node) : bool |
45
|
|
|
{ |
46
|
1 |
|
$result = $this->nestedSet->update($node); |
47
|
1 |
|
$this->nestedSet->rebuild($this->getOptions()); |
48
|
|
|
|
49
|
1 |
|
return $result; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Move node to the position |
54
|
|
|
* |
55
|
|
|
* @param int<1, max> $nodeId |
56
|
|
|
* @param int<1, max> $newPosition |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
1 |
|
public function movePosition(int $nodeId, int $newPosition) : bool |
60
|
|
|
{ |
61
|
1 |
|
$result = $this->nestedSet->move($nodeId, $newPosition); |
62
|
1 |
|
$this->nestedSet->rebuild($this->getOptions()); |
63
|
|
|
|
64
|
1 |
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Change node's parent |
69
|
|
|
* |
70
|
|
|
* @param int<1, max> $nodeId |
71
|
|
|
* @param int<0, max>|null $newParent |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
1 |
|
public function changeParent(int $nodeId, ?int $newParent) : bool |
75
|
|
|
{ |
76
|
1 |
|
$result = $this->nestedSet->changeParent($nodeId, $newParent); |
77
|
1 |
|
$this->nestedSet->rebuild($this->getOptions()); |
78
|
|
|
|
79
|
1 |
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Delete node, with or without its children |
84
|
|
|
* |
85
|
|
|
* @param int<1, max> $nodeId |
86
|
|
|
* @param bool $childrenUp children will be reconnected to upper entry or deleted too |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
2 |
|
public function delete(int $nodeId, bool $childrenUp = false) : bool |
90
|
|
|
{ |
91
|
2 |
|
$result = $childrenUp |
92
|
1 |
|
? $this->nestedSet->deletePullUpChildren($nodeId, $this->getOptions()) |
93
|
1 |
|
: !empty($this->nestedSet->deleteWithChildren($nodeId, $this->getOptions())) |
94
|
2 |
|
; |
95
|
2 |
|
$this->nestedSet->rebuild($this->getOptions()); |
96
|
|
|
|
97
|
2 |
|
return $result; |
98
|
|
|
} |
99
|
|
|
|
100
|
7 |
|
protected function getOptions() : Support\Options |
101
|
|
|
{ |
102
|
7 |
|
return $this->options ?? new Support\Options(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|