Last active
April 23, 2021 04:32
-
-
Save adevesa/61cff0ebfefdb5a877a023d69a2efb65 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\Entities; | |
class Directory | |
{ | |
/** @var string */ | |
public $title; | |
/** @var Directory[]|null */ | |
public $children; | |
public function __construct(string $title, ?array $children) | |
{ | |
$this->title = $title; | |
$this->children = $children; | |
} | |
public function addChild(Directory $directory) | |
{ | |
$this->children[] = $directory; | |
} | |
public function toArray() | |
{ | |
if ($this->children) { | |
$this->children = array_map(function (Directory $directory) { | |
return $directory->toArray(); | |
}, $this->children); | |
} | |
return [ | |
'title' => $this->title, | |
'key' => uniqid(), | |
'children' => $this->children | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment