Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Transformers/Api/Application/LocationTransformer.php
10279 views
1
<?php
2
3
namespace Pterodactyl\Transformers\Api\Application;
4
5
use Pterodactyl\Models\Location;
6
use League\Fractal\Resource\Collection;
7
use League\Fractal\Resource\NullResource;
8
use Pterodactyl\Services\Acl\Api\AdminAcl;
9
10
class LocationTransformer extends BaseTransformer
11
{
12
/**
13
* List of resources that can be included.
14
*/
15
protected array $availableIncludes = ['nodes', 'servers'];
16
17
/**
18
* Return the resource name for the JSONAPI output.
19
*/
20
public function getResourceName(): string
21
{
22
return Location::RESOURCE_NAME;
23
}
24
25
/**
26
* Return a generic transformed location array.
27
*/
28
public function transform(Location $location): array
29
{
30
return [
31
'id' => $location->id,
32
'short' => $location->short,
33
'long' => $location->long,
34
$location->getUpdatedAtColumn() => $this->formatTimestamp($location->updated_at),
35
$location->getCreatedAtColumn() => $this->formatTimestamp($location->created_at),
36
];
37
}
38
39
/**
40
* Return the nodes associated with this location.
41
*
42
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
43
*/
44
public function includeServers(Location $location): Collection|NullResource
45
{
46
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
47
return $this->null();
48
}
49
50
$location->loadMissing('servers');
51
52
return $this->collection($location->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
53
}
54
55
/**
56
* Return the nodes associated with this location.
57
*
58
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
59
*/
60
public function includeNodes(Location $location): Collection|NullResource
61
{
62
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
63
return $this->null();
64
}
65
66
$location->loadMissing('nodes');
67
68
return $this->collection($location->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), 'node');
69
}
70
}
71
72