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

94 lines
3.4 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-22 14:40:31 -05:00
use Carbon\Carbon;
use FireflyIII\Models\Account;
use Input;
/**
* 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
*/
public function getAccountDate(): array
{
return [
'name' => trim($this->input('name')),
'active' => intval($this->input('active')) === 1,
'accountType' => $this->input('what'),
'virtualBalance' => round($this->input('virtualBalance'), 2),
'virtualBalanceCurrency' => intval($this->input('amount_currency_id_virtualBalance')),
'user' => auth()->user()->id,
'iban' => trim($this->input('iban')),
'accountNumber' => trim($this->input('accountNumber')),
'accountRole' => $this->input('accountRole'),
'openingBalance' => round($this->input('openingBalance'), 2),
'openingBalanceDate' => new Carbon((string)$this->input('openingBalanceDate')),
'openingBalanceCurrency' => intval($this->input('amount_currency_id_openingBalance')),
'ccType' => $this->input('ccType'),
'ccMonthlyPaymentDate' => $this->input('ccMonthlyPaymentDate'),
];
}
2015-02-11 00:35:10 -06:00
/**
* @return array
*/
public function rules()
{
2016-04-26 14:40:15 -05:00
$accountRoles = join(',', array_keys(config('firefly.accountRoles')));
$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 = '';
if (Account::find(Input::get('id'))) {
$idRule = 'belongsToUser:accounts';
$nameRule = 'required|min:1|uniqueAccountForUser:' . Input::get('id');
}
return [
'id' => $idRule,
'name' => $nameRule,
'openingBalance' => 'numeric',
'iban' => 'iban',
'virtualBalance' => 'numeric',
'openingBalanceDate' => 'date',
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,
];
}
}