File: D:/HostingSpaces/Neopoints/momsecurity.be/vendor/komma/kms/src/Core/Tree/TreeResource.php
<?php
namespace Komma\KMS\Core\Tree;
use Komma\KMS\Core\AbstractTranslationModel;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailInterface;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailTrait;
use Komma\KMS\Core\Tree\NestedSets\Nodes\TreeModelInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Resources\Json\Resource;
/**
* Class TreeResource
*
* A Tree resource. Used for Converting TreeModelInterfaces to json
* Representing the models you give it in a tree fashioned way.
*
* @see TreeResourceCollection
* @package App\Kms\Core\Tree
*/
class TreeResource extends Resource
{
public function toArray($request)
{
if (!is_a($this->resource, TreeModelInterface::class)) throw new \RuntimeException('The item that you put into a '.TreeResource::class.' must be an '.TreeModelInterface::class.' instance');
$thumbnail = '';
if(is_a($this->resource,HasThumbnailInterface::class)) {
/** @var HasThumbnailTrait $resource $*/
$resource = $this->resource;
$thumbnail = $resource->getThumbnail();
}
$status = null;
if(isset($this->active)) $status = $this->active;
$data = [
'id' => $this->id,
'title' => $this->getSideBarName(),
'thumbnail' => $thumbnail,
'status' => $status,
];
//
// if (is_a($this->resource, AbstractTranslatableModel::class)) {
// /** @var $this HasRoutesInterface|TreeResource */
// $data['routes'] = $this->retrieveRoutes();
// }
$data['children'] = new TreeResourceCollection(Collection::make($this->findChildren()));
return $data;
}
/**
* Returns an array keyed by language id having route alias attributes as values
*
* @return array
*/
private function retrieveRoutes()
{
//Create empty Routes array
$routes = [];
//If there are no translations, return empty array
if (!isset($this->translations)) return $routes;
//Loop trough the translations
foreach ($this->translations as $translation) {
/** @var AbstractTranslationModel $translation */
//Get the primary route, if non existing, skip to the next language
if (!$route = $translation->route) continue;
//Set the alias based on the language id
$routes[$translation->language_id] = $route->alias;
}
//Return the array of routes
return $routes;
}
}