mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Expand transformers.
This commit is contained in:
parent
da91645ec0
commit
e94043edc2
@ -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,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -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 '';
|
||||
}
|
||||
}
|
54
app/Transformers/NoteTransformer.php
Normal file
54
app/Transformers/NoteTransformer.php
Normal 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),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user