Files
firefly-iii/app/Http/Requests/AccountFormRequest.php

101 lines
4.0 KiB
PHP
Raw Normal View History

<?php
/**
* AccountFormRequest.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Requests;
2016-10-23 12:41:54 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/**
2017-11-15 12:25:49 +01:00
* Class AccountFormRequest.
*/
class AccountFormRequest extends Request
{
2015-02-11 07:35:10 +01:00
/**
* @return bool
*/
public function authorize()
{
// Only allow logged in users
2016-09-16 12:07:45 +02:00
return auth()->check();
}
2016-10-22 21:40:31 +02:00
/**
* @return array
*/
2016-10-22 22:03:00 +02:00
public function getAccountData(): array
2016-10-22 21:40:31 +02:00
{
return [
2017-06-05 11:12:50 +02:00
'name' => $this->string('name'),
'active' => $this->boolean('active'),
'accountType' => $this->string('what'),
'currency_id' => $this->integer('currency_id'),
2017-11-18 05:45:58 +01:00
'virtualBalance' => $this->string('virtualBalance'),
2017-06-05 11:12:50 +02:00
'iban' => $this->string('iban'),
'BIC' => $this->string('BIC'),
'accountNumber' => $this->string('accountNumber'),
'accountRole' => $this->string('accountRole'),
2017-11-18 05:45:58 +01:00
'openingBalance' => $this->string('openingBalance'),
2017-06-05 11:12:50 +02:00
'openingBalanceDate' => $this->date('openingBalanceDate'),
'ccType' => $this->string('ccType'),
'ccMonthlyPaymentDate' => $this->string('ccMonthlyPaymentDate'),
2016-10-22 21:40:31 +02:00
];
}
2015-02-11 07:35:10 +01:00
/**
* @return array
*/
public function rules()
{
2016-10-23 12:41:54 +02:00
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
2017-02-02 20:36:32 +01:00
$accountRoles = join(',', config('firefly.accountRoles'));
2016-04-26 21:40:15 +02:00
$types = join(',', array_keys(config('firefly.subTitlesByIdentifier')));
$ccPaymentTypes = join(',', array_keys(config('firefly.ccTypes')));
2015-02-09 07:23:39 +01:00
$nameRule = 'required|min:1|uniqueAccountForUser';
$idRule = '';
2017-11-15 12:25:49 +01:00
if (null !== $repository->find(intval($this->get('id')))->id) {
$idRule = 'belongsToUser:accounts';
$nameRule = 'required|min:1|uniqueAccountForUser:' . intval($this->get('id'));
}
return [
'id' => $idRule,
'name' => $nameRule,
2017-09-12 21:44:31 +02:00
'openingBalance' => 'numeric|required_with:openingBalanceDate|nullable',
'openingBalanceDate' => 'date|required_with:openingBalance|nullable',
'iban' => 'iban|nullable',
'BIC' => 'bic|nullable',
'virtualBalance' => 'numeric|nullable',
'currency_id' => 'exists:transaction_currencies,id',
2017-09-12 21:44:31 +02:00
'accountNumber' => 'between:1,255|uniqueAccountNumberForUser|nullable',
'accountRole' => 'in:' . $accountRoles,
'active' => 'boolean',
'ccType' => 'in:' . $ccPaymentTypes,
'ccMonthlyPaymentDate' => 'date',
'amount_currency_id_openingBalance' => 'exists:transaction_currencies,id',
'amount_currency_id_virtualBalance' => 'exists:transaction_currencies,id',
2016-01-15 23:12:52 +01:00
'what' => 'in:' . $types,
];
}
}