1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace StefanoTree\NestedSet\MoveStrategy; |
6
|
|
|
|
7
|
|
|
use StefanoTree\Exception\TreeIsBrokenException; |
8
|
|
|
use StefanoTree\Exception\ValidationException; |
9
|
|
|
|
10
|
|
|
class Top extends MoveStrategyAbstract implements MoveStrategyInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
2 |
|
protected function canMoveBranch(): void |
16
|
|
|
{ |
17
|
2 |
|
if ($this->isTargetNodeInsideSourceBranch()) { |
18
|
|
|
throw new ValidationException('Cannot move. Target node is inside source branch.'); |
19
|
|
|
} |
20
|
|
|
|
21
|
2 |
|
if ($this->getTargetNodeInfo()->isRoot()) { |
22
|
1 |
|
throw new ValidationException('Cannot move. Target node is root. Root node cannot have sibling.'); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
1 |
|
protected function isSourceNodeAtRequiredPosition(): bool |
30
|
|
|
{ |
31
|
1 |
|
$source = $this->getSourceNodeInfo(); |
32
|
1 |
|
$target = $this->getTargetNodeInfo(); |
33
|
|
|
|
34
|
1 |
|
return ($target->getLeft() == ($source->getRight() + 1) && $target->getParentId() == $source->getParentId()) ? true : false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
1 |
|
protected function updateParentId(): void |
41
|
|
|
{ |
42
|
1 |
|
$newParentId = $this->getTargetNodeInfo()->getParentId(); |
43
|
1 |
|
$this->_updateParentId($this->getSourceNodeInfo(), $newParentId); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
1 |
|
protected function updateLevels(): void |
50
|
|
|
{ |
51
|
1 |
|
$source = $this->getSourceNodeInfo(); |
52
|
|
|
|
53
|
1 |
|
$levelShift = $this->getTargetNodeInfo()->getLevel() - $source->getLevel(); |
54
|
1 |
|
$this->_updateLevels($source, $levelShift); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
1 |
|
protected function makeHole(): void |
61
|
|
|
{ |
62
|
1 |
|
$holeFromIndex = $this->getTargetNodeInfo()->getLeft() - 1; |
63
|
1 |
|
$indexShift = $this->getIndexShift(); |
64
|
1 |
|
$scope = $this->getSourceNodeInfo()->getScope(); |
65
|
|
|
|
66
|
1 |
|
$this->_makeHole($holeFromIndex, $indexShift, $scope); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
1 |
|
protected function moveBranchToTheHole(): void |
73
|
|
|
{ |
74
|
1 |
|
$source = $this->getSourceNodeInfo(); |
75
|
1 |
|
$target = $this->getTargetNodeInfo(); |
76
|
|
|
|
77
|
1 |
|
if ($this->isMovedToRoot() || $this->isMovedUp()) { |
78
|
1 |
|
$leftIndex = $source->getLeft() + $this->getIndexShift(); |
79
|
1 |
|
$rightIndex = $source->getRight() + $this->getIndexShift(); |
80
|
1 |
|
$indexShift = $target->getLeft() - $source->getRight() - 1; |
81
|
1 |
|
} elseif ($this->isMovedDown()) { |
82
|
1 |
|
$leftIndex = $source->getLeft(); |
83
|
1 |
|
$rightIndex = $source->getRight(); |
84
|
1 |
|
$indexShift = $target->getLeft() - $source->getLeft(); |
85
|
|
|
} else { |
86
|
|
|
throw new TreeIsBrokenException(); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$scope = $source->getScope(); |
90
|
|
|
|
91
|
1 |
|
$this->_moveBranchToTheHole($leftIndex, $rightIndex, $indexShift, $scope); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
1 |
|
protected function patchHole(): void |
98
|
|
|
{ |
99
|
1 |
|
$source = $this->getSourceNodeInfo(); |
100
|
|
|
|
101
|
1 |
|
if ($this->isMovedToRoot()) { |
102
|
1 |
|
$fromIndex = $source->getRight() + $this->getIndexShift(); |
103
|
1 |
|
} elseif ($this->isMovedUp()) { |
104
|
1 |
|
$fromIndex = $source->getRight(); |
105
|
1 |
|
} elseif ($this->isMovedDown()) { |
106
|
1 |
|
$fromIndex = $source->getLeft(); |
107
|
|
|
} else { |
108
|
|
|
throw new TreeIsBrokenException(); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
$indexShift = $this->getIndexShift() * -1; |
112
|
1 |
|
$scope = $source->getScope(); |
113
|
|
|
|
114
|
1 |
|
$this->_patchHole($fromIndex, $indexShift, $scope); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|