|
1
|
|
|
<?php |
|
2
|
|
|
namespace StefanoTree\NestedSet\Validator; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use StefanoTree\Exception\InvalidArgumentException; |
|
6
|
|
|
use StefanoTree\Exception\TreeIsBrokenException; |
|
7
|
|
|
use StefanoTree\NestedSet\Adapter\AdapterInterface; |
|
8
|
|
|
use StefanoTree\NestedSet\NodeInfo; |
|
9
|
|
|
|
|
10
|
|
|
class Validator |
|
11
|
|
|
implements ValidatorInterface |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
private $adapter = null; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param AdapterInterface $adapter |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(AdapterInterface $adapter) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->adapter = $adapter; |
|
21
|
9 |
|
} |
|
22
|
|
|
|
|
23
|
9 |
|
/** |
|
24
|
9 |
|
* @return AdapterInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private function _getAdapter() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->adapter; |
|
29
|
9 |
|
} |
|
30
|
|
|
|
|
31
|
9 |
|
public function isValid($rootNodeId) |
|
32
|
|
|
{ |
|
33
|
|
|
$adapter = $this->_getAdapter(); |
|
34
|
|
|
|
|
35
|
|
|
$adapter->beginTransaction(); |
|
36
|
|
|
try { |
|
37
|
|
|
$adapter->lockTree(); |
|
38
|
|
|
|
|
39
|
9 |
|
$rootNodeInfo = $this->_getAdapter()->getNodeInfo($rootNodeId); |
|
40
|
|
|
|
|
41
|
9 |
|
$this->_checkIfNodeIsRootNode($rootNodeInfo); |
|
|
|
|
|
|
42
|
9 |
|
$this->_rebuild($rootNodeInfo, True); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
9 |
|
$adapter->commitTransaction(); |
|
45
|
|
|
} catch (TreeIsBrokenException $e) { |
|
46
|
9 |
|
$adapter->rollbackTransaction(); |
|
47
|
9 |
|
return False; |
|
48
|
|
|
} catch (Exception $e) { |
|
49
|
|
|
$adapter->rollbackTransaction(); |
|
50
|
9 |
|
throw $e; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return True; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
9 |
|
public function rebuild($rootNodeId) |
|
58
|
|
|
{ |
|
59
|
9 |
|
$adapter = $this->_getAdapter(); |
|
60
|
|
|
|
|
61
|
9 |
|
$adapter->beginTransaction(); |
|
62
|
9 |
|
try { |
|
63
|
|
|
$adapter->lockTree(); |
|
64
|
9 |
|
|
|
65
|
9 |
|
$rootNodeInfo = $this->_getAdapter()->getNodeInfo($rootNodeId); |
|
66
|
|
|
|
|
67
|
9 |
|
$this->_checkIfNodeIsRootNode($rootNodeInfo); |
|
|
|
|
|
|
68
|
6 |
|
$this->_rebuild($rootNodeInfo); |
|
|
|
|
|
|
69
|
6 |
|
|
|
70
|
|
|
$adapter->commitTransaction(); |
|
71
|
9 |
|
} catch (Exception $e) { |
|
72
|
|
|
$adapter->rollbackTransaction(); |
|
73
|
|
|
throw $e; |
|
74
|
|
|
} |
|
75
|
9 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param NodeInfo $parentNodeInfo |
|
79
|
|
|
* @param bool $onlyValidate |
|
80
|
|
|
* @param int $left |
|
81
|
|
|
* @param int $level |
|
82
|
9 |
|
* @return int|mixed |
|
83
|
|
|
* @throws TreeIsBrokenException if tree is broken and $onlyValidate is true |
|
84
|
9 |
|
*/ |
|
85
|
9 |
|
private function _rebuild(NodeInfo $parentNodeInfo, $onlyValidate = false, $left = 1, $level = 0) |
|
86
|
|
|
{ |
|
87
|
9 |
|
$adapter = $this->_getAdapter(); |
|
88
|
9 |
|
|
|
89
|
|
|
$right = $left + 1; |
|
90
|
9 |
|
|
|
91
|
9 |
|
$children = $adapter->getChildrenNodeInfo($parentNodeInfo->getId()); |
|
92
|
9 |
|
|
|
93
|
9 |
|
foreach ($children as $childNode) { |
|
94
|
|
|
$right = $this->_rebuild($childNode, $onlyValidate, $right, $level + 1); |
|
95
|
6 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ($parentNodeInfo->getLeft() != $left |
|
98
|
9 |
|
|| $parentNodeInfo->getRight() != $right |
|
99
|
6 |
|
|| $parentNodeInfo->getLevel() != $level) { |
|
100
|
6 |
|
$parentNodeInfo->setLeft($left); |
|
101
|
|
|
$parentNodeInfo->setRight($right); |
|
102
|
6 |
|
$parentNodeInfo->setLevel($level); |
|
103
|
|
|
|
|
104
|
|
|
if ($onlyValidate) { |
|
105
|
9 |
|
throw new TreeIsBrokenException(); |
|
106
|
|
|
} else { |
|
107
|
|
|
$adapter->updateNodeMetadata($parentNodeInfo); |
|
108
|
9 |
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $right + 1; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
9 |
|
* @param NodeInfo $node |
|
116
|
|
|
* @throws InvalidArgumentException |
|
117
|
9 |
|
*/ |
|
118
|
9 |
|
private function _checkIfNodeIsRootNode(NodeInfo $node) |
|
119
|
|
|
{ |
|
120
|
9 |
|
if (null != $node->getParentId()) { |
|
121
|
9 |
|
throw new InvalidArgumentException( |
|
122
|
9 |
|
sprintf('Given node id "%s" is not root id', $node->getId()) |
|
123
|
9 |
|
); |
|
124
|
9 |
|
} |
|
125
|
9 |
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|