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