Expanded API code, wrote a bunch new transformers as well.

This commit is contained in:
James Cole
2018-02-11 20:45:33 +01:00
parent 94f6bd34c7
commit c2da5931ec
16 changed files with 891 additions and 97 deletions

View File

@@ -88,22 +88,19 @@ class AccountController extends Controller
*/ */
public function index(Request $request) public function index(Request $request)
{ {
$type = $request->get('type') ?? 'all'; $types = $this->mapTypes($this->parameters->get('type'));
$types = $this->mapTypes($type);
$pageSize = intval(Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data); $pageSize = intval(Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data);
$page = 0 === intval($request->get('page')) ? 1 : intval($request->get('page'));
$collection = $this->repository->getAccountsByType($types); $collection = $this->repository->getAccountsByType($types);
$count = $collection->count(); $count = $collection->count();
$accounts = $collection->slice(($page - 1) * $pageSize, $pageSize); $accounts = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator: // make paginator:
$paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $page); $paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page'));
$manager = new Manager();
$manager = new Manager(); $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl)); $manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new FractalCollection($accounts, new AccountTransformer, 'accounts'); $resource = new FractalCollection($accounts, new AccountTransformer($this->parameters), 'accounts');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return Response::json($manager->createData($resource)->toArray()); return Response::json($manager->createData($resource)->toArray());
@@ -119,10 +116,10 @@ class AccountController extends Controller
{ {
$manager = new Manager(); $manager = new Manager();
$manager->parseIncludes(['attachments', 'journals', 'user']); //$manager->parseIncludes(['attachments', 'journals', 'user']);
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl)); $manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($account, new AccountTransformer, 'accounts'); $resource = new Item($account, new AccountTransformer($this->parameters), 'accounts');
return Response::json($manager->createData($resource)->toArray()); return Response::json($manager->createData($resource)->toArray());
} }

View File

@@ -22,7 +22,6 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers; namespace FireflyIII\Api\V1\Controllers;
use Carbon\Carbon;
use FireflyIII\Api\V1\Requests\BillRequest; use FireflyIII\Api\V1\Requests\BillRequest;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
@@ -87,15 +86,7 @@ class BillController extends Controller
*/ */
public function index(Request $request) public function index(Request $request)
{ {
$pageSize = intval(Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data); $pageSize = intval(Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data);
$start = null;
$end = null;
if (null !== $request->get('start')) {
$start = new Carbon($request->get('start'));
}
if (null !== $request->get('end')) {
$end = new Carbon($request->get('end'));
}
$paginator = $this->repository->getPaginator($pageSize); $paginator = $this->repository->getPaginator($pageSize);
/** @var Collection $bills */ /** @var Collection $bills */
$bills = $paginator->getCollection(); $bills = $paginator->getCollection();
@@ -104,7 +95,7 @@ class BillController extends Controller
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl)); $manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new FractalCollection($bills, new BillTransformer($start, $end), 'bills'); $resource = new FractalCollection($bills, new BillTransformer($this->parameters), 'bills');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return Response::json($manager->createData($resource)->toArray()); return Response::json($manager->createData($resource)->toArray());
@@ -119,22 +110,12 @@ class BillController extends Controller
*/ */
public function show(Request $request, Bill $bill) public function show(Request $request, Bill $bill)
{ {
$start = null;
$end = null;
if (null !== $request->get('start')) {
$start = new Carbon($request->get('start'));
}
if (null !== $request->get('end')) {
$end = new Carbon($request->get('end'));
}
$manager = new Manager(); $manager = new Manager();
$manager->parseIncludes(['attachments', 'journals', 'user']); $manager->parseIncludes(['attachments', 'journals', 'user']);
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl)); $manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($bill, new BillTransformer($start, $end), 'bills'); $resource = new Item($bill, new BillTransformer($this->parameters), 'bills');
return Response::json($manager->createData($resource)->toArray()); return Response::json($manager->createData($resource)->toArray());
} }
@@ -146,21 +127,12 @@ class BillController extends Controller
*/ */
public function store(BillRequest $request) public function store(BillRequest $request)
{ {
$start = null;
$end = null;
if (null !== $request->get('start')) {
$start = new Carbon($request->get('start'));
}
if (null !== $request->get('end')) {
$end = new Carbon($request->get('end'));
}
$bill = $this->repository->store($request->getAll()); $bill = $this->repository->store($request->getAll());
$manager = new Manager(); $manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl)); $manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($bill, new BillTransformer($start, $end), 'bills'); $resource = new Item($bill, new BillTransformer($this->parameters), 'bills');
return Response::json($manager->createData($resource)->toArray()); return Response::json($manager->createData($resource)->toArray());
@@ -175,23 +147,13 @@ class BillController extends Controller
*/ */
public function update(BillRequest $request, Bill $bill) public function update(BillRequest $request, Bill $bill)
{ {
$data = $request->getAll(); $data = $request->getAll();
$bill = $this->repository->update($bill, $data); $bill = $this->repository->update($bill, $data);
$start = null;
$end = null;
if (null !== $request->get('start')) {
$start = new Carbon($request->get('start'));
}
if (null !== $request->get('end')) {
$end = new Carbon($request->get('end'));
}
$manager = new Manager(); $manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl)); $manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($bill, new BillTransformer($start, $end), 'bills'); $resource = new Item($bill, new BillTransformer($this->parameters), 'bills');
return Response::json($manager->createData($resource)->toArray()); return Response::json($manager->createData($resource)->toArray());

