Files
firefly-iii/app/Api/V1/Requests/Models/BudgetLimit/UpdateRequest.php

101 lines
3.0 KiB
PHP
Raw Normal View History

2018-06-24 13:20:29 +02:00
<?php
/**
2021-03-06 08:40:51 +01:00
* BudgetLimitUpdateRequest.php
* Copyright (c) 2019 james@firefly-iii.org
2018-06-24 13:20:29 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-24 13:20:29 +02: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-06-24 13:20:29 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-06-24 13:20:29 +02: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-06-24 13:20:29 +02: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-06-24 13:20:29 +02:00
*/
declare(strict_types=1);
2021-03-06 08:40:51 +01:00
namespace FireflyIII\Api\V1\Requests\Models\BudgetLimit;
2018-06-24 13:20:29 +02:00
2021-03-13 20:01:42 +01:00
use Carbon\Carbon;
2020-11-08 13:31:54 +01:00
use FireflyIII\Support\Request\ChecksLogin;
2020-07-18 08:34:00 +02:00
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
2021-03-13 20:01:42 +01:00
use Illuminate\Validation\Validator;
2018-06-24 13:20:29 +02:00
/**
2021-03-06 08:40:51 +01:00
* Class UpdateRequest
*
2018-06-24 13:20:29 +02:00
*/
2021-03-06 08:40:51 +01:00
class UpdateRequest extends FormRequest
2018-06-24 13:20:29 +02:00
{
2022-10-30 14:23:00 +01:00
use ConvertsDataTypes;
use ChecksLogin;
2018-06-24 13:20:29 +02:00
/**
* Get all data from the request.
*
2018-06-24 13:20:29 +02:00
* @return array
*/
public function getAll(): array
{
2021-03-13 20:01:42 +01:00
$fields = [
'start' => ['start', 'date'],
'end' => ['end', 'date'],
2022-05-04 05:53:47 +02:00
'amount' => ['amount', 'convertString'],
2022-10-01 05:29:42 +02:00
'currency_id' => ['currency_id', 'convertInteger'],
2022-05-04 05:53:47 +02:00
'currency_code' => ['currency_code', 'convertString'],
2018-06-24 13:20:29 +02:00
];
2021-03-13 20:01:42 +01:00
return $this->getAllData($fields);
2018-06-24 13:20:29 +02:00
}
/**
* The rules that the incoming request must be matched against.
*
2018-06-24 13:20:29 +02:00
* @return array
*/
public function rules(): array
{
2020-11-08 13:31:54 +01:00
return [
2021-03-13 20:01:42 +01:00
'start' => 'date',
'end' => 'date',
2021-03-06 08:40:51 +01:00
'amount' => 'gt:0',
2018-12-09 08:45:53 +01:00
'currency_id' => 'numeric|exists:transaction_currencies,id',
2023-04-26 06:17:04 +02:00
'currency_code' => 'min:3|max:51|exists:transaction_currencies,code',
2018-06-24 13:20:29 +02:00
];
}
2021-03-13 20:01:42 +01:00
/**
* Configure the validator instance with special rules for after the basic validation rules.
*
2022-12-29 19:41:57 +01:00
* @param Validator $validator
2022-10-30 11:43:17 +01:00
* TODO duplicate code
2021-03-13 20:01:42 +01:00
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
// validate start before end only if both are there.
$data = $validator->getData();
if (array_key_exists('start', $data) && array_key_exists('end', $data)) {
$start = new Carbon($data['start']);
$end = new Carbon($data['end']);
if ($end->isBefore($start)) {
2022-12-29 19:41:57 +01:00
$validator->errors()->add('end', (string)trans('validation.date_after'));
2021-03-13 20:01:42 +01:00
}
}
}
);
}
}