Expand transformers.

This commit is contained in:
James Cole 2018-02-06 19:49:29 +01:00
parent da91645ec0
commit e94043edc2
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 100 additions and 15 deletions

View File

@ -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,
];
}

View File

@ -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 '';
}
}

View File

@ -0,0 +1,54 @@
<?php
/**
* NoteTransformer.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 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),
];
}
}