forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocationTransformer.php
More file actions
70 lines (59 loc) · 2.05 KB
/
LocationTransformer.php
File metadata and controls
70 lines (59 loc) · 2.05 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
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Location;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class LocationTransformer extends BaseTransformer
{
/**
* List of resources that can be included.
*/
protected array $availableIncludes = ['nodes', 'servers'];
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Location::RESOURCE_NAME;
}
/**
* Return a generic transformed location array.
*/
public function transform(Location $location): array
{
return [
'id' => $location->id,
'short' => $location->short,
'long' => $location->long,
$location->getUpdatedAtColumn() => $this->formatTimestamp($location->updated_at),
$location->getCreatedAtColumn() => $this->formatTimestamp($location->created_at),
];
}
/**
* Return the nodes associated with this location.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServers(Location $location): Collection|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$location->loadMissing('servers');
return $this->collection($location->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
}
/**
* Return the nodes associated with this location.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeNodes(Location $location): Collection|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
return $this->null();
}
$location->loadMissing('nodes');
return $this->collection($location->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), 'node');
}
}