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

64 lines
1.5 KiB
PHP
Raw Normal View History

2019-03-28 03:36:36 +01:00
<?php
namespace App\Ninja\Transformers;
use Auth;
use League\Fractal\TransformerAbstract;
class EntityTransformer extends TransformerAbstract
{
protected $serializer;
public function __construct($serializer = null)
{
$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);
}
protected function getTimestamp($date)
{
if (method_exists($date, 'getTimestamp')) {
return $date->getTimestamp();
} elseif (is_string($date)) {
return strtotime($date);
} else {
return null;
}
}
public function getDefaultIncludes()
{
return $this->defaultIncludes;
}
protected function getDefaults($entity)
{
$data = [
'is_owner' => (bool) (Auth::check() && Auth::user()->owns($entity)),
];
if ($entity->relationLoaded('user')) {
$data['user_id'] = (int) $entity->user->public_id + 1;
}
return $data;
}
}