1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 00:11:35 +02:00
invoiceninja/app/Ninja/Transformers/EntityTransformer.php

68 lines
1.6 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Ninja\Transformers;
2015-11-08 22:12:50 +01:00
use App\Models\Account;
2017-01-30 20:40:43 +01:00
use Auth;
2015-11-08 22:12:50 +01:00
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)
{
2017-06-12 20:18:20 +02:00
if (method_exists($date, 'getTimestamp')) {
return $date->getTimestamp();
} elseif (is_string($date)) {
return strtotime($date);
} else {
return null;
}
2015-12-27 12:08:58 +01:00
}
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
}