firefly-iii/app/Api/V1/Requests/Data/Export/ExportRequest.php

83 lines
2.5 KiB
PHP
Raw Normal View History

2021-03-03 23:28:16 -06:00
<?php
/*
* ExportRequest.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program 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 Affero General Public License for more details.
*
* 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/>.
*/
2021-03-28 04:39:26 -05:00
declare(strict_types=1);
2021-03-03 23:28:16 -06:00
2021-03-28 04:39:26 -05:00
namespace FireflyIII\Api\V1\Requests\Data\Export;
2021-03-03 23:28:16 -06:00
use Carbon\Carbon;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
/**
* Class ExportRequest
*/
class ExportRequest extends FormRequest
{
2022-10-30 08:23:00 -05:00
use ChecksLogin;
use ConvertsDataTypes;
2021-03-03 23:28:16 -06:00
public function getAll(): array
{
$result = [
2021-12-28 13:42:50 -06:00
'start' => $this->getCarbonDate('start') ?? Carbon::now()->subYear(),
'end' => $this->getCarbonDate('end') ?? Carbon::now(),
2022-05-02 12:35:35 -05:00
'type' => $this->convertString('type'),
2021-03-03 23:28:16 -06:00
];
2022-05-02 12:35:35 -05:00
$parts = explode(',', $this->convertString('accounts'));
2021-03-03 23:28:16 -06:00
$repository = app(AccountRepositoryInterface::class);
$repository->setUser(auth()->user());
2022-10-30 08:23:00 -05:00
$accounts = new Collection();
2021-03-03 23:28:16 -06:00
foreach ($parts as $part) {
2022-03-29 07:56:27 -05:00
$accountId = (int) $part;
2021-03-03 23:28:16 -06:00
if (0 !== $accountId) {
2021-06-29 23:17:38 -05:00
$account = $repository->find($accountId);
2021-03-03 23:28:16 -06:00
if (null !== $account && AccountType::ASSET === $account->accountType->type) {
$accounts->push($account);
}
}
}
$result['accounts'] = $accounts;
return $result;
}
2021-03-21 03:15:40 -05:00
/**
* The rules that the incoming request must be matched against.
*
* @return array
*/
public function rules(): array
{
return [
'type' => 'in:csv',
'accounts' => 'min:1',
'start' => 'date|before:end',
'end' => 'date|after:start',
];
}
2021-03-28 04:39:26 -05:00
}