View File

@@ -22,12 +22,15 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers; namespace FireflyIII\Api\V1\Controllers;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use FireflyConfig; use FireflyConfig;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController; use Illuminate\Routing\Controller as BaseController;
use Symfony\Component\HttpFoundation\ParameterBag;
/** /**
* Class Controller. * Class Controller.
@@ -36,6 +39,9 @@ class Controller extends BaseController
{ {
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
/** @var ParameterBag */
protected $parameters;
/** /**
* Controller constructor. * Controller constructor.
* *
@@ -50,5 +56,49 @@ class Controller extends BaseController
if (true === $isDemoSite) { if (true === $isDemoSite) {
throw new FireflyException('The API is not available on the demo site.'); throw new FireflyException('The API is not available on the demo site.');
} }
// get global parameters
$this->parameters = $this->getParameters();
}
/**
* @return ParameterBag
*/
private function getParameters(): ParameterBag
{
$bag = new ParameterBag;
$page = (int)request()->get('page');
if ($page === 0) {
$page = 1;
}
$bag->set('page', $page);
$start = request()->get('start');
$startDate = null;
if (!is_null($start)) {
try {
$startDate = new Carbon($start);
} catch (InvalidDateException $e) {
// don't care
}
}
$bag->set('start', $startDate);
$end = request()->get('end');
$endDate = null;
if (!is_null($end)) {
try {
$endDate = new Carbon($end);
} catch (InvalidDateException $e) {
// don't care
}
}
$bag->set('end', $endDate);
$type = request()->get('type') ?? 'all';
$bag->set('type', $type);
return $bag;
} }
} }

View File

