misc_pterodactyl-panel/app/Transformers/Admin/AllocationTransformer.php

83 lines
2 KiB
PHP
Raw Normal View History

<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Allocation;
use League\Fractal\TransformerAbstract;
class AllocationTransformer extends TransformerAbstract
{
/**
* The filter to be applied to this transformer.
*
* @var bool|string
*/
protected $filter;
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request|bool $request
* @param bool $filter
*/
public function __construct($request = false, $filter = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
$this->filter = $filter;
}
/**
* Return a generic transformed allocation array.
*
* @return array
*/
public function transform(Allocation $allocation)
{
return $this->transformWithFilter($allocation);
}
/**
* Determine which transformer filter to apply.
*
* @return array
*/
protected function transformWithFilter(Allocation $allocation)
{
if ($this->filter === 'server') {
return $this->transformForServer($allocation);
}
return $allocation->toArray();
}
/**
* Transform the allocation to only return information not duplicated
* in the server response (discard node_id and server_id).
*
* @return array
*/
protected function transformForServer(Allocation $allocation)
{
return collect($allocation)->only('id', 'ip', 'ip_alias', 'port')->toArray();
}
}