mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 21:22:58 +01:00
Add Transformers
This commit is contained in:
parent
8ae8300785
commit
34bbeeb146
33
app/Transformers/ArraySerializer.php
Normal file
33
app/Transformers/ArraySerializer.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
35
app/Transformers/ClientContactTransformer.php
Normal file
35
app/Transformers/ClientContactTransformer.php
Normal 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 ?: '',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
52
app/Transformers/ClientTransformer.php
Normal file
52
app/Transformers/ClientTransformer.php
Normal 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 ?: '',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user