Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Transformers/Api/Application/AllocationTransformer.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Transformers\Api\Application;
4
5
use Pterodactyl\Models\Node;
6
use Pterodactyl\Models\Server;
7
use League\Fractal\Resource\Item;
8
use Pterodactyl\Models\Allocation;
9
use League\Fractal\Resource\NullResource;
10
use Pterodactyl\Services\Acl\Api\AdminAcl;
11
12
class AllocationTransformer extends BaseTransformer
13
{
14
/**
15
* Relationships that can be loaded onto allocation transformations.
16
*/
17
protected array $availableIncludes = ['node', 'server'];
18
19
/**
20
* Return the resource name for the JSONAPI output.
21
*/
22
public function getResourceName(): string
23
{
24
return Allocation::RESOURCE_NAME;
25
}
26
27
/**
28
* Return a generic transformed allocation array.
29
*/
30
public function transform(Allocation $allocation): array
31
{
32
return [
33
'id' => $allocation->id,
34
'ip' => $allocation->ip,
35
'alias' => $allocation->ip_alias,
36
'port' => $allocation->port,
37
'notes' => $allocation->notes,
38
'assigned' => !is_null($allocation->server_id),
39
];
40
}
41
42
/**
43
* Load the node relationship onto a given transformation.
44
*
45
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
46
*/
47
public function includeNode(Allocation $allocation): Item|NullResource
48
{
49
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
50
return $this->null();
51
}
52
53
return $this->item(
54
$allocation->node,
55
$this->makeTransformer(NodeTransformer::class),
56
Node::RESOURCE_NAME
57
);
58
}
59
60
/**
61
* Load the server relationship onto a given transformation.
62
*
63
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
64
*/
65
public function includeServer(Allocation $allocation): Item|NullResource
66
{
67
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS) || !$allocation->server) {
68
return $this->null();
69
}
70
71
return $this->item(
72
$allocation->server,
73
$this->makeTransformer(ServerTransformer::class),
74
Server::RESOURCE_NAME
75
);
76
}
77
}
78
79