1
|
|
|
<?php |
2
|
|
|
namespace StefanoTree\NestedSet\MoveStrategy; |
3
|
|
|
|
4
|
|
|
use StefanoTree\Exception; |
5
|
|
|
|
6
|
|
|
class ChildTop |
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()) { |
22
|
3 |
|
return $this->getSourceNode()->getLeft() + $this->getIndexShift(); |
23
|
3 |
|
} elseif ($this->isMovedUp()) { |
24
|
3 |
|
return $this->getSourceNode()->getLeft() + $this->getIndexShift(); |
25
|
3 |
|
} elseif ($this->isMovedDown()) { |
26
|
3 |
|
return $this->getSourceNode()->getLeft(); |
27
|
|
|
} else { |
28
|
|
|
// @codeCoverageIgnoreStart |
29
|
|
|
throw new Exception\BaseException('Cannot move node'); |
30
|
|
|
// @codeCoverageIgnoreEnd |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
public function getHoleRightIndex() |
35
|
|
|
{ |
36
|
3 |
|
if ($this->isMovedToRoot()) { |
37
|
3 |
|
return $this->getSourceNode()->getRight() + $this->getIndexShift(); |
38
|
3 |
|
} elseif ($this->isMovedUp()) { |
39
|
3 |
|
return $this->getSourceNode()->getRight() + $this->getIndexShift(); |
40
|
3 |
|
} elseif ($this->isMovedDown()) { |
41
|
3 |
|
return $this->getSourceNode()->getRight(); |
42
|
|
|
} else { |
43
|
|
|
// @codeCoverageIgnoreStart |
44
|
|
|
throw new Exception\BaseException('Cannot move node'); |
45
|
|
|
// @codeCoverageIgnoreEnd |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
public function getSourceNodeIndexShift() |
50
|
|
|
{ |
51
|
3 |
|
if ($this->isMovedToRoot()) { |
52
|
3 |
|
return $this->getTargetNode()->getLeft() - $this->getSourceNode()->getRight(); |
53
|
3 |
|
} elseif ($this->isMovedUp()) { |
54
|
3 |
|
return $this->getTargetNode()->getLeft() - $this->getSourceNode()->getRight(); |
55
|
3 |
|
} elseif ($this->isMovedDown()) { |
56
|
3 |
|
return $this->getTargetNode()->getLeft() - $this->getSourceNode()->getLeft() + 1; |
57
|
|
|
} else { |
58
|
|
|
// @codeCoverageIgnoreStart |
59
|
|
|
throw new Exception\BaseException('Cannot move node'); |
60
|
|
|
// @codeCoverageIgnoreEnd |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
public function fixHoleFromIndex() |
65
|
|
|
{ |
66
|
3 |
|
if ($this->isMovedToRoot()) { |
67
|
3 |
|
return $this->getSourceNode()->getRight(); |
68
|
3 |
|
} elseif ($this->isMovedUp()) { |
69
|
3 |
|
return $this->getSourceNode()->getRight(); |
70
|
3 |
|
} elseif ($this->isMovedDown()) { |
71
|
3 |
|
return $this->getSourceNode()->getRight(); |
72
|
|
|
} else { |
73
|
|
|
// @codeCoverageIgnoreStart |
74
|
|
|
throw new Exception\BaseException('Cannot move node'); |
75
|
|
|
// @codeCoverageIgnoreEnd |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
public function makeHoleFromIndex() |
80
|
|
|
{ |
81
|
3 |
|
return $this->getTargetNode()->getLeft(); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
public function isSourceNodeAtRequiredPosition() |
85
|
|
|
{ |
86
|
3 |
|
$sourceNode = $this->getSourceNode(); |
87
|
3 |
|
$targetNode = $this->getTargetNode(); |
88
|
|
|
|
89
|
3 |
|
return ($sourceNode->getParentId() == $targetNode->getId() && |
90
|
3 |
|
$targetNode->getLeft() == ($sourceNode->getLeft() - 1)) ? |
91
|
3 |
|
true : false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|