Skip to content

Instantly share code, notes, and snippets.

@adevesa
Last active April 23, 2021 04:32
Show Gist options
  • Save adevesa/61cff0ebfefdb5a877a023d69a2efb65 to your computer and use it in GitHub Desktop.
Save adevesa/61cff0ebfefdb5a877a023d69a2efb65 to your computer and use it in GitHub Desktop.
<?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