Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use node key in API #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions src/NestedSetInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,41 @@ interface NestedSetInterface {
/**
* Inserts a node below the target node.
*
* @param \PNX\NestedSet\Node $target
* @param \PNX\NestedSet\NodeKey $parent
* The target node to insert below.
* @param \PNX\NestedSet\NodeKey $nodeKey
* @param \PNX\NestedSet\NodeKey $child
* The node to insert.
*
* @return \PNX\NestedSet\Node
* Returns a new node with position values set.
*/
public function addNodeBelow(Node $target, NodeKey $nodeKey);
public function addNodeBelow(NodeKey $parent, NodeKey $child);

/**
* Inserts a node before the target node.
*
* @param \PNX\NestedSet\Node $target
* @param \PNX\NestedSet\NodeKey $targetKey
* The target node to insert before.
* @param \PNX\NestedSet\NodeKey $nodeKey
* The node key to insert.
*
* @return \PNX\NestedSet\Node
* Returns a node with position values set.
*/
public function addNodeBefore(Node $target, NodeKey $nodeKey);
public function addNodeBefore(NodeKey $targetKey, NodeKey $nodeKey);

/**
* Inserts a node after the target node.
*
* @param \PNX\NestedSet\Node $target
* @param \PNX\NestedSet\NodeKey $targetKey
* The target node to insert after.
* @param \PNX\NestedSet\NodeKey $nodeKey
* The node key to insert.
*
* @return \PNX\NestedSet\Node
* Returns a node with position values set.
*/
public function addNodeAfter(Node $target, NodeKey $nodeKey);
public function addNodeAfter(NodeKey $targetKey, NodeKey $nodeKey);

/**
* Inserts a root node.
Expand All @@ -60,18 +60,18 @@ public function addRootNode(NodeKey $nodeKey);
/**
* Deletes a node and moves descendants up a level.
*
* @param \PNX\NestedSet\Node $node
* @param \PNX\NestedSet\NodeKey $nodeKey
* The node to delete.
*/
public function deleteNode(Node $node);
public function deleteNode(NodeKey $nodeKey);

/**
* Deletes a node and all it's descendants.
*
* @param \PNX\NestedSet\Node $node
* @param \PNX\NestedSet\NodeKey $nodeKey
* The node to delete.
*/
public function deleteSubTree(Node $node);
public function deleteSubTree(NodeKey $nodeKey);

/**
* Finds all descendants of a node.
Expand Down Expand Up @@ -122,7 +122,7 @@ public function findParent(NodeKey $nodeKey);
/**
* Gets a node for the ID and Revision ID.
*
* @param NodeKey $nodeKey
* @param \PNX\NestedSet\NodeKey $nodeKey
* The node key.
*
* @return \PNX\NestedSet\Node
Expand All @@ -133,50 +133,50 @@ public function getNode(NodeKey $nodeKey);
/**
* Moves a subtree to be a new root of the tree.
*
* @param \PNX\NestedSet\Node $node
* @param \PNX\NestedSet\NodeKey $nodeKey
* The node to become the new root node.
*/
public function moveSubTreeToRoot(Node $node);
public function moveSubTreeToRoot(NodeKey $nodeKey);

/**
* Moves a node and its sub-tree below the target node.
*
* @param Node $target
* @param \PNX\NestedSet\NodeKey $targetKey
* The node to move below.
* @param \PNX\NestedSet\Node $node
* @param \PNX\NestedSet\NodeKey $nodeKey
* The node to move.
*/
public function moveSubTreeBelow(Node $target, Node $node);
public function moveSubTreeBelow(NodeKey $targetKey, NodeKey $nodeKey);

/**
* Moves a node and its sub-tree before the target node.
*
* @param Node $target
* @param \PNX\NestedSet\NodeKey $targetKey
* The node to move before.
* @param \PNX\NestedSet\Node $node
* @param \PNX\NestedSet\NodeKey $nodeKey
* The node to move.
*/
public function moveSubTreeBefore(Node $target, Node $node);
public function moveSubTreeBefore(NodeKey $targetKey, NodeKey $nodeKey);

/**
* Moves a node and its sub-tree after the target node.
*
* @param Node $target
* @param \PNX\NestedSet\NodeKey $targetKey
* The node to move after.
* @param \PNX\NestedSet\Node $node
* @param \PNX\NestedSet\NodeKey $nodeKey
* The node to move.
*/
public function moveSubTreeAfter(Node $target, Node $node);
public function moveSubTreeAfter(NodeKey $targetKey, NodeKey $nodeKey);

/**
* Swaps the parent of a sub-tree to a new parent.
*
* @param \PNX\NestedSet\Node $oldParent
* @param \PNX\NestedSet\NodeKey $oldParentKey
* The old parent.
* @param \PNX\NestedSet\Node $newParent
* @param \PNX\NestedSet\NodeKey $newParentKey
* The new parent.
*/
public function adoptChildren(Node $oldParent, Node $newParent);
public function adoptChildren(NodeKey $oldParentKey, NodeKey $newParentKey);

