misc_pterodactyl-panel/app/Transformers/Api/Application/LocationTransformer.php

64 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Location;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
2022-12-15 00:05:46 +00:00
use Pterodactyl\Transformers\Api\Transformer;
2022-12-15 00:05:46 +00:00
class LocationTransformer extends Transformer
{
/**
* List of resources that can be included.
*/
protected array $availableIncludes = ['nodes', 'servers'];
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Location::RESOURCE_NAME;
}
/**
* Return a generic transformed location array.
*/
2022-12-15 00:05:46 +00:00
public function transform(Location $model): array
{
2018-03-04 22:30:16 +00:00
return [
2022-12-15 00:05:46 +00:00
'id' => $model->id,
'short' => $model->short,
'long' => $model->long,
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
2018-03-04 22:30:16 +00:00
];
}
/**
* Return the nodes associated with this location.
*/
2022-12-15 00:05:46 +00:00
public function includeNodes(Location $location): Collection|NullResource
{
2022-12-15 00:05:46 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
return $this->null();
}
2022-12-15 00:05:46 +00:00
return $this->collection($location->nodes, new NodeTransformer());
}
/**
* Return the nodes associated with this location.
*/
2022-12-15 00:05:46 +00:00
public function includeServers(Location $location): Collection|NullResource
{
2022-12-15 00:05:46 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
2022-12-15 00:05:46 +00:00
return $this->collection($location->servers, new ServerTransformer());
}
}