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

97 lines
3.7 KiB
PHP
Raw Normal View History

<?php
/**
* AccountFormRequest.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
namespace FireflyIII\Http\Requests;
2016-10-23 05:41:54 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/**
* Class AccountFormRequest
*
2016-02-04 00:28:39 -06:00
*
* @package FireflyIII\Http\Requests
*/
class AccountFormRequest extends Request
{
2015-02-11 00:35:10 -06:00
/**
* @return bool
*/
public function authorize()
{
// Only allow logged in users
2016-09-16 05:07:45 -05:00
return auth()->check();
}
2016-10-22 14:40:31 -05:00
/**
* @return array
*/
2016-10-22 15:03:00 -05:00
public function getAccountData(): array
2016-10-22 14:40:31 -05:00
{
return [
2017-01-21 01:32:23 -06:00
'name' => $this->string('name'),
'active' => $this->boolean('active'),
'accountType' => $this->string('what'),
'currency_id' => $this->integer('currency_id'),
'virtualBalance' => $this->float('virtualBalance'),
'virtualBalanceCurrency' => $this->integer('amount_currency_id_virtualBalance'),
'iban' => $this->string('iban'),
'BIC' => $this->string('BIC'),
'accountNumber' => $this->string('accountNumber'),
'accountRole' => $this->string('accountRole'),
'openingBalance' => $this->float('openingBalance'),
'openingBalanceDate' => $this->date('openingBalanceDate'),
'openingBalanceCurrency' => $this->integer('amount_currency_id_openingBalance'),
'ccType' => $this->string('ccType'),
'ccMonthlyPaymentDate' => $this->string('ccMonthlyPaymentDate'),
2016-10-22 14:40:31 -05:00
];
}
2015-02-11 00:35:10 -06:00
/**
* @return array
*/
public function rules()
{
2016-10-23 05:41:54 -05:00
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
2017-02-02 13:36:32 -06:00
$accountRoles = join(',', config('firefly.accountRoles'));
2016-04-26 14:40:15 -05:00
$types = join(',', array_keys(config('firefly.subTitlesByIdentifier')));
$ccPaymentTypes = join(',', array_keys(config('firefly.ccTypes')));
2015-02-09 00:23:39 -06:00
$nameRule = 'required|min:1|uniqueAccountForUser';
$idRule = '';
2016-10-23 05:41:54 -05:00
if (!is_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-02-11 11:35:16 -06:00
'openingBalance' => 'numeric|required_with:openingBalanceDate',
'openingBalanceDate' => 'date|required_with:openingBalance',
'iban' => 'iban',
2016-12-14 11:59:12 -06:00
'BIC' => 'bic',
'virtualBalance' => 'numeric',
'currency_id' => 'exists:transaction_currencies,id',
2016-02-10 23:40:06 -06:00
'accountNumber' => 'between:1,255|uniqueAccountNumberForUser',
'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 16:12:52 -06:00
'what' => 'in:' . $types,
];
}
}