/**
* Gets a node at a specified left position.
Expand Down
49 changes: 26 additions & 23 deletions src/Storage/DbalNestedSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ public function addRootNode(NodeKey $nodeKey) {
/**
* {@inheritdoc}
*/
public function addNodeBelow(Node $target, NodeKey $nodeKey) {
$target = $this->ensureNodeIsFresh($target);
public function addNodeBelow(NodeKey $parentKey, NodeKey $child) {
$target = $this->getNode($parentKey);
$newLeftPosition = $target->getRight();
$depth = $target->getDepth() + 1;
return $this->insertNodeAtPostion($newLeftPosition, $depth, $nodeKey);
return $this->insertNodeAtPostion($newLeftPosition, $depth, $child);
}

/**
* {@inheritdoc}
*/
public function addNodeBefore(Node $target, NodeKey $nodeKey) {
$target = $this->ensureNodeIsFresh($target);
public function addNodeBefore(NodeKey $targetKey, NodeKey $nodeKey) {
$target = $this->getNode($targetKey);
$newLeftPosition = $target->getLeft();
$depth = $target->getDepth();
return $this->insertNodeAtPostion($newLeftPosition, $depth, $nodeKey);
Expand All @@ -43,8 +43,8 @@ public function addNodeBefore(Node $target, NodeKey $nodeKey) {
/**
* {@inheritdoc}
*/
public function addNodeAfter(Node $target, NodeKey $nodeKey) {
$target = $this->ensureNodeIsFresh($target);
public function addNodeAfter(NodeKey $targetKeyKey, NodeKey $nodeKey) {
$target = $this->getNode($targetKeyKey);
$newLeftPosition = $target->getRight() + 1;
$depth = $target->getDepth();
return $this->insertNodeAtPostion($newLeftPosition, $depth, $nodeKey);
Expand Down Expand Up @@ -225,8 +225,8 @@ public function getTree() {
/**
* {@inheritdoc}
*/
public function deleteNode(Node $node) {
$node = $this->ensureNodeIsFresh($node);
public function deleteNode(NodeKey $nodeKey) {
$node = $this->getNode($nodeKey);
if ($node->getLeft() < 1 || $node->getRight() < 1) {
throw new \InvalidArgumentException("Left and right values must be > 0");
}
Expand Down Expand Up @@ -271,8 +271,8 @@ public function deleteNode(Node $node) {
/**
* {@inheritdoc}
*/
public function deleteSubTree(Node $node) {
$node = $this->ensureNodeIsFresh($node);
public function deleteSubTree(NodeKey $nodeKey) {
$node = $this->getNode($nodeKey);
$left = $node->getLeft();
$right = $node->getRight();
$width = $right - $left + 1;
Expand Down Expand Up @@ -308,44 +308,47 @@ public function deleteSubTree(Node $node) {
/**
* {@inheritdoc}
*/
public function moveSubTreeToRoot(Node $node) {
$root = $this->findRoot($node->getNodeKey());
$this->moveSubTreeBefore($root, $node);
public function moveSubTreeToRoot(NodeKey $nodeKey) {
$root = $this->findRoot($nodeKey);
$this->moveSubTreeBefore($root->getNodeKey(), $nodeKey);
}

/**
* {@inheritdoc}
*/
public function moveSubTreeBelow(Node $target, Node $node) {
$target = $this->ensureNodeIsFresh($target);
public function moveSubTreeBelow(NodeKey $targetKey, NodeKey $nodeKey) {
$target = $this->getNode($targetKey);
$node = $this->getNode($nodeKey);
$newLeftPosition = $target->getLeft() + 1;
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth() + 1);
}

/**
* {@inheritdoc}
*/
public function moveSubTreeBefore(Node $target, Node $node) {
$target = $this->ensureNodeIsFresh($target);
public function moveSubTreeBefore(NodeKey $targetKey, NodeKey $nodeKey) {
$target = $this->getNode($targetKey);
$node = $this->getNode($nodeKey);
$newLeftPosition = $target->getLeft();
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
}

/**
* {@inheritdoc}
*/
public function moveSubTreeAfter(Node $target, Node $node) {
$target = $this->ensureNodeIsFresh($target);
public function moveSubTreeAfter(NodeKey $targetKey, NodeKey $nodeKey) {
$target = $this->getNode($targetKey);
$node = $this->getNode($nodeKey);
$newLeftPosition = $target->getRight() + 1;
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
}

/**
* {@inheritdoc}
*/
public function adoptChildren(Node $oldParent, Node $newParent) {
$children = $this->findChildren($oldParent->getNodeKey());
$newParent = $this->ensureNodeIsFresh($newParent);
public function adoptChildren(NodeKey $oldParentKey, NodeKey $newParentKey) {
$children = $this->findChildren($oldParentKey);
$newParent = $this->getNode($newParentKey);
$newLeftPosition = $newParent->getRight();
$this->moveMultipleSubTreesToPosition($newLeftPosition, $children, $newParent->getDepth() + 1);
}
Expand Down
Loading