forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllocationTransformer.php
More file actions
81 lines (70 loc) · 2.17 KB
/
AllocationTransformer.php
File metadata and controls
81 lines (70 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class AllocationTransformer extends BaseTransformer
{
/**
* Relationships that can be loaded onto allocation transformations.
*
* @var array
*/
protected $availableIncludes = ['node', 'server'];
/**
* Return the resource name for the JSONAPI output.
*
* @return string
*/
public function getResourceName(): string
{
return Allocation::RESOURCE_NAME;
}
/**
* Return a generic transformed allocation array.
*
* @param \Pterodactyl\Models\Allocation $allocation
* @return array
*/
public function transform(Allocation $allocation)
{
return [
'id' => $allocation->id,
'ip' => $allocation->ip,
'alias' => $allocation->ip_alias,
'port' => $allocation->port,
'assigned' => ! is_null($allocation->server_id),
];
}
/**
* Load the node relationship onto a given transformation.
*
* @param \Pterodactyl\Models\Allocation $allocation
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeNode(Allocation $allocation)
{
if (! $this->authorize(AdminAcl::RESOURCE_NODES)) {
return $this->null();
}
$allocation->loadMissing('node');
return $this->item(
$allocation->getRelation('node'), $this->makeTransformer(NodeTransformer::class), 'node'
);
}
/**
* Load the server relationship onto a given transformation.
*
* @param \Pterodactyl\Models\Allocation $allocation
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeServer(Allocation $allocation)
{
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$allocation->loadMissing('server');
return $this->item(
$allocation->getRelation('server'), $this->makeTransformer(ServerTransformer::class), 'server'
);
}
}