/** * Adds a folder to its place in a folder tree. * @internal Its crucial that all parent folders are already in the tree or this will fail. * @param array $tree The array of top level folders to add to. {@internal Passed by reference. } * @param Folder $folder The folder to add. * @param string $shortestTree The shortest tree that in the hierarchy. * @throws FolderException Thrown if the $tree is not prepared to recieve the $folder. * @author Björn Hjortsten * @return void */ protected static function addToTree(array &$tree, Folder $folder, $shortestTree) { $rootLevel = count(array_filter(explode('-', $shortestTree))); $folderTree = explode('-', $folder->getTree()); $folderTree = array_filter($folderTree); $folderTree = Folder::arrangeKeys($folderTree); if (count($folderTree) == $rootLevel) { $tree[$folder->getId()] = $folder; } else { try { $currentFolder = $tree[intval($folderTree[$rootLevel - 1], 16)]; if ($currentFolder == null) { throw new FolderException(sprintf('Error while getting great parent folder. The great parent folder is supposed to be: %d, child folder: %d', intval($folderTree[$rootLevel - 1], 16), $folder->getId())); } for ($i = $rootLevel; $i < count($folderTree) - 1; $i++) { $currentFolder = $currentFolder->getChild(intval($folderTree[$i], 16)); } $currentFolder->addChild($folder); } catch (FolderException $fe) { if ($currentFolder instanceof Folder) { $parentId = $currentFolder->getId(); } else { $parentId = 'null'; } throw new FolderException(sprintf('Error while adding folder to tree. Current folder id: %s, child folder id: %d. Previous exception: %s', $parentId, $folder->getId(), $fe->getMessage())); } } }