Add all of the potential transformers that might be needed for now.
This commit is contained in:
parent
65630bdcce
commit
51204b8d9d
12 changed files with 642 additions and 9 deletions
|
@ -30,12 +30,55 @@ use League\Fractal\TransformerAbstract;
|
|||
class AllocationTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* Return a generic transformed server array.
|
||||
* The filter to be applied to this transformer.
|
||||
*
|
||||
* @var bool|string
|
||||
*/
|
||||
protected $filter;
|
||||
|
||||
/**
|
||||
* Transformer constructor.
|
||||
*
|
||||
* @param bool|string $filter
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($filter = false)
|
||||
{
|
||||
$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();
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue