2015-11-03 20:03:24 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
use Session;
|
2015-11-03 20:03:24 +01:00
|
|
|
use Utils;
|
|
|
|
use Response;
|
2015-11-27 13:55:28 +01:00
|
|
|
use Request;
|
2015-11-03 20:03:24 +01:00
|
|
|
use League\Fractal;
|
|
|
|
use League\Fractal\Manager;
|
2015-11-08 10:43:32 +01:00
|
|
|
use League\Fractal\Resource\Item;
|
|
|
|
use League\Fractal\Resource\Collection;
|
2015-11-27 13:55:28 +01:00
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
2015-11-03 20:03:24 +01:00
|
|
|
use App\Ninja\Serializers\ArraySerializer;
|
2015-11-27 13:55:28 +01:00
|
|
|
use League\Fractal\Serializer\JsonApiSerializer;
|
2015-11-03 20:03:24 +01:00
|
|
|
|
2015-11-08 21:34:26 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Swagger(
|
|
|
|
* schemes={"http","https"},
|
|
|
|
* host="ninja.dev",
|
|
|
|
* basePath="/api/v1",
|
|
|
|
* @SWG\Info(
|
|
|
|
* version="1.0.0",
|
|
|
|
* title="Invoice Ninja API",
|
|
|
|
* description="An open-source invoicing and time-tracking app built with Laravel",
|
|
|
|
* termsOfService="",
|
|
|
|
* @SWG\Contact(
|
|
|
|
* email="contact@invoiceninja.com"
|
|
|
|
* ),
|
|
|
|
* @SWG\License(
|
|
|
|
* name="Attribution Assurance License",
|
|
|
|
* url="https://raw.githubusercontent.com/invoiceninja/invoiceninja/master/LICENSE"
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @SWG\ExternalDocumentation(
|
|
|
|
* description="Find out more about Invoice Ninja",
|
|
|
|
* url="https://www.invoiceninja.com"
|
|
|
|
* ),
|
|
|
|
* @SWG\SecurityScheme(
|
|
|
|
* securityDefinition="api_key",
|
|
|
|
* type="apiKey",
|
|
|
|
* in="header",
|
|
|
|
* name="TOKEN"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2015-11-03 20:03:24 +01:00
|
|
|
class BaseAPIController extends Controller
|
|
|
|
{
|
|
|
|
protected $manager;
|
2015-11-27 13:55:28 +01:00
|
|
|
protected $serializer;
|
2015-11-03 20:03:24 +01:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->manager = new Manager();
|
2015-11-27 13:55:28 +01:00
|
|
|
|
|
|
|
if ($include = Request::get('include')) {
|
|
|
|
$this->manager->parseIncludes($include);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->serializer = Request::get('serializer') ?: API_SERIALIZER_ARRAY;
|
|
|
|
|
|
|
|
if ($this->serializer === API_SERIALIZER_JSON) {
|
|
|
|
$this->manager->setSerializer(new JsonApiSerializer());
|
|
|
|
} else {
|
|
|
|
$this->manager->setSerializer(new ArraySerializer());
|
|
|
|
}
|
2015-11-03 20:03:24 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
protected function createItem($data, $transformer, $entityType)
|
2015-11-03 20:03:24 +01:00
|
|
|
{
|
2015-11-27 13:55:28 +01:00
|
|
|
if ($this->serializer && $this->serializer != API_SERIALIZER_JSON) {
|
|
|
|
$entityType = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$resource = new Item($data, $transformer, $entityType);
|
2015-11-08 10:43:32 +01:00
|
|
|
return $this->manager->createData($resource)->toArray();
|
|
|
|
}
|
2015-11-05 09:44:48 +01:00
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
protected function createCollection($data, $transformer, $entityType, $paginator = false)
|
2015-11-08 10:43:32 +01:00
|
|
|
{
|
2015-11-27 13:55:28 +01:00
|
|
|
if ($this->serializer && $this->serializer != API_SERIALIZER_JSON) {
|
|
|
|
$entityType = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$resource = new Collection($data, $transformer, $entityType);
|
|
|
|
|
|
|
|
if ($paginator) {
|
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
|
|
|
}
|
|
|
|
|
2015-11-08 10:43:32 +01:00
|
|
|
return $this->manager->createData($resource)->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function response($response)
|
|
|
|
{
|
2015-11-27 13:55:28 +01:00
|
|
|
$index = Request::get('index') ?: 'data';
|
|
|
|
$meta = isset($response['meta']) ? $response['meta'] : null;
|
|
|
|
$response = [
|
|
|
|
$index => $response
|
|
|
|
];
|
|
|
|
if ($meta) {
|
|
|
|
$response['meta'] = $meta;
|
|
|
|
unset($response[$index]['meta']);
|
|
|
|
}
|
|
|
|
|
2015-11-03 20:03:24 +01:00
|
|
|
$response = json_encode($response, JSON_PRETTY_PRINT);
|
|
|
|
$headers = Utils::getApiHeaders();
|
|
|
|
|
|
|
|
return Response::make($response, 200, $headers);
|
|
|
|
}
|
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
protected function getIncluded()
|
|
|
|
{
|
|
|
|
$data = ['user'];
|
|
|
|
|
|
|
|
$included = Request::get('include');
|
|
|
|
$included = explode(',', $included);
|
|
|
|
|
|
|
|
foreach ($included as $include) {
|
|
|
|
if ($include == 'invoices') {
|
|
|
|
$data[] = 'invoices.invoice_items';
|
|
|
|
$data[] = 'invoices.user';
|
|
|
|
} elseif ($include == 'clients') {
|
|
|
|
$data[] = 'clients.contacts';
|
|
|
|
$data[] = 'clients.user';
|
2016-01-06 15:23:58 +01:00
|
|
|
} elseif ($include == 'vendors') {
|
|
|
|
$data[] = 'vendors.vendorcontacts';
|
|
|
|
$data[] = 'vendors.user';
|
|
|
|
}
|
|
|
|
elseif ($include) {
|
2015-11-27 13:55:28 +01:00
|
|
|
$data[] = $include;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2015-11-03 20:03:24 +01:00
|
|
|
}
|