2017-01-30 21:40:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Ninja\Transformers;
|
2015-11-08 23:12:50 +02:00
|
|
|
|
|
|
|
|
use App\Models\Account;
|
2017-01-30 21:40:43 +02:00
|
|
|
use Auth;
|
2015-11-08 23:12:50 +02:00
|
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
|
|
|
|
|
|
class EntityTransformer extends TransformerAbstract
|
|
|
|
|
{
|
|
|
|
|
protected $account;
|
2015-11-27 14:55:28 +02:00
|
|
|
protected $serializer;
|
2015-11-08 23:12:50 +02:00
|
|
|
|
2015-11-27 14:55:28 +02:00
|
|
|
public function __construct(Account $account = null, $serializer = null)
|
2015-11-08 23:12:50 +02:00
|
|
|
{
|
|
|
|
|
$this->account = $account;
|
2015-11-27 14:55:28 +02: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 23:12:50 +02:00
|
|
|
}
|
2015-12-27 13:08:58 +02:00
|
|
|
|
|
|
|
|
protected function getTimestamp($date)
|
|
|
|
|
{
|
2017-06-12 21:18:20 +03:00
|
|
|
if (method_exists($date, 'getTimestamp')) {
|
|
|
|
|
return $date->getTimestamp();
|
|
|
|
|
} elseif (is_string($date)) {
|
|
|
|
|
return strtotime($date);
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-12-27 13:08:58 +02:00
|
|
|
}
|
2016-06-06 19:04:24 +03:00
|
|
|
|
2016-05-03 11:39:10 +03:00
|
|
|
public function getDefaultIncludes()
|
|
|
|
|
{
|
|
|
|
|
return $this->defaultIncludes;
|
|
|
|
|
}
|
2016-06-06 19:04:24 +03:00
|
|
|
|
2016-05-03 23:02:29 +03:00
|
|
|
protected function getDefaults($entity)
|
|
|
|
|
{
|
|
|
|
|
$data = [
|
|
|
|
|
'account_key' => $this->account->account_key,
|
2016-06-06 19:04:24 +03:00
|
|
|
'is_owner' => (bool) (Auth::check() && Auth::user()->owns($entity)),
|
2016-05-03 23:02:29 +03:00
|
|
|
];
|
2016-06-06 19:04:24 +03:00
|
|
|
|
2016-05-03 23:02:29 +03:00
|
|
|
if ($entity->relationLoaded('user')) {
|
|
|
|
|
$data['user_id'] = (int) $entity->user->public_id + 1;
|
|
|
|
|
}
|
2016-06-06 19:04:24 +03:00
|
|
|
|
2016-05-03 23:02:29 +03:00
|
|
|
return $data;
|
|
|
|
|
}
|
2015-11-08 23:12:50 +02:00
|
|
|
}
|