Skip to content

Firefly III: a personal finances manager

Expand transformers to include other objects.

James Cole9b3abd3b1978
diff --git a/app/Api/V1/Controllers/BillController.php b/app/Api/V1/Controllers/BillController.php
index c6676e6373..515a661f9c 100644
--- a/app/Api/V1/Controllers/BillController.php
+++ b/app/Api/V1/Controllers/BillController.php
@@ -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 \Illuminate\Http\Request $request
- * @param \FireflyIII\Models\Bill $bill
+ * @param BillRequest $request
+ * @param 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));
diff --git a/app/Transformers/AttachmentTransformer.php b/app/Transformers/AttachmentTransformer.php
index 86b6685b4c..dc44ce0301 100644
--- a/app/Transformers/AttachmentTransformer.php
+++ b/app/Transformers/AttachmentTransformer.php
@@ -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
*
diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php
index 80859a7ee2..6f1eb4e33f 100644
--- a/app/Transformers/BillTransformer.php
+++ b/app/Transformers/BillTransformer.php
@@ -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;
diff --git a/app/Transformers/JournalTransformer.php b/app/Transformers/JournalTransformer.php
new file mode 100644
index 0000000000..ad4cd83eb5
--- /dev/null
+++ b/app/Transformers/JournalTransformer.php
@@ -0,0 +1,88 @@
+<?php
+/**
+ * JournalTransformer.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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,
+ ],
+ ],
+ ];
+ }
+
+}
\ No newline at end of file
diff --git a/app/Transformers/TransactionTransformer.php b/app/Transformers/TransactionTransformer.php
new file mode 100644
index 0000000000..803d9e2aff
--- /dev/null
+++ b/app/Transformers/TransactionTransformer.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * TransactionTransformer.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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;
+ }
+
+}
\ No newline at end of file
diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php
new file mode 100644
index 0000000000..5e658521b3
--- /dev/null
+++ b/app/Transformers/UserTransformer.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * UserTransformer.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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,
+ ],
+ ],
+ ];
+ }
+
+}
\ No newline at end of file