misc_pterodactyl-panel/app/Transformers/Api/Admin/LocationTransformer.php
Dane Everitt 54b6fb5ebd
More work on the API utilizing Laravel 5.5 exception rendering
Also corrects API format to maintain JSONAPI spec
2017-12-17 14:57:05 -06:00

69 lines
1.8 KiB
PHP

<?php
namespace Pterodactyl\Transformers\Api\Admin;
use Pterodactyl\Models\Location;
use Pterodactyl\Transformers\Api\ApiTransformer;
class LocationTransformer extends ApiTransformer
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = ['nodes', 'servers'];
/**
* Return a generic transformed pack array.
*
* @param \Pterodactyl\Models\Location $location
* @return array
*/
public function transform(Location $location): array
{
return $location->toArray();
}
/**
* Return the nodes associated with this location.
*
* @param \Pterodactyl\Models\Location $location
* @return bool|\League\Fractal\Resource\Collection
*
* @throws \Pterodactyl\Exceptions\PterodactylException
*/
public function includeServers(Location $location)
{
if (! $this->authorize('server-list')) {
return false;
}
if (! $location->relationLoaded('servers')) {
$location->load('servers');
}
return $this->collection($location->getRelation('servers'), new ServerTransformer($this->getRequest()), 'server');
}
/**
* Return the nodes associated with this location.
*
* @param \Pterodactyl\Models\Location $location
* @return bool|\League\Fractal\Resource\Collection
*
* @throws \Pterodactyl\Exceptions\PterodactylException
*/
public function includeNodes(Location $location)
{
if (! $this->authorize('node-list')) {
return false;
}
if (! $location->relationLoaded('nodes')) {
$location->load('nodes');
}
return $this->collection($location->getRelation('nodes'), new NodeTransformer($this->getRequest()), 'node');
}
}