1
|
|
|
<?php |
2
|
|
|
namespace StefanoTree\NestedSet\MoveStrategy; |
3
|
|
|
|
4
|
|
|
use StefanoTree\Exception; |
5
|
|
|
|
6
|
|
|
class Bottom |
7
|
|
|
extends MoveStrategyAbstract |
|
|
|
|
8
|
|
|
{ |
9
|
6 |
|
public function getNewParentId() |
10
|
|
|
{ |
11
|
6 |
|
return $this->getTargetNode() |
12
|
6 |
|
->getParentId(); |
13
|
|
|
} |
14
|
|
|
|
15
|
6 |
|
public function getLevelShift() |
16
|
|
|
{ |
17
|
6 |
|
return $this->getTargetNode()->getLevel() - $this->getSourceNode()->getLevel(); |
18
|
|
|
} |
19
|
|
|
|
20
|
6 |
|
public function getHoleLeftIndex() |
21
|
|
|
{ |
22
|
6 |
|
if ($this->isMovedToRoot() || $this->isMovedDown()) { |
23
|
6 |
|
return $this->getSourceNode()->getLeft(); |
24
|
3 |
|
} elseif ($this->isMovedUp()) { |
25
|
3 |
|
return $this->getSourceNode()->getLeft() + $this->getIndexShift(); |
26
|
|
|
} else { |
27
|
|
|
throw new Exception\BaseException('Cannot move node'); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
6 |
|
public function getHoleRightIndex() |
32
|
|
|
{ |
33
|
6 |
|
if ($this->isMovedToRoot() || $this->isMovedDown()) { |
34
|
6 |
|
return $this->getSourceNode()->getRight(); |
35
|
3 |
|
} elseif ($this->isMovedUp()) { |
36
|
3 |
|
return $this->getSourceNode()->getRight() + $this->getIndexShift(); |
37
|
|
|
} else { |
38
|
|
|
throw new Exception\BaseException('Cannot move node'); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
6 |
|
public function getSourceNodeIndexShift() |
43
|
|
|
{ |
44
|
6 |
|
if ($this->isMovedToRoot() || $this->isMovedDown()) { |
45
|
6 |
|
return $this->getTargetNode()->getRight() - $this->getSourceNode()->getLeft() + 1; |
46
|
3 |
|
} elseif ($this->isMovedUp()) { |
47
|
3 |
|
return $this->getTargetNode()->getRight() - $this->getSourceNode()->getLeft() |
48
|
3 |
|
+ 1 - $this->getIndexShift(); |
49
|
|
|
} else { |
50
|
|
|
throw new Exception\BaseException('Cannot move node'); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
6 |
|
public function fixHoleFromIndex() |
55
|
|
|
{ |
56
|
6 |
|
if ($this->isMovedToRoot() || $this->isMovedDown()) { |
57
|
6 |
|
return $this->getSourceNode()->getLeft(); |
58
|
3 |
|
} elseif ($this->isMovedUp()) { |
59
|
3 |
|
return $this->getSourceNode()->getLeft() + $this->getIndexShift(); |
60
|
|
|
} else { |
61
|
|
|
throw new Exception\BaseException('Cannot move node'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
public function makeHoleFromIndex() |
66
|
|
|
{ |
67
|
6 |
|
return $this->getTargetNode()->getRight(); |
68
|
|
|
} |
69
|
|
|
|
70
|
9 |
|
public function canMoveBranch() |
71
|
|
|
{ |
72
|
9 |
|
if (false == parent::canMoveBranch()) { |
|
|
|
|
73
|
3 |
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
return ($this->getTargetNode()->isRoot()) ? false : true; |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
public function isSourceNodeAtRequiredPosition() |
80
|
|
|
{ |
81
|
6 |
|
$sourceNode = $this->getSourceNode(); |
82
|
6 |
|
$targetNode = $this->getTargetNode(); |
83
|
|
|
|
84
|
6 |
|
return ($targetNode->getRight() == ($sourceNode->getLeft() - 1) && |
85
|
6 |
|
$targetNode->getParentId() == $sourceNode->getParentId()) ? |
86
|
6 |
|
true : false; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|