@@ -64,7 +64,23 @@ class Handler extends ExceptionHandler
public function render($request, Exception $exception) public function render($request, Exception $exception)
{ {
if ($exception instanceof NotFoundHttpException && $request->expectsJson()) { if ($exception instanceof NotFoundHttpException && $request->expectsJson()) {
return response()->json(['message' => 'Not found'], 404); return response()->json(['message' => 'Resource not found', 'exception' => 'NotFoundHttpException'], 404);
}
if ($request->expectsJson()) {
$isDebug = env('APP_DEBUG', false);
if ($isDebug) {
return response()->json(
[
'message' => $exception->getMessage(),
'exception' => get_class($exception),
'line' => $exception->getLine(),
'file' => $exception->getFile(),
'trace' => $exception->getTrace(),
], 500
);
}
return response()->json(['message' => 'Internal Firefly III Exception. See log files.', 'exception' => get_class($exception)], 500);
} }
if ($exception instanceof FireflyException || $exception instanceof ErrorException) { if ($exception instanceof FireflyException || $exception instanceof ErrorException) {

View File

@@ -27,13 +27,84 @@ namespace FireflyIII\Transformers;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\Note; use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/** /**
* Class AccountTransformer * Class AccountTransformer
*/ */
class AccountTransformer extends TransformerAbstract class AccountTransformer extends TransformerAbstract
{ {
/**
* List of resources possible to include.
*
* @var array
*/
protected $availableIncludes = ['journals', 'piggy_banks', 'user'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = ['journals', 'piggy_banks', 'user'];
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/**
* @param Account $account
*
* @return FractalCollection
*/
public function includeJournals(Account $account): FractalCollection
{
$ids = $account->transactions()->get(['transactions.transaction_journal_id'])->pluck('transaction_journal_id')->toArray();
$query = TransactionJournal::whereIn('id', $ids);
if (!is_null($this->parameters->get('end'))) {
$query->where('date', '<=', $this->parameters->get('end')->format('Y-m-d 00:00:00'));
}
if (!is_null($this->parameters->get('start'))) {
$query->where('date', '>=', $this->parameters->get('start')->format('Y-m-d 00:00:00'));
}
$journals = $query->get(['transaction_journals.*']);
return $this->collection($journals, new TransactionJournalTransformer($this->parameters), 'journals');
}
/**
* @param Account $account
*
* @return FractalCollection
*/
public function includePiggyBanks(Account $account): FractalCollection
{
$piggies = $account->piggyBanks()->get();
return $this->collection($piggies, new PiggyBankTransformer($this->parameters), 'piggy_banks');
}
/**
* @param Account $account
*
* @return Item
*/
public function includeUser(Account $account): Item
{
return $this->item($account->user, new UserTransformer($this->parameters), 'user');
}
/** /**
* @param Account $account * @param Account $account
* *
@@ -58,17 +129,22 @@ class AccountTransformer extends TransformerAbstract
$data = [ $data = [
'id' => (int)$account->id, 'id' => (int)$account->id,
'updated_at' => $account->updated_at->toAtomString(), 'updated_at' => $account->updated_at->toAtomString(),
'created_at' => $account->created_at->toAtomString(), 'created_at' => $account->created_at->toAtomString(),
'name' => $account->name, 'name' => $account->name,
'active' => intval($account->active) === 1, 'active' => intval($account->active) === 1,
'type' => $account->accountType->type, 'type' => $account->accountType->type,
'currency_id' => $currencyId, 'currency_id' => $currencyId,
'currency_code' => $currencyCode, 'currency_code' => $currencyCode,
'notes' => null, 'notes' => null,
'role' => $role, 'monthly_payment_date' => $this->getMeta($account, 'ccMonthlyPaymentDate'),
'links' => [ 'credit_card_type' => $this->getMeta($account, 'ccType'),
'account_number' => $this->getMeta($account, 'accountNumber'),
'iban' => $account->iban,
'bic' => $this->getMeta($account, 'BIC'),
'role' => $role,
'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/accounts/' . $account->id, 'uri' => '/accounts/' . $account->id,
@@ -84,4 +160,20 @@ class AccountTransformer extends TransformerAbstract
return $data; return $data;
} }
/**
* @param Account $account
* @param string $field
*
* @return null|string
*/
private function getMeta(Account $account, string $field): ?string
{
$result = $account->getMeta($field);
if (strlen($result) === 0) {
return null;
}
return $result;
}
} }

View File

@@ -27,6 +27,7 @@ namespace FireflyIII\Transformers;
use FireflyIII\Models\Attachment; use FireflyIII\Models\Attachment;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/** /**
* Class AttachmentTransformer * Class AttachmentTransformer
@@ -44,7 +45,20 @@ class AttachmentTransformer extends TransformerAbstract
* *
* @var array * @var array
*/ */
protected $defaultIncludes = []; protected $defaultIncludes = ['user'];
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/** /**
* @param Attachment $attachment * @param Attachment $attachment
@@ -53,9 +67,7 @@ class AttachmentTransformer extends TransformerAbstract
*/ */
public function includeUser(Attachment $attachment): Item public function includeUser(Attachment $attachment): Item
{ {
$user = $attachment->user()->first(); return $this->item($attachment->user, new UserTransformer($this->parameters), 'user');
return $this->item($user, new UserTransformer, 'user');
} }
/** /**

View File

@@ -31,6 +31,7 @@ use Illuminate\Support\Collection;
use League\Fractal\Resource\Collection as FractalCollection; use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/** /**
* Class BillTransformer * Class BillTransformer
@@ -49,21 +50,18 @@ class BillTransformer extends TransformerAbstract
* @var array * @var array
*/ */
protected $defaultIncludes = []; protected $defaultIncludes = [];
/** @var Carbon */
private $end = null; /** @var ParameterBag */
/** @var Carbon */ protected $parameters;
private $start = null;
/** /**
* BillTransformer constructor. * BillTransformer constructor.
* *
* @param Carbon|null $start * @param ParameterBag $parameters
* @param Carbon|null $end
*/ */
public function __construct(Carbon $start = null, Carbon $end = null) public function __construct(ParameterBag $parameters)
{ {
$this->start = $start; $this->parameters = $parameters;
$this->end = $end;
} }
/** /**
@@ -75,7 +73,7 @@ class BillTransformer extends TransformerAbstract
{ {
$attachments = $bill->attachments()->get(); $attachments = $bill->attachments()->get();
return $this->collection($attachments, new AttachmentTransformer, 'attachments'); return $this->collection($attachments, new AttachmentTransformer($this->parameters), 'attachments');
} }
/** /**
@@ -85,9 +83,16 @@ class BillTransformer extends TransformerAbstract
*/ */
public function includeJournals(Bill $bill): FractalCollection public function includeJournals(Bill $bill): FractalCollection
{ {
$journals = $bill->transactionJournals()->get(); $query = $bill->transactionJournals();
if (!is_null($this->parameters->get('end'))) {
$query->where('date', '<=', $this->parameters->get('end')->format('Y-m-d 00:00:00'));
}
if (!is_null($this->parameters->get('start'))) {
$query->where('date', '>=', $this->parameters->get('start')->format('Y-m-d 00:00:00'));
}
$journals = $query->get();
return $this->collection($journals, new TransactionJournalTransformer, 'journals'); return $this->collection($journals, new TransactionJournalTransformer($this->parameters), 'journals');
} }
/** /**
@@ -97,9 +102,7 @@ class BillTransformer extends TransformerAbstract
*/ */
public function includeUser(Bill $bill): Item public function includeUser(Bill $bill): Item
{ {
$user = $bill->user()->first(); return $this->item($bill->user, new UserTransformer($this->parameters), 'users');
return $this->item($user, new UserTransformer, 'users');
} }
/** /**
@@ -196,7 +199,7 @@ class BillTransformer extends TransformerAbstract
*/ */
protected function paidData(Bill $bill): array protected function paidData(Bill $bill): array
{ {
if (is_null($this->start) || is_null($this->end)) { if (is_null($this->parameters->get('start')) || is_null($this->parameters->get('end'))) {
return [ return [
'paid_dates' => [], 'paid_dates' => [],
'next_expected_match' => null, 'next_expected_match' => null,
@@ -206,7 +209,7 @@ class BillTransformer extends TransformerAbstract
/** @var BillRepositoryInterface $repository */ /** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class); $repository = app(BillRepositoryInterface::class);
$repository->setUser($bill->user); $repository->setUser($bill->user);
$set = $repository->getPaidDatesInRange($bill, $this->start, $this->end); $set = $repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end'));
$simple = $set->map( $simple = $set->map(
function (Carbon $date) { function (Carbon $date) {
return $date->format('Y-m-d'); return $date->format('Y-m-d');
@@ -214,7 +217,7 @@ class BillTransformer extends TransformerAbstract
); );
// calculate next expected match: // calculate next expected match:
$lastPaidDate = $this->lastPaidDate($set, $this->start); $lastPaidDate = $this->lastPaidDate($set, $this->parameters->get('start'));
$nextMatch = clone $bill->date; $nextMatch = clone $bill->date;
while ($nextMatch < $lastPaidDate) { while ($nextMatch < $lastPaidDate) {
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip); $nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
@@ -238,15 +241,15 @@ class BillTransformer extends TransformerAbstract
*/ */
protected function payDates(Bill $bill): array protected function payDates(Bill $bill): array
{ {
if (is_null($this->start) || is_null($this->end)) { if (is_null($this->parameters->get('start')) || is_null($this->parameters->get('end'))) {
return []; return [];
} }
$set = new Collection; $set = new Collection;
$currentStart = clone $this->start; $currentStart = clone $this->parameters->get('start');
while ($currentStart <= $this->end) { while ($currentStart <= $this->parameters->get('end')) {
$nextExpectedMatch = $this->nextDateMatch($bill, $currentStart); $nextExpectedMatch = $this->nextDateMatch($bill, $currentStart);
// If nextExpectedMatch is after end, we continue: // If nextExpectedMatch is after end, we continue:
if ($nextExpectedMatch > $this->end) { if ($nextExpectedMatch > $this->parameters->get('end')) {
break; break;
} }
// add to set // add to set

View File

@@ -0,0 +1,85 @@
<?php
/**
* BudgetTransformer.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\Budget;
use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class BudgetTransformer
*/
class BudgetTransformer extends TransformerAbstract
{
/**
* List of resources possible to include
*
* @var array
*/
protected $availableIncludes = ['user'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = [];
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/**
* @param Budget $budget
*
* @return array
*/
public function transform(Budget $budget): array
{
$data = [
'id' => (int)$budget->id,
'updated_at' => $budget->updated_at->toAtomString(),
'created_at' => $budget->created_at->toAtomString(),
'name' => $budget->name,
'links' => [
[
'rel' => 'self',
'uri' => '/budgets/' . $budget->id,
],
],
];
return $data;
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* CategoryTransformer.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\Category;
use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class CategoryTransformer
*/
class CategoryTransformer extends TransformerAbstract
{
/**
* List of resources possible to include
*
* @var array
*/
protected $availableIncludes = ['user'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = [];
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/**
* @param Category $category
*
* @return array
*/
public function transform(Category $category): array
{
$data = [
'id' => (int)$category->id,
'updated_at' => $category->updated_at->toAtomString(),
'created_at' => $category->created_at->toAtomString(),
'name' => $category->name,
'links' => [
[
'rel' => 'self',
'uri' => '/categories/' . $category->id,
],
],
];
return $data;
}
}

View File

@@ -0,0 +1,87 @@
<?php
/**
* JournalMetaTransformer.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\TransactionJournalMeta;
use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class JournalMetaTransformer
*/
class JournalMetaTransformer extends TransformerAbstract
{
/**
* List of resources possible to include
*
* @var array
*/
protected $availableIncludes = ['journal'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = [];
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/**
* @param TransactionJournalMeta $meta
*
* @return array
*/
public function transform(TransactionJournalMeta $meta): array
{
$data = [
'id' => (int)$meta->id,
'updated_at' => $meta->updated_at->toAtomString(),
'created_at' => $meta->created_at->toAtomString(),
'name' => $meta->name,
'data' => $meta->data,
'hash' => $meta->hash,
'links' => [
[
'rel' => 'self',
'uri' => '/journal_meta/' . $meta->id,
],
],
];
return $data;
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* PiggyBankEventTransformer.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\PiggyBankEvent;
use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class PiggyBankEventTransformer
*/
class PiggyBankEventTransformer extends TransformerAbstract
{
/**
* List of resources possible to include
*
* @var array
*/
protected $availableIncludes = ['piggy_bank', 'journal'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = [];
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/**
* @param PiggyBankEvent $event
*
* @return array
*/
public function transform(PiggyBankEvent $event): array
{
$data = [
'id' => (int)$event->id,
'updated_at' => $event->updated_at->toAtomString(),
'created_at' => $event->created_at->toAtomString(),
'amount' => $event->amount,
'links' => [
[
'rel' => 'self',
'uri' => '/piggy_bank_events/' . $event->id,
],
],
];
return $data;
}
}

View File

@@ -0,0 +1,92 @@
<?php
/**
* PiggyBankTransformer.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\Note;
use FireflyIII\Models\PiggyBank;
use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class PiggyBankTransformer
*/
class PiggyBankTransformer extends TransformerAbstract
{
/**
* List of resources possible to include
*
* @var array
*/
protected $availableIncludes = ['account', 'user', 'piggy_bank_events'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = [];
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/**
* @param PiggyBank $piggyBank
*
* @return array
*/
public function transform(PiggyBank $piggyBank): array
{
$data = [
'id' => (int)$piggyBank->id,
'updated_at' => $piggyBank->updated_at->toAtomString(),
'created_at' => $piggyBank->created_at->toAtomString(),
'name' => $piggyBank->name,
'notes' => null,
'links' => [
[
'rel' => 'self',
'uri' => '/piggy_banks/' . $piggyBank->id,
],
],
];
/** @var Note $note */
$note = $piggyBank->notes()->first();
if (!is_null($note)) {
$data['notes'] = $note->text;
}
return $data;
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* TagTransformer.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\Tag;
use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class TagTransformer
*/
class TagTransformer extends TransformerAbstract
{
/**
* List of resources possible to include
*
* @var array
*/
protected $availableIncludes = ['user'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = [];
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/**
* @param Tag $tag
*
* @return array
*/
public function transform(Tag $tag): array
{
$data = [
'id' => (int)$tag->id,
'updated_at' => $tag->updated_at->toAtomString(),
'created_at' => $tag->created_at->toAtomString(),
'tag' => $tag->tag,
'links' => [
[
'rel' => 'self',
'uri' => '/tags/' . $tag->id,
],
],
];
return $data;
}
}

View File

@@ -27,7 +27,9 @@ namespace FireflyIII\Transformers;
use FireflyIII\Models\Note; use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use League\Fractal\Resource\Collection as FractalCollection; use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/** /**
* Class TransactionJournalTransformer * Class TransactionJournalTransformer
@@ -40,13 +42,117 @@ class TransactionJournalTransformer extends TransformerAbstract
* *
* @var array * @var array
*/ */
protected $availableIncludes = ['attachments', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'meta', 'piggy_bank_events']; protected $availableIncludes = ['attachments', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'journal_meta', 'piggy_bank_events'];
/** /**
* List of resources to automatically include * List of resources to automatically include
* *
* @var array * @var array
*/ */
protected $defaultIncludes = ['transactions']; protected $defaultIncludes = ['attachments', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'journal_meta', 'piggy_bank_events'];
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/**
* @param TransactionJournal $journal
*
* @return FractalCollection
*/
public function includeAttachments(TransactionJournal $journal): FractalCollection
{
return $this->collection($journal->attachments, new AttachmentTransformer($this->parameters), 'attachments');
}
/**
* @param TransactionJournal $journal
*
* @return Item|null
*/
public function includeBill(TransactionJournal $journal): ?Item
{
$bill = $journal->bill()->first();
if (!is_null($bill)) {
return $this->item($bill, new BillTransformer($this->parameters), 'bills');
}
return null;
}
/**
* @param TransactionJournal $journal
*
* @return Item|null
*/
public function includeBudget(TransactionJournal $journal): ?Item
{
$budget = $journal->budgets()->first();
if (!is_null($budget)) {
return $this->item($budget, new BudgetTransformer($this->parameters), 'budgets');
}
return null;
}
/**
* @param TransactionJournal $journal
*
* @return Item|null
*/
public function includeCategory(TransactionJournal $journal): ?Item
{
$category = $journal->categories()->first();
if (!is_null($category)) {
return $this->item($category, new CategoryTransformer($this->parameters), 'categories');
}
return null;
}
/**
* @param TransactionJournal $journal
*
* @return FractalCollection
*/
public function includeJournalMeta(TransactionJournal $journal): FractalCollection
{
$meta = $journal->transactionJournalMeta()->get();
return $this->collection($meta, new JournalMetaTransformer($this->parameters), 'journal_meta');
}
/**
* @param TransactionJournal $journal
*
* @return FractalCollection
*/
public function includePiggyBankEvents(TransactionJournal $journal): FractalCollection
{
$events = $journal->piggyBankEvents()->get();
return $this->collection($events, new PiggyBankEventTransformer($this->parameters), 'piggy_bank_events');
}
/**
* @param TransactionJournal $journal
*
* @return FractalCollection
*/
public function includeTags(TransactionJournal $journal): FractalCollection
{
$set = $journal->tags;
return $this->collection($set, new TagTransformer($this->parameters), 'tag');
}
/** /**
* @param TransactionJournal $journal * @param TransactionJournal $journal
@@ -57,9 +163,18 @@ class TransactionJournalTransformer extends TransformerAbstract
{ {
$set = $journal->transactions()->where('amount', '<', 0)->get(['transactions.*']); $set = $journal->transactions()->where('amount', '<', 0)->get(['transactions.*']);
return $this->collection($set, new TransactionTransformer, 'transactions'); return $this->collection($set, new TransactionTransformer($this->parameters), 'transactions');
} }
/**
* @param TransactionJournal $journal
*
* @return \League\Fractal\Resource\Item
*/
public function includeUser(TransactionJournal $journal): Item
{
return $this->item($journal->user, new UserTransformer($this->parameters), 'users');
}
/** /**
* @param TransactionJournal $journal * @param TransactionJournal $journal

View File

@@ -26,12 +26,26 @@ namespace FireflyIII\Transformers;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/** /**
* Class TransactionTransformer * Class TransactionTransformer
*/ */
class TransactionTransformer extends TransformerAbstract class TransactionTransformer extends TransformerAbstract
{ {
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/** /**
* @param Transaction $transaction * @param Transaction $transaction
* *

View File

@@ -26,12 +26,26 @@ namespace FireflyIII\Transformers;
use FireflyIII\User; use FireflyIII\User;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/** /**
* Class UserTransformer * Class UserTransformer
*/ */
class UserTransformer extends TransformerAbstract class UserTransformer extends TransformerAbstract
{ {
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/** /**
* @param User $user * @param User $user
* *