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

114 lines
3.8 KiB
PHP
Raw Normal View History

2018-02-10 09:57:31 +01:00
<?php
2018-05-11 10:08:34 +02:00
2018-02-10 09:57:31 +01:00
/**
2020-10-27 16:26:10 +01:00
* BillUpdateRequest.php
* Copyright (c) 2019 james@firefly-iii.org
2018-02-10 09:57:31 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-10 09:57:31 +01: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-10 09:57:31 +01:00
*
* This program is distributed in the hope that it will be useful,
2018-02-10 09:57:31 +01: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-10 09:57:31 +01: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-10 09:57:31 +01:00
*/
2018-05-11 10:08:34 +02:00
declare(strict_types=1);
2018-02-10 09:57:31 +01:00
2021-03-06 12:45:49 +01:00
namespace FireflyIII\Api\V1\Requests\Models\Bill;
2018-02-10 09:57:31 +01:00
use FireflyIII\Rules\IsBoolean;
2020-10-27 16:26:10 +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;
2018-02-18 10:31:15 +01:00
use Illuminate\Validation\Validator;
2018-02-10 09:57:31 +01:00
/**
2021-03-06 12:45:49 +01:00
* Class UpdateRequest
2019-06-09 08:26:23 +02:00
*
* @codeCoverageIgnore
2018-02-10 09:57:31 +01:00
*/
2021-03-06 12:45:49 +01:00
class UpdateRequest extends FormRequest
2018-02-10 09:57:31 +01:00
{
2020-10-27 16:26:10 +01:00
use ConvertsDataTypes, ChecksLogin;
2018-02-10 09:57:31 +01:00
/**
* Get all data from the request.
*
2018-02-10 09:57:31 +01:00
* @return array
*/
public function getAll(): array
{
2021-03-13 17:16:38 +01:00
$fields = [
'name' => ['name', 'string'],
'amount_min' => ['amount_min', 'string'],
'amount_max' => ['amount_max', 'string'],
'currency_id' => ['currency_id', 'integer'],
'currency_code' => ['currency_code', 'string'],
'date' => ['date', 'date'],
'repeat_freq' => ['repeat_freq', 'string'],
'skip' => ['skip', 'integer'],
'active' => ['active', 'boolean'],
'order' => ['order', 'integer'],
'notes' => ['notes', 'nlString'],
'object_group_id' => ['object_group_id', 'integer'],
'object_group_title' => ['object_group_title', 'string'],
2018-02-10 09:57:31 +01:00
];
2021-03-13 17:16:38 +01:00
return $this->getAllData($fields);
2018-02-10 09:57:31 +01:00
}
/**
* The rules that the incoming request must be matched against.
*
2018-02-10 09:57:31 +01:00
* @return array
*/
2018-02-16 16:42:23 +01:00
public function rules(): array
2018-02-10 09:57:31 +01:00
{
2021-03-13 17:16:38 +01:00
$bill = $this->route()->parameter('bill');
2020-10-27 16:26:10 +01:00
return [
'name' => sprintf('between:1,255|uniqueObjectForUser:bills,name,%d', $bill->id),
2020-07-06 06:55:27 +02:00
'amount_min' => 'numeric|gt:0',
'amount_max' => 'numeric|gt:0',
'currency_id' => 'numeric|exists:transaction_currencies,id',
'currency_code' => 'min:3|max:3|exists:transaction_currencies,code',
2020-07-01 15:33:06 +02:00
'date' => 'date',
'repeat_freq' => 'in:weekly,monthly,quarterly,half-year,yearly',
'skip' => 'between:0,31',
'active' => [new IsBoolean],
2018-04-15 19:20:24 +02:00
'notes' => 'between:1,65536',
2018-02-10 09:57:31 +01:00
];
}
2018-02-18 10:31:15 +01:00
/**
* Configure the validator instance.
*
* @param Validator $validator
2018-02-18 10:31:15 +01:00
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
2019-06-10 20:14:00 +02:00
static function (Validator $validator) {
2018-02-18 10:31:15 +01:00
$data = $validator->getData();
2021-03-13 17:16:38 +01:00
if (array_key_exists('amount_min', $data) && array_key_exists('amount_max', $data)) {
$min = (float)($data['amount_min'] ?? 0);
$max = (float)($data['amount_max'] ?? 0);
if ($min > $max) {
$validator->errors()->add('amount_min', (string)trans('validation.amount_min_over_max'));
}
2018-02-18 10:31:15 +01:00
}
}
);
}
2018-03-05 19:35:58 +01:00
}