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

202 lines
7.8 KiB
PHP
Raw Normal View History

2018-06-29 19:27:07 +02:00
<?php
2021-03-06 16:15:39 +01:00
/*
* RecurrenceStoreRequest.php
2021-03-06 16:15:39 +01:00
* Copyright (c) 2021 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-06 16:15:39 +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\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-06 16:15:39 +01:00
* Class StoreRequest
2018-06-29 19:27:07 +02:00
*/
2021-03-06 16:15:39 +01:00
class StoreRequest extends FormRequest
2018-06-29 19:27:07 +02:00
{
2022-10-30 14:23:00 +01:00
use ConvertsDataTypes;
use RecurrenceValidation;
use TransactionValidation;
use CurrencyValidation;
use GetRecurrenceData;
use ChecksLogin;
2020-11-08 13:36:13 +01:00
/**
* Get all data from the request.
*
* @return array
*/
public function getAll(): array
{
2021-03-14 16:08:49 +01:00
$fields = [
2022-05-04 05:53:47 +02:00
'type' => ['type', 'convertString'],
'title' => ['title', 'convertString'],
'description' => ['description', 'convertString'],
2021-03-14 16:08:49 +01:00
'first_date' => ['first_date', 'date'],
'repeat_until' => ['repeat_until', 'date'],
2022-10-01 05:29:42 +02:00
'nr_of_repetitions' => ['nr_of_repetitions', 'convertInteger'],
2021-03-14 16:08:49 +01:00
'apply_rules' => ['apply_rules', 'boolean'],
'active' => ['active', 'boolean'],
2021-04-06 13:30:09 +02:00
'notes' => ['notes', 'stringWithNewlines'],
2021-03-14 16:08:49 +01:00
];
$recurrence = $this->getAllData($fields);
2020-03-15 08:16:16 +01:00
return [
2021-03-14 16:08:49 +01:00
'recurrence' => $recurrence,
'transactions' => $this->getTransactionData(),
'repetitions' => $this->getRepetitionData(),
];
}
2020-10-18 08:00:49 +02:00
/**
* 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.
*
* @return array
*/
private function getTransactionData(): array
{
$return = [];
// transaction data:
2021-04-06 08:51:27 +02:00
/** @var array|null $transactions */
2020-10-18 08:00:49 +02:00
$transactions = $this->get('transactions');
if (null === $transactions) {
return [];
}
/** @var array $transaction */
foreach ($transactions as $transaction) {
2021-03-14 16:08:49 +01:00
$return[] = $this->getSingleTransactionData($transaction);
2020-10-18 08:00:49 +02:00
}
return $return;
}
/**
* Returns the repetition data as it is found in the submitted data.
*
* @return array
*/
private function getRepetitionData(): array
{
$return = [];
// repetition data:
2021-04-06 08:51:27 +02:00
/** @var array|null $repetitions */
2020-10-18 08:00:49 +02:00
$repetitions = $this->get('repetitions');
if (null === $repetitions) {
return [];
}
/** @var array $repetition */
foreach ($repetitions as $repetition) {
2021-03-14 16:08:49 +01:00
$current = [];
if (array_key_exists('type', $repetition)) {
$current['type'] = $repetition['type'];
}
if (array_key_exists('moment', $repetition)) {
$current['moment'] = $repetition['moment'];
}
if (array_key_exists('skip', $repetition)) {
2022-12-29 19:41:57 +01:00
$current['skip'] = (int)$repetition['skip'];
2021-03-14 16:08:49 +01:00
}
if (array_key_exists('weekend', $repetition)) {
2022-12-29 19:41:57 +01:00
$current['weekend'] = (int)$repetition['weekend'];
2021-03-14 16:08:49 +01:00
}
$return[] = $current;
2020-10-18 08:00:49 +02:00
}
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
return [
2022-03-29 14:56:27 +02:00
'type' => 'required|in:withdrawal,transfer,deposit',
'title' => 'required|between:1,255|uniqueObjectForUser:recurrences,title',
'description' => 'between:1,65000',
'first_date' => 'required|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',
'repetitions.*.type' => 'required|in:daily,weekly,ndom,monthly,yearly',
'repetitions.*.moment' => 'between:0,10',
'repetitions.*.skip' => 'nullable|numeric|between:0,31',
'repetitions.*.weekend' => 'numeric|min:1|max:4',
2022-02-23 06:33:27 +01:00
2019-08-22 17:56:48 +02:00
'transactions.*.description' => 'required|between:1,255',
2020-07-06 06:55:27 +02:00
'transactions.*.amount' => 'required|numeric|gt:0',
2022-02-17 19:03:41 +01:00
'transactions.*.foreign_amount' => 'nullable|numeric|gt:0',
2022-02-21 16:48:26 +01:00
'transactions.*.currency_id' => 'nullable|numeric|exists:transaction_currencies,id',
'transactions.*.currency_code' => 'nullable|min:3|max:3|exists:transaction_currencies,code',
2022-02-17 19:03:41 +01:00
'transactions.*.foreign_currency_id' => 'nullable|numeric|exists:transaction_currencies,id',
'transactions.*.foreign_currency_code' => 'nullable|min:3|max:3|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-17 19:03:41 +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.
*
2022-12-29 19:41:57 +01: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-14 16:08:49 +01:00
$this->validateRecurringConfig($validator);
2019-06-09 08:26:23 +02:00
$this->validateOneRecurrenceTransaction($validator);
2018-07-05 18:02:02 +02:00
$this->validateOneRepetition($validator);
$this->validateRecurrenceRepetition($validator);
$this->validateRepetitionMoment($validator);
$this->validateForeignCurrencyInformation($validator);
2018-06-29 19:27:07 +02:00
$this->validateAccountInformation($validator);
}
);
}
}