Skip to content

Commit

Permalink
Merge pull request #8 from dc-ag/mb/get-changed-nodes
Browse files Browse the repository at this point in the history
fill optional by-ref param for changed nodes
  • Loading branch information
MBauerDC committed Jan 25, 2022
2 parents bcae472 + a3197a0 commit 15169f5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 42 deletions.
11 changes: 7 additions & 4 deletions src/GenericSortableTreeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ class GenericSortableTreeNode extends GenericTreeNode implements SortableTreeNod
public function addChildWithSorting(SortableTreeNode $child, ?int $sorting = null): void
{
if ($sorting !== null && !array_key_exists($sorting, $this->childrenWithSorting)) {
$this->childrenWithSorting[$sorting] = $child;
$newSorting = $sorting;
$this->childrenWithSorting[$newSorting] = $child;
} else {
$currentHighestSorting = 0;
if ($this->getNoOfChildrenWithSorting() > 0) {
$currentHighestSorting = max(array_keys($this->childrenWithSorting));
}

$this->childrenWithSorting[($currentHighestSorting + 1)] = $child;
$newSorting = $currentHighestSorting + 1;
$this->childrenWithSorting[$newSorting] = $child;
}
parent::addChild($child);
}

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

ksort($this->childrenWithSorting);
Expand All @@ -47,6 +49,7 @@ public function removeChildWithSorting(SortableTreeNode $childToRemove): void
} elseif ($currentSorting > $sortingChildToRemove) {
unset($this->childrenWithSorting[$currentSorting]);
$this->childrenWithSorting[($currentSorting - 1)] = $childWithSorting;
$changedNodes[] = $childWithSorting;
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/GenericTypedPayloadSortableTreeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@ class GenericTypedPayloadSortableTreeNode extends GenericTypedPayloadTreeNode im
public function addChildWithSorting(SortableTreeNode $child, ?int $sorting = null): void
{
if ($sorting !== null && !array_key_exists($sorting, $this->childrenWithSorting)) {
$this->childrenWithSorting[$sorting] = $child;
$newSorting = $sorting;
$this->childrenWithSorting[$newSorting] = $child;
} else {
$currentHighestSorting = 0;
if ($this->getNoOfChildrenWithSorting() > 0) {
$currentHighestSorting = max(array_keys($this->childrenWithSorting));
}

$this->childrenWithSorting[($currentHighestSorting + 1)] = $child;
$newSorting = $currentHighestSorting + 1;
$this->childrenWithSorting[$newSorting] = $child;
}
parent::addChild($child);
}

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

ksort($this->childrenWithSorting);
Expand All @@ -49,6 +51,7 @@ public function removeChildWithSorting(SortableTreeNode $childToRemove): void
} elseif ($currentSorting > $sortingChildToRemove) {
unset($this->childrenWithSorting[$currentSorting]);
$this->childrenWithSorting[($currentSorting - 1)] = $childWithSorting;
$changedNodes[] = $childWithSorting;
}
}

