@@ -32,27 +32,47 @@ public function __construct(Connection $connection) {
3232 /**
3333 * {@inheritdoc}
3434 */
35- public function addNode (Node $ parent , Node $ child ) {
35+ public function insertNodeBelow (Node $ target , Node $ node ) {
36+ $ newLeftPosition = $ target ->getRight () - 1 ;
37+ $ depth = $ target ->getDepth () + 1 ;
38+ return $ this ->insertNodeAtPostion ($ newLeftPosition , $ depth , $ node );
39+ }
40+
41+ /**
42+ * Inserts a node to the target position.
43+ *
44+ * @param int $newLeftPosition
45+ * The new left position.
46+ * @param int $depth
47+ * The new depth.
48+ * @param \PNX\NestedSet\Node $node
49+ * The node to insert.
50+ *
51+ * @return \PNX\NestedSet\Node
52+ * The new node with updated position.
53+ *
54+ * @throws \Exception
55+ * If a transaction error occurs.
56+ */
57+ protected function insertNodeAtPostion ($ newLeftPosition , $ depth , Node $ node ) {
3658
3759 try {
3860 $ this ->connection ->beginTransaction ();
3961
40- list ($ right , $ depth ) = $ this ->getInsertionPosition ($ parent );
41-
4262 // Move everything across two places.
4363 $ this ->connection ->executeUpdate ('UPDATE tree SET nested_right = nested_right + 2 WHERE nested_right > ? ' ,
44- [$ right ]
64+ [$ newLeftPosition ]
4565 );
4666 $ this ->connection ->executeUpdate ('UPDATE tree SET nested_left = nested_left + 2 WHERE nested_left > ? ' ,
47- [$ right ]
67+ [$ newLeftPosition ]
4868 );
4969
5070 // Create a new node object to be returned.
5171 $ newNode = new Node (
52- $ child ->getId (),
53- $ child ->getRevisionId (),
54- $ right + 1 ,
55- $ right + 2 ,
72+ $ node ->getId (),
73+ $ node ->getRevisionId (),
74+ $ newLeftPosition + 1 ,
75+ $ newLeftPosition + 2 ,
5676 $ depth
5777 );
5878
@@ -75,23 +95,6 @@ public function addNode(Node $parent, Node $child) {
7595
7696 }
7797
78- /**
79- * Finds the right-most child node.
80- *
81- * @param \PNX\NestedSet\Node $parent
82- * The parent node.
83- *
84- * @return \PNX\NestedSet\Node
85- * The right-most child node.
86- */
87- protected function findRightMostChild (Node $ parent ) {
88- $ result = $ this ->connection ->fetchAssoc ('SELECT id, revision_id, nested_left, nested_right, depth FROM tree WHERE nested_right = ? - 1 ' ,
89- [$ parent ->getRight ()]);
90- if ($ result ) {
91- return new Node ($ result ['id ' ], $ result ['revision_id ' ], $ result ['nested_left ' ], $ result ['nested_right ' ], $ result ['depth ' ]);
92- }
93- }
94-
9598 /**
9699 * {@inheritdoc}
97100 */
@@ -336,19 +339,6 @@ protected function moveSubTreeToPosition($newLeftPosition, Node $node) {
336339
337340 }
338341
339- /**
340- * Determines if this node is a 'leaf', i.e. has no children.
341- *
342- * @param \PNX\NestedSet\Node $node
343- * The node to check.
344- *
345- * @return bool
346- * TRUE if there are no children. FALSE otherwise.
347- */
348- protected function isLeaf (Node $ node ) {
349- return $ node ->getRight () - $ node ->getLeft () === 1 ;
350- }
351-
352342 /**
353343 * {@inheritdoc}
354344 */
@@ -361,31 +351,4 @@ public function getNodeAtPosition($left) {
361351 }
362352 }
363353
364- /**
365- * Gets the insertion position under the given parent.
366- *
367- * Takes into account if the parent has no children.
368- *
369- * @param \PNX\NestedSet\Node $parent
370- * The parent node.
371- *
372- * @return int[]
373- * The right and depth postiions.
374- */
375- protected function getInsertionPosition (Node $ parent ) {
376- if ($ this ->isLeaf ($ parent )) {
377- // We are on a leaf node.
378- $ right = $ parent ->getLeft ();
379- $ depth = $ parent ->getDepth () + 1 ;
380- }
381- else {
382- // Find right most child.
383- /** @var Node $rightChild */
384- $ rightChild = $ this ->findRightMostChild ($ parent );
385- $ right = $ rightChild ->getRight ();
386- $ depth = $ rightChild ->getDepth ();
387- }
388- return [$ right , $ depth ];
389- }
390-
391354}
0 commit comments