1 | <?php |
||
27 | class NestedSet implements TreeInterface |
||
28 | { |
||
29 | private $adapter; |
||
30 | |||
31 | private $validator; |
||
32 | |||
33 | /** |
||
34 | * @param Options|array $options |
||
35 | * @param object $dbAdapter |
||
36 | * |
||
37 | * @return TreeInterface |
||
38 | * |
||
39 | * @throws InvalidArgumentException |
||
40 | */ |
||
41 | 7 | public static function factory($options, $dbAdapter): TreeInterface |
|
64 | |||
65 | /** |
||
66 | * @param AdapterInterface $adapter |
||
67 | */ |
||
68 | 62 | public function __construct(AdapterInterface $adapter) |
|
72 | |||
73 | /** |
||
74 | * @return AdapterInterface |
||
75 | */ |
||
76 | 62 | public function getAdapter(): AdapterInterface |
|
80 | |||
81 | /** |
||
82 | * @return ValidatorInterface |
||
83 | */ |
||
84 | 8 | private function getValidator(): ValidatorInterface |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 5 | public function createRootNode($data = array(), $scope = null) |
|
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | 2 | public function updateNode($nodeId, array $data): void |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 9 | public function addNode($targetNodeId, array $data = array(), string $placement = self::PLACEMENT_CHILD_TOP) |
|
126 | { |
||
127 | 9 | return $this->getAddStrategy($placement)->add($targetNodeId, $data); |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param string $placement |
||
132 | * |
||
133 | * @return AddStrategyInterface |
||
134 | * |
||
135 | * @throws InvalidArgumentException |
||
136 | */ |
||
137 | 9 | protected function getAddStrategy(string $placement): AddStrategyInterface |
|
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 13 | public function moveNode($sourceNodeId, $targetNodeId, string $placement = self::PLACEMENT_CHILD_TOP): void |
|
159 | { |
||
160 | 13 | $this->getMoveStrategy($placement)->move($sourceNodeId, $targetNodeId); |
|
161 | 5 | } |
|
162 | |||
163 | /** |
||
164 | * @param string $placement |
||
165 | * |
||
166 | * @return MoveStrategyInterface |
||
167 | * |
||
168 | * @throws InvalidArgumentException |
||
169 | */ |
||
170 | 13 | protected function getMoveStrategy(string $placement): MoveStrategyInterface |
|
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | 3 | public function deleteBranch($nodeId): void |
|
192 | { |
||
193 | 3 | $adapter = $this->getAdapter(); |
|
194 | |||
195 | 3 | $adapter->beginTransaction(); |
|
196 | try { |
||
197 | 3 | $adapter->lockTree(); |
|
198 | |||
199 | 3 | $nodeInfo = $adapter->getNodeInfo($nodeId); |
|
200 | |||
201 | // node does not exist |
||
202 | 3 | if (!$nodeInfo) { |
|
203 | 1 | $adapter->commitTransaction(); |
|
204 | |||
205 | 1 | return; |
|
206 | } |
||
207 | |||
208 | 2 | $adapter->delete($nodeInfo->getId()); |
|
209 | |||
210 | //patch hole |
||
211 | 2 | $moveFromIndex = $nodeInfo->getLeft(); |
|
212 | 2 | $shift = $nodeInfo->getLeft() - $nodeInfo->getRight() - 1; |
|
213 | 2 | $adapter->moveLeftIndexes($moveFromIndex, $shift, $nodeInfo->getScope()); |
|
214 | 2 | $adapter->moveRightIndexes($moveFromIndex, $shift, $nodeInfo->getScope()); |
|
215 | |||
216 | 2 | $adapter->commitTransaction(); |
|
217 | } catch (Exception $e) { |
||
218 | $adapter->rollbackTransaction(); |
||
219 | |||
220 | throw $e; |
||
221 | } |
||
222 | 2 | } |
|
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | 3 | public function getNode($nodeId): ?array |
|
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | 4 | public function getAncestorsQueryBuilder(): AncestorQueryBuilderInterface |
|
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | 7 | public function getDescendantsQueryBuilder(): DescendantQueryBuilderInterface |
|
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | 7 | public function getRootNode($scope = null): array |
|
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | 1 | public function getRoots(): array |
|
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | 5 | public function isValid($rootNodeId): bool |
|
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | 3 | public function rebuild($rootNodeId): void |
|
284 | } |
||
285 |