Expand transformers to include other objects.

This commit is contained in:
James Cole 2018-02-10 10:58:06 +01:00
parent db02fefcf4
commit 9b3abd3b19
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
6 changed files with 276 additions and 19 deletions

View File

@ -81,6 +81,8 @@ class BillController extends Controller
/**
* Display a listing of the resource.
*
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
@ -128,7 +130,7 @@ class BillController extends Controller
$manager = new Manager();
$manager->parseIncludes(['attachments']);
$manager->parseIncludes(['attachments', 'notes', 'transactionJournals', 'user']);
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
@ -138,11 +140,9 @@ class BillController extends Controller
}
/**
* Store a newly created resource in storage.
* @param BillRequest $request
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function store(BillRequest $request)
{
@ -158,7 +158,6 @@ class BillController extends Controller
$bill = $this->repository->store($request->getAll());
$manager = new Manager();
$manager->parseIncludes(['attachments']);
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
@ -169,12 +168,10 @@ class BillController extends Controller
}
/**
* Update the specified resource in storage.
* @param BillRequest $request
* @param Bill $bill
*
* @param \Illuminate\Http\Request $request
* @param \FireflyIII\Models\Bill $bill
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function update(BillRequest $request, Bill $bill)
{
@ -190,7 +187,6 @@ class BillController extends Controller
$end = new Carbon($request->get('end'));
}
$manager = new Manager();
$manager->parseIncludes(['attachments']);
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));

View File

@ -25,6 +25,7 @@ namespace FireflyIII\Transformers;
use FireflyIII\Models\Attachment;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;
/**
@ -32,6 +33,31 @@ use League\Fractal\TransformerAbstract;
*/
class AttachmentTransformer extends TransformerAbstract
{
/**
* List of resources possible to include
*
* @var array
*/
protected $availableIncludes = ['user'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = [];
/**
* @param Attachment $attachment
*
* @return Item
*/
public function includeUser(Attachment $attachment): Item
{
$user = $attachment->user()->first();
return $this->item($user, new UserTransformer, 'user');
}
/**
* @param Attachment $attachment
*

View File

@ -27,7 +27,9 @@ use Carbon\Carbon;
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;
use League\Fractal\Resource\Collection as FractalCollection;
/**
* Class BillTransformer
@ -39,7 +41,7 @@ class BillTransformer extends TransformerAbstract
*
* @var array
*/
protected $availableIncludes = ['attachments', 'notes'];
protected $availableIncludes = ['attachments', 'notes', 'transactionJournals', 'user'];
/**
* List of resources to automatically include
*
@ -66,9 +68,21 @@ class BillTransformer extends TransformerAbstract
/**
* @param Bill $bill
*
* @return \League\Fractal\Resource\Collection
* @return \League\Fractal\Resource\Item
*/
public function includeAttachments(Bill $bill)
public function includeUser(Bill $bill): Item
{
$user = $bill->user()->first();
return $this->item($user, new UserTransformer, 'user');
}
/**
* @param Bill $bill
*
* @return FractalCollection
*/
public function includeAttachments(Bill $bill): FractalCollection
{
$attachments = $bill->attachments()->get();
@ -78,15 +92,27 @@ class BillTransformer extends TransformerAbstract
/**
* @param Bill $bill
*
* @return \League\Fractal\Resource\Collection
* @return FractalCollection
*/
public function includeNotes(Bill $bill)
public function includeNotes(Bill $bill): FractalCollection
{
$notes = $bill->notes()->get();
return $this->collection($notes, new NoteTransformer, 'note');
}
/**
* @param Bill $bill
*
* @return FractalCollection
*/
public function includeTransactionJournals(Bill $bill): FractalCollection
{
$journals = $bill->transactionJournals()->get();
return $this->collection($journals, new JournalTransformer, 'transaction_journal');
}
/**
* @param Bill $bill
*
@ -119,8 +145,7 @@ class BillTransformer extends TransformerAbstract
],
];
// todo: attachments, journals, notes
// todo updated at, created at
return $data;

View File

@ -0,0 +1,88 @@
<?php
/**
* JournalTransformer.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\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\TransformerAbstract;
/**
* Class JournalTransformer
*/
class JournalTransformer extends TransformerAbstract
{
/**
* List of resources possible to include
*
* @var array
*/
protected $availableIncludes = ['attachments', 'notes', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'meta', 'piggy_bank_events'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = ['transactions',];
/**
* @param TransactionJournal $journal
*
* @return FractalCollection
*/
public function includeTransactions(TransactionJournal $journal): FractalCollection
{
$tasker = app(JournalTaskerInterface::class);
$tasker->setUser($journal->user);
$transactions = $tasker->getTransactionsOverview($journal);
return $this->collection($transactions, new TransactionTransformer, 'transaction');
}
/**
* @param TransactionJournal $journal
*
* @return array
*/
public function transform(TransactionJournal $journal): array
{
return [
'id' => (int)$journal->id,
'type' => $journal->transactionType->type,
'description' => $journal->description,
'date' => $journal->date->format('Y-m-d'),
'order' => $journal->order,
'completed' => $journal->completed,
'links' => [
[
'rel' => 'self',
'uri' => '/transaction_journal/' . $journal->id,
],
],
];
}
}

View File

@ -0,0 +1,68 @@
<?php
/**
* TransactionTransformer.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 League\Fractal\TransformerAbstract;
/**
* Class TransactionTransformer
*/
class TransactionTransformer extends TransformerAbstract
{
/**
* @param array $original
*
* @return array
*/
public function transform(array $original): array
{
$id = $original['source_id'];
$foreignAmount = null;
if (!is_null($original['foreign_source_amount'])) {
$foreignAmount = round($original['foreign_source_amount'], $original['foreign_currency_dp']);
}
$return = [
'id' => $id,
'amount' => round($original['source_amount'], $original['transaction_currency_dp']),
'currency_id' => $original['transaction_currency_id'],
'currency_code' => $original['transaction_currency_code'],
'foreign_amount' => $foreignAmount,
'foreign_currency_id' => $original['foreign_currency_id'],
'foreign_currency_code' => $original['foreign_currency_code'],
'description' => $original['description'],
'links' => [
[
'rel' => 'self',
'uri' => '/transaction/' . $id,
],
],
];
// todo source account, dest account, budget, category
return $return;
}
}

View File

@ -0,0 +1,54 @@
<?php
/**
* UserTransformer.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\User;
use League\Fractal\TransformerAbstract;
/**
* Class UserTransformer
*/
class UserTransformer extends TransformerAbstract
{
/**
* @param User $user
*
* @return array
*/
public function transform(User $user): array
{
return [
'id' => (int)$user->id,
'links' => [
[
'rel' => 'self',
'uri' => '/user/' . $user->id,
],
],
];
}
}