From e94043edc2141ef359b55da96e4a4f4b69bbbe24 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 6 Feb 2018 19:49:29 +0100 Subject: [PATCH] Expand transformers. --- app/Transformers/AttachmentTransformer.php | 10 +++- app/Transformers/BillTransformer.php | 51 ++++++++++++++------ app/Transformers/NoteTransformer.php | 54 ++++++++++++++++++++++ 3 files changed, 100 insertions(+), 15 deletions(-) create mode 100644 app/Transformers/NoteTransformer.php 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 @@ +. + */ + +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