1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Ninja/Transformers/EntityTransformer.php

60 lines
1.5 KiB
PHP
Raw Normal View History

2015-11-08 22:12:50 +01:00
<?php namespace App\Ninja\Transformers;
2016-05-03 22:02:29 +02:00
use Auth;
2015-11-08 22:12:50 +01:00
use App\Models\Account;
use League\Fractal\TransformerAbstract;
class EntityTransformer extends TransformerAbstract
{
protected $account;
2015-11-27 13:55:28 +01:00
protected $serializer;
2015-11-08 22:12:50 +01:00
2015-11-27 13:55:28 +01:00
public function __construct(Account $account = null, $serializer = null)
2015-11-08 22:12:50 +01:00
{
$this->account = $account;
2015-11-27 13:55:28 +01:00
$this->serializer = $serializer;
}
protected function includeCollection($data, $transformer, $entityType)
{
if ($this->serializer && $this->serializer != API_SERIALIZER_JSON) {
$entityType = null;
}
return $this->collection($data, $transformer, $entityType);
}
protected function includeItem($data, $transformer, $entityType)
{
if ($this->serializer && $this->serializer != API_SERIALIZER_JSON) {
$entityType = null;
}
return $this->item($data, $transformer, $entityType);
2015-11-08 22:12:50 +01:00
}
2015-12-27 12:08:58 +01:00
protected function getTimestamp($date)
{
return $date ? $date->getTimestamp() : null;
}
2016-05-03 10:39:10 +02:00
public function getDefaultIncludes()
{
return $this->defaultIncludes;
}
2016-05-03 22:02:29 +02:00
protected function getDefaults($entity)
{
$data = [
'account_key' => $this->account->account_key,
'is_owner' => (bool) (Auth::check() && Auth::user()->owns($entity)),
2016-05-03 22:02:29 +02:00
];
2016-05-03 22:02:29 +02:00
if ($entity->relationLoaded('user')) {
$data['user_id'] = (int) $entity->user->public_id + 1;
}
2016-05-03 22:02:29 +02:00
return $data;
}
2015-11-08 22:12:50 +01:00
}