Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Transformers/Api/Application/NestTransformer.php
10279 views
1
<?php
2
3
namespace Pterodactyl\Transformers\Api\Application;
4
5
use Pterodactyl\Models\Egg;
6
use Pterodactyl\Models\Nest;
7
use Pterodactyl\Models\Server;
8
use League\Fractal\Resource\Collection;
9
use League\Fractal\Resource\NullResource;
10
use Pterodactyl\Services\Acl\Api\AdminAcl;
11
12
class NestTransformer extends BaseTransformer
13
{
14
/**
15
* Relationships that can be loaded onto this transformation.
16
*/
17
protected array $availableIncludes = [
18
'eggs', 'servers',
19
];
20
21
/**
22
* Return the resource name for the JSONAPI output.
23
*/
24
public function getResourceName(): string
25
{
26
return Nest::RESOURCE_NAME;
27
}
28
29
/**
30
* Transform a Nest model into a representation that can be consumed by the
31
* application API.
32
*/
33
public function transform(Nest $model): array
34
{
35
$response = $model->toArray();
36
37
$response[$model->getUpdatedAtColumn()] = $this->formatTimestamp($model->updated_at);
38
$response[$model->getCreatedAtColumn()] = $this->formatTimestamp($model->created_at);
39
40
return $response;
41
}
42
43
/**
44
* Include the Eggs relationship on the given Nest model transformation.
45
*
46
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
47
*/
48
public function includeEggs(Nest $model): Collection|NullResource
49
{
50
if (!$this->authorize(AdminAcl::RESOURCE_EGGS)) {
51
return $this->null();
52
}
53
54
$model->loadMissing('eggs');
55
56
return $this->collection($model->getRelation('eggs'), $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME);
57
}
58
59
/**
60
* Include the servers relationship on the given Nest model.
61
*
62
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
63
*/
64
public function includeServers(Nest $model): Collection|NullResource
65
{
66
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
67
return $this->null();
68
}
69
70
$model->loadMissing('servers');
71
72
return $this->collection($model->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), Server::RESOURCE_NAME);
73
}
74
}
75
76