Expand All @@ -63,10 +66,13 @@ public function removeChildWithSorting(SortableTreeNode $childToRemove): void
*/
public function addChildWithTypedPayloadAndWithSorting(TypedPayloadSortableTreeNode $child, ?int $sorting = null): void
{
$childPayload = $child->getPayload();
$childPayloadPHPType = \gettype($childPayload);
$childPayloadPHPFQDN = \is_object($childPayload) ? $childPayload::class : null;
if (parent::isTypedPayloadValid($child->getPayload(), $this->getPayloadType(), $this->getPayloadObjectFQDN())) {
$this->addChildWithSorting($child, $sorting);
} else {
throw new InvalidArgumentException("Could not add child with typed payload, type has to be [{$this->getPayloadType()}]");
throw new InvalidArgumentException("Could not add child with typed payload, type has to be [{$this->getPayloadType()}] - actual type [$childPayloadPHPType] with fqdn [$childPayloadPHPFQDN].");
}
}
}
67 changes: 34 additions & 33 deletions src/canProcessTreeNodeSortRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,47 @@ trait canProcessTreeNodeSortRequests
* @param SortableTreeNode $treeNodeToSort
* @param int $newSorting
*/
public static function processNewSortingRequest(SortableTreeNode $treeNodeToSort, int $newSorting): void
public static function processNewSortingRequest(SortableTreeNode $treeNodeToSort, int $newSorting, ?array &$changedNodes = null): void
{
$changedNodes = null === $changedNodes ? [] : $changedNodes;
$parentTreeNode = $treeNodeToSort->getParent();

if ($parentTreeNode instanceof SortableTreeNode && $parentTreeNode->getNoOfChildrenWithSorting() > 0) {
$childrenWithSorting = &$parentTreeNode->getChildrenWithSorting();
$treeNodeToSortPerLevelSorting = $treeNodeToSort->getPerLevelSorting();
$moveUpAction = $treeNodeToSortPerLevelSorting < $newSorting;
$moveDownAction = $treeNodeToSortPerLevelSorting > $newSorting;

if ($moveUpAction) {
ksort($childrenWithSorting);
} elseif ($moveDownAction) {
krsort($childrenWithSorting);
$currSorting = $treeNodeToSort->getPerLevelSorting();

if ($currSorting === $newSorting) {
return;
}
$changedNodes[] = $treeNodeToSort;
$minAffectedSorting = min($currSorting, $newSorting);
$maxAffectedSorting = max($currSorting, $newSorting);
$targetMovesUp = $newSorting > $currSorting;

unset($childrenWithSorting[$treeNodeToSortPerLevelSorting]);
$childrenWithSortingRef = &$parentTreeNode->getChildrenWithSorting();
ksort($childrenWithSortingRef);
$newChildArray = [];

/** @var SortableTreeNode $childWithSorting */
foreach ($childrenWithSorting as $sorting => $childWithSorting) {
if (
($moveDownAction &&
($sorting < $newSorting ||
$sorting > $treeNodeToSortPerLevelSorting)) ||
($moveUpAction &&
($sorting > $newSorting ||
$sorting < $treeNodeToSortPerLevelSorting))
) {
/** @var SortableTreeNode $currNode */
foreach ($childrenWithSortingRef as $sorting => $currNode) {
if ($sorting < $minAffectedSorting || $sorting > $maxAffectedSorting) {
$newChildArray[$sorting] = $currNode;
continue;
}
if ($sorting === $currSorting) {
continue;
}
$changedNodes[] = $currNode;

if ($moveDownAction) {
unset($childrenWithSorting[$sorting]);
$childrenWithSorting[($sorting + 1)] = $childWithSorting;
} elseif ($moveUpAction) {
unset($childrenWithSorting[$sorting]);
$childrenWithSorting[($sorting - 1)] = $childWithSorting;
if ($targetMovesUp) {
$newChildArray[($sorting - 1)] = $currNode;
} else {
$newChildArray[($sorting + 1)] = $currNode;
}
}

$childrenWithSorting[$newSorting] = $treeNodeToSort;
ksort($childrenWithSorting);
$newChildArray[$newSorting] = $treeNodeToSort;
ksort($newChildArray);
$childrenWithSortingRef = $newChildArray;
}
}

Expand All @@ -62,12 +61,14 @@ public static function processNewSortingRequest(SortableTreeNode $treeNodeToSort
* @param integer $newSorting
* @return void
*/
public static function processMoveRequest(SortableTreeNode $nodeToMove, SortableTreeNode $newParentNode, int $newSorting): void
public static function processMoveRequest(SortableTreeNode $nodeToMove, SortableTreeNode $newParentNode, int $newSorting, ?array &$changedNodes = null): void
{
/** @var SortableTreeNode $nodeToMoveParent */
$nodeToMoveParent = $nodeToMove->getParent();
$nodeToMoveParent->removeChildWithSorting($nodeToMove);
$newParentNode->addChildWithSorting($nodeToMove);
GenericSortableTreeNode::processNewSortingRequest($nodeToMove, $newSorting);
if ($nodeToMoveParent !== $newParentNode) {
$nodeToMoveParent->removeChildWithSorting($nodeToMove, $changedNodes);
$newParentNode->addChildWithSorting($nodeToMove);
}
GenericSortableTreeNode::processNewSortingRequest($nodeToMove, $newSorting, $changedNodes);
}
}

0 comments on commit 15169f5

Please sign in to comment.