2017-12-17 20:57:05 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
2017-12-17 20:57:05 +00:00
|
|
|
|
|
|
|
use Pterodactyl\Models\Location;
|
2018-01-13 02:39:15 +00:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2017-12-17 20:57:05 +00:00
|
|
|
|
2018-01-12 04:49:46 +00:00
|
|
|
class LocationTransformer extends BaseTransformer
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* List of resources that can be included.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $availableIncludes = ['nodes', 'servers'];
|
|
|
|
|
2018-01-26 03:26:06 +00:00
|
|
|
/**
|
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Location::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
/**
|
2020-09-13 18:13:37 +00:00
|
|
|
* Return a generic transformed location array.
|
2017-12-17 20:57:05 +00:00
|
|
|
*
|
2021-01-01 22:55:30 +00:00
|
|
|
* @param \Pterodactyl\Models\Location $model
|
2017-12-17 20:57:05 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2021-01-01 22:55:30 +00:00
|
|
|
public function transform(Location $model): array
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
2018-03-04 22:30:16 +00:00
|
|
|
return [
|
2021-01-01 22:55:30 +00:00
|
|
|
'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
|
|
|
];
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the nodes associated with this location.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Location $location
|
2018-01-13 02:39:15 +00:00
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
2018-03-04 22:30:16 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-12-17 20:57:05 +00:00
|
|
|
*/
|
|
|
|
public function includeServers(Location $location)
|
|
|
|
{
|
2018-01-13 02:39:15 +00:00
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
|
|
|
|
return $this->null();
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 02:39:15 +00:00
|
|
|
$location->loadMissing('servers');
|
2017-12-17 20:57:05 +00:00
|
|
|
|
2018-01-13 02:39:15 +00:00
|
|
|
return $this->collection($location->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the nodes associated with this location.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Location $location
|
2018-01-13 02:39:15 +00:00
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
2018-03-04 22:30:16 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-12-17 20:57:05 +00:00
|
|
|
*/
|
|
|
|
public function includeNodes(Location $location)
|
|
|
|
{
|
2018-01-13 02:39:15 +00:00
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_NODES)) {
|
|
|
|
return $this->null();
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 02:39:15 +00:00
|
|
|
$location->loadMissing('nodes');
|
2017-12-17 20:57:05 +00:00
|
|
|
|
2018-01-13 02:39:15 +00:00
|
|
|
return $this->collection($location->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), 'node');
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
}
|