Path: blob/1.0-develop/app/Transformers/Api/Application/AllocationTransformer.php
10280 views
<?php12namespace Pterodactyl\Transformers\Api\Application;34use Pterodactyl\Models\Node;5use Pterodactyl\Models\Server;6use League\Fractal\Resource\Item;7use Pterodactyl\Models\Allocation;8use League\Fractal\Resource\NullResource;9use Pterodactyl\Services\Acl\Api\AdminAcl;1011class AllocationTransformer extends BaseTransformer12{13/**14* Relationships that can be loaded onto allocation transformations.15*/16protected array $availableIncludes = ['node', 'server'];1718/**19* Return the resource name for the JSONAPI output.20*/21public function getResourceName(): string22{23return Allocation::RESOURCE_NAME;24}2526/**27* Return a generic transformed allocation array.28*/29public function transform(Allocation $allocation): array30{31return [32'id' => $allocation->id,33'ip' => $allocation->ip,34'alias' => $allocation->ip_alias,35'port' => $allocation->port,36'notes' => $allocation->notes,37'assigned' => !is_null($allocation->server_id),38];39}4041/**42* Load the node relationship onto a given transformation.43*44* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException45*/46public function includeNode(Allocation $allocation): Item|NullResource47{48if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {49return $this->null();50}5152return $this->item(53$allocation->node,54$this->makeTransformer(NodeTransformer::class),55Node::RESOURCE_NAME56);57}5859/**60* Load the server relationship onto a given transformation.61*62* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException63*/64public function includeServer(Allocation $allocation): Item|NullResource65{66if (!$this->authorize(AdminAcl::RESOURCE_SERVERS) || !$allocation->server) {67return $this->null();68}6970return $this->item(71$allocation->server,72$this->makeTransformer(ServerTransformer::class),73Server::RESOURCE_NAME74);75}76}777879