Created
April 23, 2021 04:21
-
-
Save adevesa/115127ef989081396a55d7d365ce18ed 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
<?php | |
namespace App\Helpers; | |
use Illuminate\Support\Facades\Storage; | |
class DirectoriesFormatter | |
{ | |
/** @var string */ | |
private $root; | |
public function __construct(string $root) | |
{ | |
$this->root = $root; | |
} | |
private function getDirectoryName($directory) | |
{ | |
$separator = $directory ? explode('/', $directory) : null; | |
return $separator ? $separator[count($separator)-1] : null; | |
} | |
private function getSubDirectories($directory) | |
{ | |
$subdirectorios = Storage::directories($directory); | |
$recursiveMapped = array_map(function ($directory) { | |
return new Entities\Directory($this->getDirectoryName($directory), $this->getSubDirectories($directory)); | |
}, $subdirectorios); | |
return $recursiveMapped; | |
} | |
public function listDirectories() | |
{ | |
$subdirectorios = $this->getSubDirectories($this->root); | |
$directorioRoot = new Entities\Directory($this->root, $subdirectorios); | |
return $directorioRoot->toArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment