protected function copyNodesRelationsAndReplace(INode $source, INode $destination) { foreach ($source->getChildren() as $index => $child) { /* @var $child INode */ $destination->addChild($child, $index); } $parent = $source->getParent(); if ($parent instanceof INode && $parent !== $source) { $parent->addChild($destination, $parent->getChildIndex($source)); } }
/** * Replaces a node with a new one. Copies all its relationships and resets its child index if it has a parent. * @internal * * * @param INode $source * @param mixed $data data for the node * @param string $hierarchy the hierarchy member string value * @return INode */ protected function replaceNode(INode $source, $data, $hierarchy) { /* @var $destination INode */ $destination = $this->createNode($data); foreach ($source->getChildren() as $index => $child) { $destination->addChild($child, $index); } $parent = $source->getParent(); if ($parent instanceof INode && $parent !== $source) { $parent->removeChild($parent->getChildIndex($source)); $parent->addChild($destination, $this->getChildIndex($hierarchy, $destination)); } return $destination; }