1
|
|
|
<?php |
2
|
|
|
namespace StefanoTree\NestedSet\MoveStrategy; |
3
|
|
|
|
4
|
|
|
use StefanoTree\Exception; |
5
|
|
|
|
6
|
|
|
class ChildBottom |
7
|
|
|
extends MoveStrategyAbstract |
|
|
|
|
8
|
|
|
{ |
9
|
3 |
|
public function getNewParentId() |
10
|
|
|
{ |
11
|
3 |
|
return $this->getTargetNode()->getId(); |
12
|
|
|
} |
13
|
|
|
|
14
|
3 |
|
public function getLevelShift() |
15
|
|
|
{ |
16
|
3 |
|
return $this->getTargetNode()->getLevel() - $this->getSourceNode()->getLevel() + 1; |
17
|
|
|
} |
18
|
|
|
|
19
|
3 |
|
public function getHoleLeftIndex() |
20
|
|
|
{ |
21
|
3 |
|
if ($this->isMovedToRoot() || $this->isMovedDown()) { |
22
|
3 |
|
return $this->getSourceNode()->getLeft(); |
23
|
3 |
|
} elseif ($this->isMovedUp()) { |
24
|
3 |
|
return $this->getSourceNode()->getLeft() + $this->getIndexShift(); |
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->isMovedDown()) { |
33
|
3 |
|
return $this->getSourceNode()->getRight(); |
34
|
3 |
|
} elseif ($this->isMovedUp()) { |
35
|
3 |
|
return $this->getSourceNode()->getRight() + $this->getIndexShift(); |
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->isMovedDown()) { |
44
|
3 |
|
return $this->getTargetNode()->getRight() - $this->getSourceNode()->getLeft(); |
45
|
3 |
|
} elseif ($this->isMovedUp()) { |
46
|
3 |
|
return $this->getTargetNode()->getRight() - $this->getSourceNode()->getRight() - 1; |
47
|
|
|
} else { |
48
|
|
|
throw new Exception\BaseException('Cannot move node'); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
public function fixHoleFromIndex() |
53
|
|
|
{ |
54
|
3 |
|
return $this->getSourceNode()->getRight(); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
public function makeHoleFromIndex() |
58
|
|
|
{ |
59
|
3 |
|
return $this->getTargetNode()->getRight() - 1; |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
public function isSourceNodeAtRequiredPosition() |
63
|
|
|
{ |
64
|
3 |
|
$sourceNode = $this->getSourceNode(); |
65
|
3 |
|
$targetNode = $this->getTargetNode(); |
66
|
|
|
|
67
|
3 |
|
return ($sourceNode->getParentId() == $targetNode->getId() && |
68
|
3 |
|
$sourceNode->getRight() == ($targetNode->getRight() - 1)) ? |
69
|
3 |
|
true : false; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|