Skip to content

Firefly III: a personal finances manager

Expand transformers.

James Colee94043edc214
diff --git a/app/Transformers/AttachmentTransformer.php b/app/Transformers/AttachmentTransformer.php
index 1d6540bdda..81c03f4b7e 100644
--- a/app/Transformers/AttachmentTransformer.php
+++ b/app/Transformers/AttachmentTransformer.php
@@ -40,7 +40,15 @@ class AttachmentTransformer extends TransformerAbstract
public function transform(Attachment $attachment): array
{
return [
- 'id' => (int)$attachment->id,
+ 'id' => (int)$attachment->id,
+ 'attachable_type' => $attachment->attachable_type,
+ 'md5' => $attachment->md5,
+ 'filename' => $attachment->filename,
+ 'title' => $attachment->title,
+ 'description' => $attachment->description,
+ 'notes' => $attachment->notes,
+ 'mime' => $attachment->mime,
+ 'size' => $attachment->size,
];
}
diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php
index 53ffb8c4d7..a7aac08745 100644
--- a/app/Transformers/BillTransformer.php
+++ b/app/Transformers/BillTransformer.php
@@ -21,11 +21,10 @@
declare(strict_types=1);
-namespace FireflyIII\Transformers\Bill;
+namespace FireflyIII\Transformers;
use Carbon\Carbon;
use FireflyIII\Models\Bill;
-use FireflyIII\Models\Note;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
use League\Fractal\TransformerAbstract;
@@ -35,6 +34,18 @@ use League\Fractal\TransformerAbstract;
*/
class BillTransformer extends TransformerAbstract
{
+ /**
+ * List of resources possible to include
+ *
+ * @var array
+ */
+ protected $availableIncludes = ['attachments', 'notes'];
+ /**
+ * List of resources to automatically include
+ *
+ * @var array
+ */
+ protected $defaultIncludes = ['notes',];
/** @var Carbon */
private $end = null;
/** @var Carbon */
@@ -52,6 +63,30 @@ class BillTransformer extends TransformerAbstract
$this->end = $end;
}
+ /**
+ * @param Bill $bill
+ *
+ * @return \League\Fractal\Resource\Collection
+ */
+ public function includeAttachments(Bill $bill)
+ {
+ $attachments = $bill->attachments()->get();
+
+ return $this->collection($attachments, new AttachmentTransformer);
+ }
+
+ /**
+ * @param Bill $bill
+ *
+ * @return \League\Fractal\Resource\Collection
+ */
+ public function includeNotes(Bill $bill)
+ {
+ $notes = $bill->notes()->get();
+
+ return $this->collection($notes, new NoteTransformer);
+ }
+
/**
* @param Bill $bill
*
@@ -75,7 +110,6 @@ class BillTransformer extends TransformerAbstract
'pay_dates' => $this->payDates($bill),
'paid_dates' => $paidData['paid_dates'],
'next_expected_match' => $paidData['next_expected_match'],
- 'notes' => $this->getNote($bill),
'links' => [
[
'rel' => 'self',
@@ -204,15 +238,4 @@ class BillTransformer extends TransformerAbstract
return $simple->toArray();
}
-
- private function getNote($bill): string
- {
- /** @var Note $note */
- $note = $bill->notes()->first();
- if (!is_null($note)) {
- return $note->text;
- }
-
- return '';
- }
}
\ No newline at end of file
diff --git a/app/Transformers/NoteTransformer.php b/app/Transformers/NoteTransformer.php
new file mode 100644
index 0000000000..e46a13d710
--- /dev/null
+++ b/app/Transformers/NoteTransformer.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * NoteTransformer.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\Note;
+use League\CommonMark\CommonMarkConverter;
+use League\Fractal\TransformerAbstract;
+
+/**
+ * Class NoteTransformer
+ */
+class NoteTransformer extends TransformerAbstract
+{
+ /**
+ * @param Note $note
+ *
+ * @return array
+ */
+ public function transform(Note $note): array
+ {
+ $converter = new CommonMarkConverter;
+
+ return [
+ 'id' => (int)$note->id,
+ 'notable_type' => $note->noteable_type,
+ 'title' => $note->title,
+ 'text' => $note->text,
+ 'markdown' => $converter->convertToHtml($note->text),
+ ];
+ }
+
+}
\ No newline at end of file