Files
firefly-iii/app/Api/V2/Request/Chart/BalanceChartRequest.php

88 lines
2.9 KiB
PHP
Raw Normal View History

2023-08-01 09:27:39 +02:00
<?php
2023-08-01 09:27:39 +02:00
/*
* BalanceChartRequest.php
* Copyright (c) 2023 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/>.
*/
2023-09-20 06:36:43 +02:00
declare(strict_types=1);
2023-08-01 09:27:39 +02:00
namespace FireflyIII\Api\V2\Request\Chart;
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
2023-08-01 09:27:39 +02:00
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
2024-01-09 20:58:18 +01:00
use Illuminate\Support\Facades\Log;
2023-08-01 09:27:39 +02:00
use Illuminate\Validation\Validator;
2023-09-20 06:36:43 +02:00
/**
* Class BalanceChartRequest
*/
2023-08-01 09:27:39 +02:00
class BalanceChartRequest extends FormRequest
{
use ChecksLogin;
2023-11-04 14:18:49 +01:00
use ConvertsDataTypes;
use ValidatesUserGroupTrait;
2023-08-01 09:27:39 +02:00
/**
* Get all data from the request.
*/
public function getAll(): array
{
return [
'accounts' => $this->getAccountList(),
'period' => $this->convertString('period'),
2023-08-01 09:27:39 +02:00
];
}
/**
* The rules that the incoming request must be matched against.
*/
public function rules(): array
{
return [
'start' => 'required|date|after:1900-01-01|before:2099-12-31',
'end' => 'required|date|after_or_equal:start|before:2099-12-31|after:1900-01-01',
'accounts.*' => 'required|exists:accounts,id',
2023-11-04 14:18:49 +01:00
'period' => sprintf('required|in:%s', implode(',', config('firefly.valid_view_ranges'))),
2023-08-01 09:27:39 +02:00
];
}
public function withValidator(Validator $validator): void
{
$validator->after(
2023-12-21 05:07:26 +01:00
static function (Validator $validator): void {
2023-08-01 09:27:39 +02:00
// validate transaction query data.
$data = $validator->getData();
if (!array_key_exists('accounts', $data)) {
$validator->errors()->add('accounts', trans('validation.filled', ['attribute' => 'accounts']));
2023-12-20 19:35:52 +01:00
2023-08-01 09:27:39 +02:00
return;
}
if (!is_array($data['accounts'])) {
$validator->errors()->add('accounts', trans('validation.filled', ['attribute' => 'accounts']));
}
}
);
2024-01-09 20:58:18 +01:00
if($validator->fails()) {
Log::channel('audit')->error(sprintf('Validation errors in %s', __CLASS__), $validator->errors()->toArray());
}
2023-08-01 09:27:39 +02:00
}
}