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

78 lines
2.2 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Location;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class LocationTransformer extends BaseTransformer
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $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.
*/
public function transform(Location $model): array
{
2018-03-04 22:30:16 +00:00
return [
'id' => $model->id,
'short' => $model->short,
'long' => $model->long,
$model->getUpdatedAtColumn() => $this->formatTimestamp($model->updated_at),
$model->getCreatedAtColumn() => $this->formatTimestamp($model->created_at),
2018-03-04 22:30:16 +00:00
];
}
/**
* Return the nodes associated with this location.
*
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
2018-03-04 22:30:16 +00:00
*
2021-01-24 01:17:35 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2018-03-04 22:30:16 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServers(Location $location)
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$location->loadMissing('servers');
return $this->collection($location->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
}
/**
* Return the nodes associated with this location.
*
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
2018-03-04 22:30:16 +00:00
*
2021-01-24 01:17:35 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2018-03-04 22:30:16 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
2021-01-24 01:17:35 +00:00
*/
public function includeNodes(Location $location)
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
return $this->null();
}
$location->loadMissing('nodes');
return $this->collection($location->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), 'node');
}
}