mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Expand API for attachments.
This commit is contained in:
79
app/Factory/AttachmentFactory.php
Normal file
79
app/Factory/AttachmentFactory.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* AttachmentFactory.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\Factory;
|
||||
|
||||
use FireflyIII\Models\Attachment;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\User;
|
||||
|
||||
/**
|
||||
* Class AttachmentFactory
|
||||
*/
|
||||
class AttachmentFactory
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return Attachment|null
|
||||
*/
|
||||
public function create(array $data): ?Attachment
|
||||
{
|
||||
// create attachment:
|
||||
$attachment = Attachment::create(
|
||||
[
|
||||
'user_id' => $this->user->id,
|
||||
'attachable_id' => $data['model_id'],
|
||||
'attachable_type' => $data['model'],
|
||||
'md5' => '',
|
||||
'filename' => $data['filename'],
|
||||
'title' => '' === $data['title'] ? null : $data['title'],
|
||||
'description' => null,
|
||||
'mime' => '',
|
||||
'size' => 0,
|
||||
'uploaded' => 0,
|
||||
]
|
||||
);
|
||||
$notes = (string)($data['notes'] ?? '');
|
||||
if ('' !== $notes) {
|
||||
$note = new Note;
|
||||
$note->noteable()->associate($attachment);
|
||||
$note->text = $notes;
|
||||
$note->save();
|
||||
}
|
||||
|
||||
return $attachment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user