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