Skip to content

Commit

Permalink
Merge pull request #9 from dc-ag/mb/fix-add-remove-move-bugs
Browse files Browse the repository at this point in the history
Mb/fix add remove move bugs
  • Loading branch information
MBauerDC committed Jan 28, 2022
2 parents 8eec162 + 2e1c54f commit af2a673
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 183 deletions.
22 changes: 14 additions & 8 deletions src/GenericSortableTreeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,26 @@ public function removeChildWithSorting(SortableTreeNode $childToRemove, ?array &
{
$changedNodes = null === $changedNodes ? [] : $changedNodes;
$sortingChildToRemove = $childToRemove->getPerLevelSorting();

ksort($this->childrenWithSorting);
$nodeToRemoveId = $childToRemove->getId();

/** @var SortableTreeNode $childWithSorting */
$newChildArray = [];

foreach ($this->childrenWithSorting as $sorting => $childWithSorting) {
$nodeId = $childWithSorting->getId();
$currentSorting = $sorting;
if ($childWithSorting === $childToRemove) {
unset($this->childrenWithSorting[$sorting]);
} elseif ($currentSorting > $sortingChildToRemove) {
unset($this->childrenWithSorting[$currentSorting]);
$this->childrenWithSorting[($currentSorting - 1)] = $childWithSorting;
$changedNodes[] = $childWithSorting;
if ($nodeId === $nodeToRemoveId) {
//Do nothing
} elseif ($currentSorting < $sortingChildToRemove) {
$newChildArray[$currentSorting] = $childWithSorting;
}elseif ($currentSorting > $sortingChildToRemove) {
$newChildArray[($currentSorting - 1)] = $childWithSorting;
$changedNodes[$nodeId] = $childWithSorting;
$changedNodeSorting = $childWithSorting->getPerLevelSorting();
}
}
ksort($newChildArray);
$this->childrenWithSorting = $newChildArray;

parent::removeChild($childToRemove);
}
Expand Down
14 changes: 11 additions & 3 deletions src/GenericTreeNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace TreeNodes;

use DynCom\dc\utils\AggregateTreeNode\AggregateTreeNode;

class GenericTreeNodeVisitor implements TreeNodeVisitor
{
/** @var callable */
Expand All @@ -29,11 +31,17 @@ public function visitPreOrder(?SortableTreeNode $node, bool $atRoot = true): voi
if (null === $node) {
return;
}
$nodeId = $node->getId();
$leftmostChild = null;
$leftmostChildId = null;
if ($node->getNoOfChildrenWithSorting() > 0) {
$leftmostChild = reset($node->getChildrenWithSorting());
while ($leftmostChild->getLeftSibling() !== null) {
$leftmostChild = $leftmostChild->getLeftSibling();
$children = $node->getChildrenWithSorting();
$leftmostChild = reset($children);
$currLeftSibling = $leftmostChild->getLeftSibling();
while ($currLeftSibling !== null) {
$leftmostChild = $currLeftSibling;
$leftmostChildId = $leftmostChild->getId();
$currLeftSibling = $leftmostChild->getLeftSibling();
}
}
($this->visitorCallable)($node);
Expand Down
46 changes: 34 additions & 12 deletions src/GenericTypedPayloadSortableTreeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,63 @@ class GenericTypedPayloadSortableTreeNode extends GenericTypedPayloadTreeNode im
*/
public function addChildWithSorting(SortableTreeNode $child, ?int $sorting = null): void
{
if ($sorting !== null && !array_key_exists($sorting, $this->childrenWithSorting)) {
$childId = $child->getId();
$parentId = $this->getId();
$children = $this->childrenWithSorting;
if ($sorting !== null && !array_key_exists($sorting, $children)) {
$newSorting = $sorting;
$this->childrenWithSorting[$newSorting] = $child;
} else {
$currentHighestSorting = 0;
if ($this->getNoOfChildrenWithSorting() > 0) {
$currentHighestSorting = max(array_keys($this->childrenWithSorting));
$currentHighestSorting = max(array_keys($children));
}
$newSorting = $currentHighestSorting + 1;
$this->childrenWithSorting[$newSorting] = $child;
}
parent::addChild($child);
}

public function removeAllChildren(): void
{
$this->children = [];
$this->childrenWithSorting = [];
}

/**
* @param SortableTreeNode $childToRemove
*/
public function removeChildWithSorting(SortableTreeNode $childToRemove, ?array &$changedNodes = null): void
{
$changedNodes = null === $changedNodes ? [] : $changedNodes;
$sortingChildToRemove = $childToRemove->getPerLevelSorting();

ksort($this->childrenWithSorting);

/** @var SortableTreeNode $childWithSorting */
$nodeToRemoveId = $childToRemove->getId();
$newChildArray = [];
$locallyChangedNodes = [];
foreach ($this->childrenWithSorting as $sorting => $childWithSorting) {
$nodeId = $childWithSorting->getId();
$currentSorting = $sorting;
if ($childWithSorting === $childToRemove) {
unset($this->childrenWithSorting[$sorting]);
} elseif ($currentSorting > $sortingChildToRemove) {
unset($this->childrenWithSorting[$currentSorting]);
$this->childrenWithSorting[($currentSorting - 1)] = $childWithSorting;
$changedNodes[] = $childWithSorting;
if ($nodeId === $nodeToRemoveId) {
//Do nothing
} elseif ($currentSorting < $sortingChildToRemove) {
$newChildArray[$currentSorting] = $childWithSorting;
}elseif ($currentSorting > $sortingChildToRemove) {
$newSorting = $currentSorting - 1;
$newChildArray[$newSorting] = $childWithSorting;
$changedNodes[$nodeId] = $childWithSorting;
$id = $childWithSorting->getId();
$locallyChangedNodes[$id]['node'] = $childWithSorting;
$locallyChangedNodes[$id]['targetSorting'] = $newSorting;
$locallyChangedNodes[$id]['originalSorting'] = $sorting;
}
}
ksort($newChildArray);
$this->childrenWithSorting = $newChildArray;
foreach ($locallyChangedNodes as $id => $changedNodeArr) {
$changedNode = $changedNodeArr['node'];
$targetSorting = $changedNodeArr['targetSorting'];
$originalSorting = $changedNodeArr['originalSorting'];
}

parent::removeChild($childToRemove);
}
Expand Down
Loading

0 comments on commit af2a673

Please sign in to comment.