-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
419 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
/** | ||
* A helper class for high-level tree operations. | ||
* | ||
* This contrasts with the low-level interface. | ||
* This contrasts with the low-level interface of movable nodes: | ||
* @see MovableNodeContract | ||
* | ||
* @author Andrej Rypak <[email protected]> | ||
|
@@ -115,6 +115,35 @@ public static function unlinkChildren( | |
$parent->removeChildren(); | ||
} | ||
|
||
/** | ||
* | ||
* Usage of <=> (spaceship) operator to compare based on path props: | ||
* fn(Node $a, Node $b) => $a->data()->path <=> $b->data()->path | ||
* | ||
*/ | ||
public static function reindexTree( | ||
MovableNodeContract $node, | ||
?callable $key, | ||
?callable $sort, | ||
): void { | ||
$children = Seed::array($node->children()); | ||
$node->removeChildren(); | ||
if (null !== $sort) { | ||
uasort($children, $sort); | ||
} | ||
$seq = 0; | ||
foreach ($children as $childKey => $child) { | ||
if (!$child instanceof MovableNodeContract) { | ||
// TODO improve exceptions | ||
throw new Exception('Child not movable.'); | ||
} | ||
$newKey = null !== $key ? $key($child, $childKey, $seq) : $childKey; | ||
$node->addChild($child, $newKey); | ||
self::reindexTree($child, $key, $sort); | ||
$seq += 1; | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
|
Oops, something went wrong.