Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Transformers/Api/Application/DatabaseHostTransformer.php
10284 views
1
<?php
2
3
namespace Pterodactyl\Transformers\Api\Application;
4
5
use Pterodactyl\Models\Database;
6
use Pterodactyl\Models\DatabaseHost;
7
use League\Fractal\Resource\Collection;
8
use League\Fractal\Resource\NullResource;
9
use Pterodactyl\Services\Acl\Api\AdminAcl;
10
11
class DatabaseHostTransformer extends BaseTransformer
12
{
13
protected array $availableIncludes = [
14
'databases',
15
];
16
17
/**
18
* Return the resource name for the JSONAPI output.
19
*/
20
public function getResourceName(): string
21
{
22
return DatabaseHost::RESOURCE_NAME;
23
}
24
25
/**
26
* Transform database host into a representation for the application API.
27
*/
28
public function transform(DatabaseHost $model): array
29
{
30
return [
31
'id' => $model->id,
32
'name' => $model->name,
33
'host' => $model->host,
34
'port' => $model->port,
35
'username' => $model->username,
36
'node' => $model->node_id,
37
'created_at' => $model->created_at->toAtomString(),
38
'updated_at' => $model->updated_at->toAtomString(),
39
];
40
}
41
42
/**
43
* Include the databases associated with this host.
44
*
45
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
46
*/
47
public function includeDatabases(DatabaseHost $model): Collection|NullResource
48
{
49
if (!$this->authorize(AdminAcl::RESOURCE_SERVER_DATABASES)) {
50
return $this->null();
51
}
52
53
$model->loadMissing('databases');
54
55
return $this->collection($model->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), Database::RESOURCE_NAME);
56
}
57
}
58
59