1
|
|
|
<?php |
2
|
|
|
namespace StefanoTree\NestedSet\MoveStrategy; |
3
|
|
|
|
4
|
|
|
use StefanoTree\NestedSet\NodeInfo; |
5
|
|
|
|
6
|
|
|
abstract class MoveStrategyAbstract |
7
|
|
|
implements MoveStrategyInterface |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
protected $sourceNode; |
10
|
|
|
protected $targetNode; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param NodeInfo $sourceNode |
14
|
|
|
* @param NodeInfo $targetNode |
15
|
|
|
*/ |
16
|
18 |
|
public function __construct(NodeInfo $sourceNode, NodeInfo $targetNode) |
17
|
|
|
{ |
18
|
18 |
|
$this->sourceNode = $sourceNode; |
19
|
18 |
|
$this->targetNode = $targetNode; |
20
|
18 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return NodeInfo |
24
|
|
|
*/ |
25
|
18 |
|
protected function getSourceNode() |
26
|
|
|
{ |
27
|
18 |
|
return $this->sourceNode; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return NodeInfo |
32
|
|
|
*/ |
33
|
18 |
|
protected function getTargetNode() |
34
|
|
|
{ |
35
|
18 |
|
return $this->targetNode; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return boolean |
40
|
|
|
*/ |
41
|
12 |
|
protected function isMovedUp() |
42
|
|
|
{ |
43
|
12 |
|
return ($this->getTargetNode()->getRight() < $this->getSourceNode()->getLeft()) ? |
44
|
12 |
|
true : false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return boolean |
49
|
|
|
*/ |
50
|
15 |
|
protected function isMovedDown() |
51
|
|
|
{ |
52
|
15 |
|
return ($this->getSourceNode()->getRight() < $this->getTargetNode()->getLeft()) ? |
53
|
15 |
|
true : false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return boolean |
58
|
|
|
*/ |
59
|
15 |
|
protected function isMovedToRoot() |
60
|
|
|
{ |
61
|
15 |
|
return ($this->getSourceNode()->getLeft() > $this->getTargetNode()->getLeft() && |
62
|
15 |
|
$this->getSourceNode()->getRight() < $this->getTargetNode()->getRight()) ? |
63
|
15 |
|
true : false; |
64
|
|
|
} |
65
|
|
|
|
66
|
15 |
|
public function getIndexShift() |
67
|
|
|
{ |
68
|
15 |
|
return $this->getSourceNode()->getRight() - $this->getSourceNode()->getLeft() + 1; |
69
|
|
|
} |
70
|
|
|
|
71
|
18 |
|
public function canMoveBranch() |
72
|
|
|
{ |
73
|
18 |
|
return ($this->isTargetNodeInsideSourceBranch()) ? |
74
|
18 |
|
false : true; |
75
|
|
|
} |
76
|
|
|
|
77
|
18 |
|
protected function isTargetNodeInsideSourceBranch() |
78
|
|
|
{ |
79
|
18 |
|
$targetNode = $this->getTargetNode(); |
80
|
18 |
|
$sourceNode = $this->getSourceNode(); |
81
|
|
|
|
82
|
18 |
|
return ($targetNode->getLeft() > $sourceNode->getLeft() && |
83
|
18 |
|
$targetNode->getRight() < $sourceNode->getRight()) ? |
84
|
18 |
|
true : false; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|