1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Add Transformers

This commit is contained in:
David Bomba 2019-03-29 08:35:35 +11:00
parent 8ae8300785
commit 34bbeeb146
3 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace App\Transformers;
use League\Fractal\Serializer\ArraySerializer as FractalArraySerializer;
/**
* Class ArraySerializer.
*/
class ArraySerializer extends FractalArraySerializer
{
/**
* @param string $resourceKey
* @param array $data
*
* @return array
*/
public function collection($resourceKey, array $data)
{
return $data;
}
/**
* @param string $resourceKey
* @param array $data
*
* @return array
*/
public function item($resourceKey, array $data)
{
return $data;
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Transformers;
use App\Models\ClientContact;
/**
* Class ContactTransformer.
*
* @SWG\Definition(definition="ClientContact", @SWG\Xml(name="ClientContact"))
*/
class ClientContactTransformer extends EntityTransformer
{
/**
* @param ClientContact $contact
*
* @return array
*
*/
public function transform(ClientContact $contact)
{
return [
'id' => (int) $contact->public_id,
'first_name' => $contact->first_name ?: '',
'last_name' => $contact->last_name ?: '',
'email' => $contact->email ?: '',
'updated_at' => $contact->updated_at,
'archived_at' => $contact->deleted_at,
'is_primary' => (bool) $contact->is_primary,
'phone' => $contact->phone ?: '',
'custom_value1' => $contact->custom_value1 ?: '',
'custom_value2' => $contact->custom_value2 ?: '',
];
}
}

View File

@ -0,0 +1,52 @@
<?php
namespace App\Transformers;
use App\Models\Client;
use App\Models\ClientContact;
/**
* @SWG\Definition(definition="Client", @SWG\Xml(name="Client"))
*/
class ClientTransformer extends EntityTransformer
{
/**
* @SWG\Property(property="id", type="integer", example=1, readOnly=true)
*/
protected $defaultIncludes = [
'contacts',
];
/**
* @var array
*/
protected $availableIncludes = [
];
/**
* @param Client $client
*
* @return \League\Fractal\Resource\Collection
*/
public function includeContacts(Client $client)
{
$transformer = new ClientContactTransformer($this->serializer);
return $this->includeCollection($client->contacts, $transformer, ClientContact::class);
}
/**
* @param Client $client
*
* @return array
*/
public function transform(Client $client)
{
return [
'id' => (int) $client->id,
'name' => $client->name ?: '',
];
}
}