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()) { |
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() - 1; |
53
|
3 |
|
} elseif ($this->isMovedUp()) { |
54
|
3 |
|
return $this->getTargetNode()->getLeft() - $this->getSourceNode()->getRight() - 1; |
55
|
3 |
|
} elseif ($this->isMovedDown()) { |
56
|
3 |
|
return $this->getTargetNode()->getLeft() - $this->getSourceNode()->getLeft(); |
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() + $this->getIndexShift(); |
68
|
3 |
|
} elseif ($this->isMovedUp()) { |
69
|
3 |
|
return $this->getSourceNode()->getRight(); |
70
|
3 |
|
} elseif ($this->isMovedDown()) { |
71
|
3 |
|
return $this->getSourceNode()->getLeft(); |
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() - 1; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
public function canMoveBranch() |
85
|
|
|
{ |
86
|
3 |
|
if (false == parent::canMoveBranch()) { |
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
return ($this->getTargetNode()->isRoot()) ? |
91
|
3 |
|
false : true; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
public function isSourceNodeAtRequiredPosition() |
95
|
|
|
{ |
96
|
3 |
|
$sourceNode = $this->getSourceNode(); |
97
|
3 |
|
$targetNode = $this->getTargetNode(); |
98
|
|
|
|
99
|
3 |
|
return ($targetNode->getLeft() == ($sourceNode->getRight() + 1) && |
100
|
3 |
|
$targetNode->getParentId() == $sourceNode->getParentId()) ? |
101
|
3 |
|
true : false; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|