Created
June 28, 2017 13:23
-
-
Save mstaack/28f13620c2b2c6b708d7ca685b1d2a6a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Crazy tree building | |
* | |
* @param \Illuminate\Support\Collection $folders | |
* | |
* @return array | |
*/ | |
public function createTree(\Illuminate\Support\Collection $folders) | |
{ | |
$folders = $folders->toArray(); | |
krsort($folders); | |
foreach ($folders as $folder) { | |
/** @var Folder $item */ | |
$item = array_shift($folders); | |
$delimiter = $item->getDelimiter(); | |
$a = explode($delimiter, $item->getName()); | |
array_pop($a); | |
$parent = implode($delimiter, $a); | |
if (array_key_exists($parent, $folders)) { | |
/** @var Folder[] $folders */ | |
$folders[$parent]->addChild($this->transformFolder($item)); | |
} else { | |
$folders[] = $this->transformFolder($item); | |
} | |
} | |
return collect($folders)->toArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment