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

63 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use League\Fractal\Resource\Item;
use Pterodactyl\Models\Allocation;
use League\Fractal\Resource\NullResource;
2018-01-13 20:08:19 +00:00
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 AllocationTransformer extends Transformer
{
protected array $availableIncludes = ['node', 'server'];
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Allocation::RESOURCE_NAME;
}
/**
* Return a generic transformed allocation array.
*/
2022-12-15 00:05:46 +00:00
public function transform(Allocation $model): array
{
return [
2022-12-15 00:05:46 +00:00
'id' => $model->id,
'ip' => $model->ip,
'alias' => $model->ip_alias,
'port' => $model->port,
'notes' => $model->notes,
'server_id' => $model->server_id,
'assigned' => !is_null($model->server_id),
];
}
/**
* Load the node relationship onto a given transformation.
*/
public function includeNode(Allocation $allocation): Item|NullResource
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
2018-01-13 20:08:19 +00:00
return $this->null();
}
2022-12-15 00:05:46 +00:00
return $this->item($allocation->node, new NodeTransformer());
}
/**
* Load the server relationship onto a given transformation.
*/
public function includeServer(Allocation $allocation): Item|NullResource
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS) || !$allocation->server) {
2018-01-13 20:08:19 +00:00
return $this->null();
}
2022-12-15 00:05:46 +00:00
return $this->item($allocation->server, new ServerTransformer());
}
}