public function move(NodeInterface $node, NodeInterface $targetNode, $position)
{
if (!in_array($position, array('before', 'into', 'after'), true)) {
throw new NodeException('The position should be one of the following: "before", "into", "after".', 1296132542);
}
$designatedParentNode = $this->getDesignatedParentNode($targetNode, $position);
// If we stay inside the same parent we basically just reorder, no rename needed or wanted.
if ($designatedParentNode !== $node->getParent()) {
$designatedNodePath = NodePaths::addNodePathSegment($designatedParentNode->getPath(), $node->getName());
if ($this->nodeService->nodePathAvailableForNode($designatedNodePath, $node) === false) {
$nodeName = $this->nodeService->generateUniqueNodeName($designatedParentNode->getPath(), $node->getName());
if ($nodeName !== $node->getName()) {
// FIXME: This can be removed if $node->move* supports additionally changing the name of the node.
$node->setName($nodeName);
}
}
}
switch ($position) {
case 'before':
$node->moveBefore($targetNode);
break;
case 'into':
$node->moveInto($targetNode);
break;
case 'after':
$node->moveAfter($targetNode);
}
return $node;
}