misc_pterodactyl-panel/app/Transformers/Api/Application/DatabaseHostTransformer.php

67 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class DatabaseHostTransformer extends BaseTransformer
{
/**
* @var array
*/
protected $availableIncludes = [
'databases',
];
/**
* Return the resource name for the JSONAPI output.
*
* @return string
*/
public function getResourceName(): string
{
return DatabaseHost::RESOURCE_NAME;
}
/**
* Transform database host into a representation for the application API.
*
* @param \Pterodactyl\Models\DatabaseHost $model
* @return array
*/
public function transform(DatabaseHost $model)
{
return [
'id' => $model->id,
'name' => $model->name,
'host' => $model->host,
'port' => $model->port,
'username' => $model->username,
'node' => $model->node_id,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
];
}
/**
* Include the databases associated with this host.
*
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
2021-01-23 20:33:34 +00:00
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2018-05-13 16:19:35 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeDatabases(DatabaseHost $model)
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_SERVER_DATABASES)) {
return $this->null();
}
$model->loadMissing('databases');
return $this->collection($model->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), Database::RESOURCE_NAME);
}
}