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

222 lines
8.2 KiB
PHP
Raw Normal View History

2018-06-29 19:27:07 +02:00
<?php
/**
* RecurrenceUpdateRequest.php
* Copyright (c) 2019 james@firefly-iii.org
2018-06-29 19:27:07 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-29 19:27:07 +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-29 19:27:07 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-06-29 19:27:07 +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-29 19:27:07 +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-29 19:27:07 +02:00
*/
declare(strict_types=1);
2021-03-07 06:15:13 +01:00
namespace FireflyIII\Api\V1\Requests\Models\Recurrence;
2018-06-29 19:27:07 +02:00
2019-08-22 17:56:48 +02:00
use FireflyIII\Models\Recurrence;
use FireflyIII\Rules\BelongsUser;
use FireflyIII\Rules\IsBoolean;
2020-11-08 13:36:13 +01:00
use FireflyIII\Support\Request\ChecksLogin;
2020-07-18 08:34:00 +02:00
use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\Support\Request\GetRecurrenceData;
2020-03-21 06:00:36 +01:00
use FireflyIII\Validation\CurrencyValidation;
2018-07-05 18:02:02 +02:00
use FireflyIII\Validation\RecurrenceValidation;
use FireflyIII\Validation\TransactionValidation;
use Illuminate\Foundation\Http\FormRequest;
2018-06-29 19:27:07 +02:00
use Illuminate\Validation\Validator;
/**
2021-03-07 06:15:13 +01:00
* Class UpdateRequest
2018-06-29 19:27:07 +02:00
*/
2021-03-07 06:15:13 +01:00
class UpdateRequest extends FormRequest
2018-06-29 19:27:07 +02:00
{
2023-11-04 14:18:49 +01:00
use ChecksLogin;
2022-10-30 14:23:00 +01:00
use ConvertsDataTypes;
use CurrencyValidation;
use GetRecurrenceData;
2023-11-04 14:18:49 +01:00
use RecurrenceValidation;
use TransactionValidation;
2020-11-08 13:36:13 +01:00
/**
* Get all data from the request.
*
* @return array
*/
public function getAll(): array
{
2021-03-12 18:31:19 +01:00
// this is the way:
$fields = [
2022-05-04 05:53:47 +02:00
'title' => ['title', 'convertString'],
'description' => ['description', 'convertString'],
2023-05-17 07:02:08 +02:00
'first_date' => ['first_date', 'convertDateTime'],
'repeat_until' => ['repeat_until', 'convertDateTime'],
2022-10-01 05:29:42 +02:00
'nr_of_repetitions' => ['nr_of_repetitions', 'convertInteger'],
2021-03-12 18:31:19 +01:00
'apply_rules' => ['apply_rules', 'boolean'],
'active' => ['active', 'boolean'],
2022-05-04 05:53:47 +02:00
'notes' => ['notes', 'convertString'],
2021-03-12 18:31:19 +01:00
];
$reps = $this->getRepetitionData();
$transactions = $this->getTransactionData();
$return = [
'recurrence' => $this->getAllData($fields),
];
if (null !== $reps) {
$return['repetitions'] = $reps;
}
2021-03-12 18:31:19 +01:00
if (null !== $transactions) {
$return['transactions'] = $transactions;
}
2020-03-15 08:16:16 +01:00
2021-03-12 18:31:19 +01:00
return $return;
}
2023-06-21 12:34:58 +02:00
/**
* Returns the repetition data as it is found in the submitted data.
*
* @return array|null
*/
private function getRepetitionData(): ?array
{
$return = [];
// repetition data:
/** @var array|null $repetitions */
$repetitions = $this->get('repetitions');
if (null === $repetitions) {
return null;
}
/** @var array $repetition */
foreach ($repetitions as $repetition) {
$current = [];
if (array_key_exists('type', $repetition)) {
$current['type'] = $repetition['type'];
}
if (array_key_exists('moment', $repetition)) {
$current['moment'] = (string)$repetition['moment'];
}
if (array_key_exists('skip', $repetition)) {
$current['skip'] = (int)$repetition['skip'];
}
if (array_key_exists('weekend', $repetition)) {
$current['weekend'] = (int)$repetition['weekend'];
}
$return[] = $current;
}
if (0 === count($return)) {
return null;
}
return $return;
}
/**
* Returns the transaction data as it is found in the submitted data. It's a complex method according to code
* standards but it just has a lot of ??-statements because of the fields that may or may not exist.
*
2023-11-04 07:18:03 +01:00
* @return array
2023-06-21 12:34:58 +02:00
*/
2023-11-04 07:18:03 +01:00
private function getTransactionData(): array
2023-06-21 12:34:58 +02:00
{
$return = [];
// transaction data:
/** @var array|null $transactions */
$transactions = $this->get('transactions');
if (null === $transactions) {
2023-11-04 07:18:03 +01:00
return [];
2023-06-21 12:34:58 +02:00
}
/** @var array $transaction */
foreach ($transactions as $transaction) {
$return[] = $this->getSingleTransactionData($transaction);
}
return $return;
}
2018-06-29 19:27:07 +02:00
/**
* The rules that the incoming request must be matched against.
*
2018-06-29 19:27:07 +02:00
* @return array
*/
public function rules(): array
{
2019-08-22 17:56:48 +02:00
/** @var Recurrence $recurrence */
$recurrence = $this->route()->parameter('recurrence');
return [
2022-03-29 14:56:27 +02:00
'title' => sprintf('between:1,255|uniqueObjectForUser:recurrences,title,%d', $recurrence->id),
'description' => 'between:1,65000',
'first_date' => 'date',
2022-10-30 14:23:00 +01:00
'apply_rules' => [new IsBoolean()],
'active' => [new IsBoolean()],
2022-03-29 14:56:27 +02:00
'repeat_until' => 'nullable|date',
'nr_of_repetitions' => 'nullable|numeric|between:1,31',
2022-02-23 06:33:27 +01:00
2019-09-04 17:39:39 +02:00
'repetitions.*.type' => 'in:daily,weekly,ndom,monthly,yearly',
'repetitions.*.moment' => 'between:0,10',
2022-02-23 06:33:27 +01:00
'repetitions.*.skip' => 'nullable|numeric|between:0,31',
'repetitions.*.weekend' => 'nullable|numeric|min:1|max:4',
2019-08-27 05:57:09 +02:00
2021-03-12 18:31:19 +01:00
'transactions.*.description' => 'between:1,255',
'transactions.*.amount' => 'numeric|gt:0',
2022-02-23 06:33:27 +01:00
'transactions.*.foreign_amount' => 'nullable|numeric|gt:0',
'transactions.*.currency_id' => 'nullable|numeric|exists:transaction_currencies,id',
2023-04-26 06:17:04 +02:00
'transactions.*.currency_code' => 'nullable|min:3|max:51|exists:transaction_currencies,code',
2022-02-23 06:33:27 +01:00
'transactions.*.foreign_currency_id' => 'nullable|numeric|exists:transaction_currencies,id',
2023-04-26 06:17:04 +02:00
'transactions.*.foreign_currency_code' => 'nullable|min:3|max:51|exists:transaction_currencies,code',
2022-10-30 14:23:00 +01:00
'transactions.*.source_id' => ['numeric', 'nullable', new BelongsUser()],
2019-08-22 17:56:48 +02:00
'transactions.*.source_name' => 'between:1,255|nullable',
2022-10-30 14:23:00 +01:00
'transactions.*.destination_id' => ['numeric', 'nullable', new BelongsUser()],
2019-08-22 17:56:48 +02:00
'transactions.*.destination_name' => 'between:1,255|nullable',
// new and updated fields:
2022-10-30 14:23:00 +01:00
'transactions.*.budget_id' => ['nullable', 'mustExist:budgets,id', new BelongsUser()],
'transactions.*.budget_name' => ['between:1,255', 'nullable', new BelongsUser()],
'transactions.*.category_id' => ['nullable', 'mustExist:categories,id', new BelongsUser()],
'transactions.*.category_name' => 'between:1,255|nullable',
2022-10-30 14:23:00 +01:00
'transactions.*.piggy_bank_id' => ['nullable', 'numeric', 'mustExist:piggy_banks,id', new BelongsUser()],
'transactions.*.piggy_bank_name' => ['between:1,255', 'nullable', new BelongsUser()],
2022-02-23 06:33:27 +01:00
'transactions.*.tags' => 'nullable|between:1,64000',
2019-08-22 17:56:48 +02:00
];
2018-06-29 19:27:07 +02:00
}
/**
* Configure the validator instance.
*
2023-06-21 12:34:58 +02:00
* @param Validator $validator
2018-06-29 19:27:07 +02:00
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
2021-03-13 12:01:01 +01:00
//$this->validateOneRecurrenceTransaction($validator);
2021-03-14 20:03:27 +01:00
//$this->validateOneRepetitionUpdate($validator);
2023-06-03 21:17:49 +02:00
/** @var Recurrence $recurrence */
$recurrence = $this->route()->parameter('recurrence');
$this->validateTransactionId($recurrence, $validator);
2021-03-12 20:25:15 +01:00
$this->validateRecurrenceRepetition($validator);
2018-07-05 18:02:02 +02:00
$this->validateRepetitionMoment($validator);
$this->validateForeignCurrencyInformation($validator);
2019-08-27 05:57:09 +02:00
$this->valUpdateAccountInfo($validator);
2018-06-29 19:27:07 +02:00
}
);
}
2023-05-29 13:56:55 +02:00
}