firefly-iii/app/Transformers/AttachmentTransformer.php

87 lines
2.8 KiB
PHP
Raw Normal View History

2018-02-04 08:57:35 -06:00
<?php
/**
2018-02-06 00:49:56 -06:00
* AttachmentTransformer.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
2018-02-04 08:57:35 -06:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-04 08:57:35 -06:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-02-04 08:57:35 -06:00
*
* This program is distributed in the hope that it will be useful,
2018-02-04 08:57:35 -06:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-02-04 08:57:35 -06:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-02-04 08:57:35 -06:00
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
2018-02-06 00:49:56 -06:00
use FireflyIII\Models\Attachment;
2018-09-27 00:43:30 -05:00
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
use Log;
2018-02-04 08:57:35 -06:00
/**
2018-02-06 00:49:56 -06:00
* Class AttachmentTransformer
2018-02-04 08:57:35 -06:00
*/
2018-12-16 06:55:19 -06:00
class AttachmentTransformer extends AbstractTransformer
2018-02-04 08:57:35 -06:00
{
2018-09-27 00:43:30 -05:00
/** @var AttachmentRepositoryInterface */
private $repository;
/**
* BillTransformer constructor.
*
* @codeCoverageIgnore
*/
2018-12-16 06:55:19 -06:00
public function __construct()
{
2018-09-27 00:43:30 -05:00
$this->repository = app(AttachmentRepositoryInterface::class);
if ('testing' === config('app.env')) {
2019-06-07 11:20:15 -05:00
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
}
2018-02-04 08:57:35 -06:00
/**
* Transform attachment.
*
2018-02-06 00:49:56 -06:00
* @param Attachment $attachment
2018-02-04 08:57:35 -06:00
*
* @return array
*/
2018-02-06 00:49:56 -06:00
public function transform(Attachment $attachment): array
2018-02-04 08:57:35 -06:00
{
2018-09-27 00:43:30 -05:00
$this->repository->setUser($attachment->user);
2018-02-04 08:57:35 -06:00
return [
2018-02-06 12:49:29 -06:00
'id' => (int)$attachment->id,
2018-02-11 00:46:34 -06:00
'created_at' => $attachment->created_at->toAtomString(),
2018-12-12 13:30:25 -06:00
'updated_at' => $attachment->updated_at->toAtomString(),
2018-12-04 12:36:54 -06:00
'attachable_id' => $attachment->attachable_id,
'attachable_type' => str_replace('FireflyIII\\Models\\', '', $attachment->attachable_type),
2018-02-06 12:49:29 -06:00
'md5' => $attachment->md5,
'filename' => $attachment->filename,
2018-06-23 23:51:22 -05:00
'download_uri' => route('api.v1.attachments.download', [$attachment->id]),
'upload_uri' => route('api.v1.attachments.upload', [$attachment->id]),
2018-02-06 12:49:29 -06:00
'title' => $attachment->title,
2018-09-27 00:43:30 -05:00
'notes' => $this->repository->getNoteText($attachment),
2018-12-18 23:06:01 -06:00
'mime' => $attachment->mime,
2018-06-23 23:51:22 -05:00
'size' => (int)$attachment->size,
'links' => [
[
'rel' => 'self',
'uri' => '/attachment/' . $attachment->id,
],
2018-02-09 12:11:55 -06:00
],
2018-02-04 08:57:35 -06:00
];
}
2018-02-06 00:49:56 -06:00
2018-03-05 12:35:58 -06:00
}