Update requests to use uniform methods.

This commit is contained in:
James Cole
2017-01-20 19:49:35 +01:00
parent 6b57d4397a
commit 8635fe7ebb
12 changed files with 64 additions and 67 deletions

View File

@@ -51,7 +51,6 @@ final class Entry
public $destination_account_id; public $destination_account_id;
public $destination_account_name; public $destination_account_name;
public $budget_id; public $budget_id;
public $budget_name; public $budget_name;
public $category_id; public $category_id;

View File

@@ -39,21 +39,21 @@ class AccountFormRequest extends Request
public function getAccountData(): array public function getAccountData(): array
{ {
return [ return [
'name' => trim(strval($this->input('name'))), 'name' => $this->getFieldOrEmptyString('name'),
'active' => intval($this->input('active')) === 1, 'active' => intval($this->input('active')) === 1,
'accountType' => $this->input('what'), 'accountType' => $this->getFieldOrEmptyString('what'),
'currency_id' => intval($this->input('currency_id')), 'currency_id' => intval($this->input('currency_id')),
'virtualBalance' => round($this->input('virtualBalance'), 12), 'virtualBalance' => round($this->input('virtualBalance'), 12),
'virtualBalanceCurrency' => intval($this->input('amount_currency_id_virtualBalance')), 'virtualBalanceCurrency' => intval($this->input('amount_currency_id_virtualBalance')),
'iban' => trim(strval($this->input('iban'))), 'iban' => $this->getFieldOrEmptyString('iban'),
'BIC' => trim(strval($this->input('BIC'))), 'BIC' => $this->getFieldOrEmptyString('BIC'),
'accountNumber' => trim(strval($this->input('accountNumber'))), 'accountNumber' => $this->getFieldOrEmptyString('accountNumber'),
'accountRole' => $this->input('accountRole'), 'accountRole' => $this->getFieldOrEmptyString('accountRole'),
'openingBalance' => round($this->input('openingBalance'), 12), 'openingBalance' => round($this->input('openingBalance'), 12),
'openingBalanceDate' => new Carbon((string)$this->input('openingBalanceDate')), 'openingBalanceDate' => new Carbon((string)$this->input('openingBalanceDate')),
'openingBalanceCurrency' => intval($this->input('amount_currency_id_openingBalance')), 'openingBalanceCurrency' => intval($this->input('amount_currency_id_openingBalance')),
'ccType' => $this->input('ccType'), 'ccType' => $this->getFieldOrEmptyString('ccType'),
'ccMonthlyPaymentDate' => $this->input('ccMonthlyPaymentDate'), 'ccMonthlyPaymentDate' => $this->getFieldOrEmptyString('ccMonthlyPaymentDate'),
]; ];
} }
@@ -72,7 +72,7 @@ class AccountFormRequest extends Request
$idRule = ''; $idRule = '';
if (!is_null($repository->find(intval($this->get('id')))->id)) { if (!is_null($repository->find(intval($this->get('id')))->id)) {
$idRule = 'belongsToUser:accounts'; $idRule = 'belongsToUser:accounts';
$nameRule = 'required|min:1|uniqueAccountForUser:' . $this->get('id'); $nameRule = 'required|min:1|uniqueAccountForUser:' . intval($this->get('id'));
} }
return [ return [

View File

@@ -36,9 +36,9 @@ class AttachmentFormRequest extends Request
public function getAttachmentData(): array public function getAttachmentData(): array
{ {
return [ return [
'title' => trim($this->input('title')), 'title' => $this->getFieldOrEmptyString('title'),
'description' => trim($this->input('description')), 'description' => $this->getFieldOrEmptyString('description'),
'notes' => trim($this->input('notes')), 'notes' => $this->getFieldOrEmptyString('notes'),
]; ];
} }

View File

@@ -38,14 +38,14 @@ class BillFormRequest extends Request
public function getBillData() public function getBillData()
{ {
return [ return [
'name' => $this->get('name'), 'name' => $this->getFieldOrEmptyString('name'),
'match' => $this->get('match'), 'match' => $this->getFieldOrEmptyString('match'),
'amount_min' => round($this->get('amount_min'), 12), 'amount_min' => round($this->get('amount_min'), 12),
'amount_currency_id_amount_min' => intval($this->get('amount_currency_id_amount_min')), 'amount_currency_id_amount_min' => intval($this->get('amount_currency_id_amount_min')),
'amount_currency_id_amount_max' => intval($this->get('amount_currency_id_amount_max')), 'amount_currency_id_amount_max' => intval($this->get('amount_currency_id_amount_max')),
'amount_max' => round($this->get('amount_max'), 12), 'amount_max' => round($this->get('amount_max'), 12),
'date' => new Carbon($this->get('date')), 'date' => new Carbon($this->get('date')),
'repeat_freq' => $this->get('repeat_freq'), 'repeat_freq' => $this->getFieldOrEmptyString('repeat_freq'),
'skip' => intval($this->get('skip')), 'skip' => intval($this->get('skip')),
'automatch' => intval($this->get('automatch')) === 1, 'automatch' => intval($this->get('automatch')) === 1,
'active' => intval($this->get('active')) === 1, 'active' => intval($this->get('active')) === 1,

View File

@@ -37,7 +37,7 @@ class BudgetFormRequest extends Request
public function getBudgetData(): array public function getBudgetData(): array
{ {
return [ return [
'name' => trim($this->input('name')), 'name' => $this->getFieldOrEmptyString('name'),
'active' => intval($this->input('active')) == 1, 'active' => intval($this->input('active')) == 1,
]; ];
} }

View File

@@ -38,7 +38,7 @@ class CategoryFormRequest extends Request
public function getCategoryData(): array public function getCategoryData(): array
{ {
return [ return [
'name' => trim($this->input('name')), 'name' => $this->getFieldOrEmptyString('name'),
]; ];
} }

View File

@@ -36,9 +36,9 @@ class CurrencyFormRequest extends Request
public function getCurrencyData() public function getCurrencyData()
{ {
return [ return [
'name' => $this->get('name'), 'name' => $this->getFieldOrEmptyString('name'),
'code' => $this->get('code'), 'code' => $this->getFieldOrEmptyString('code'),
'symbol' => $this->get('symbol'), 'symbol' => $this->getFieldOrEmptyString('symbol'),
'decimal_places' => intval($this->get('decimal_places')), 'decimal_places' => intval($this->get('decimal_places')),
]; ];
} }

View File

@@ -54,8 +54,8 @@ class JournalFormRequest extends Request
'due_date' => $this->getDateOrNull('due_date'), 'due_date' => $this->getDateOrNull('due_date'),
'payment_date' => $this->getDateOrNull('payment_date'), 'payment_date' => $this->getDateOrNull('payment_date'),
'invoice_date' => $this->getDateOrNull('invoice_date'), 'invoice_date' => $this->getDateOrNull('invoice_date'),
'internal_reference' => trim(strval($this->get('internal_reference'))), 'internal_reference' => $this->getFieldOrEmptyString('internal_reference'),
'notes' => trim(strval($this->get('notes'))), 'notes' => $this->getFieldOrEmptyString('notes'),
// transaction / journal data: // transaction / journal data:
'description' => $this->getFieldOrEmptyString('description'), 'description' => $this->getFieldOrEmptyString('description'),
@@ -152,40 +152,4 @@ class JournalFormRequest extends Request
{ {
return $this->get($field) ? new Carbon($this->get($field)) : null; return $this->get($field) ? new Carbon($this->get($field)) : null;
} }
/**
* @param string $field
*
* @return string
*/
private function getFieldOrEmptyString(string $field): string
{
$string = $this->get($field) ?? '';
$search = [
"\xa0", // non-breaking space
"\u{1680}", // OGHAM SPACE MARK
"\u{180E}", // MONGOLIAN VOWEL SEPARATOR
"\u{2000}", // EN QUAD
"\u{2001}", // EM QUAD
"\u{2002}", //EN SPACE
"\u{2003}", //EM SPACE
"\u{2004}", //THREE-PER-EM SPACE
"\u{2005}", //FOUR-PER-EM SPACE
"\u{2006}", //SIX-PER-EM SPACE
"\u{2007}", //FIGURE SPACE
"\u{2008}", //PUNCTUATION SPACE
"\u{2009}", //THIN SPACE
"\u{200A}", //HAIR SPACE
"\u{200B}", //ZERO WIDTH SPACE
"\u{202F}", //NARROW NO-BREAK SPACE
"\u{205F}", //MEDIUM MATHEMATICAL SPACE
"\u{3000}", //IDEOGRAPHIC SPACE
"\u{FEFF}", // ZERO WIDTH NO -BREAK SPACE
];
$replace = "\x20"; // plain old normal space
$string = str_replace($search, $replace, $string);
return trim($string);
}
} }

View File

@@ -38,7 +38,7 @@ class PiggyBankFormRequest extends Request
public function getPiggyBankData(): array public function getPiggyBankData(): array
{ {
return [ return [
'name' => trim($this->get('name')), 'name' => $this->getFieldOrEmptyString('name'),
'startdate' => new Carbon, 'startdate' => new Carbon,
'account_id' => intval($this->get('account_id')), 'account_id' => intval($this->get('account_id')),
'targetamount' => round($this->get('targetamount'), 12), 'targetamount' => round($this->get('targetamount'), 12),

View File

@@ -20,7 +20,41 @@ use Illuminate\Foundation\Http\FormRequest;
* *
* @package FireflyIII\Http\Requests * @package FireflyIII\Http\Requests
*/ */
abstract class Request extends FormRequest class Request extends FormRequest
{ {
// /**
* @param string $field
*
* @return string
*/
protected function getFieldOrEmptyString(string $field): string
{
$string = $this->get($field) ?? '';
$search = [
"\u{00A0}", // non-breaking space
"\u{1680}", // OGHAM SPACE MARK
"\u{180E}", // MONGOLIAN VOWEL SEPARATOR
"\u{2000}", // EN QUAD
"\u{2001}", // EM QUAD
"\u{2002}", // EN SPACE
"\u{2003}", // EM SPACE
"\u{2004}", // THREE-PER-EM SPACE
"\u{2005}", // FOUR-PER-EM SPACE
"\u{2006}", // SIX-PER-EM SPACE
"\u{2007}", // FIGURE SPACE
"\u{2008}", // PUNCTUATION SPACE
"\u{2009}", // THIN SPACE
"\u{200A}", // HAIR SPACE
"\u{200B}", // ZERO WIDTH SPACE
"\u{202F}", // NARROW NO-BREAK SPACE
"\u{205F}", // MEDIUM MATHEMATICAL SPACE
"\u{3000}", // IDEOGRAPHIC SPACE
"\u{FEFF}", // ZERO WIDTH NO -BREAK SPACE
];
$replace = "\x20"; // plain old normal space
$string = str_replace($search, $replace, $string);
return trim($string);
}
} }

View File

@@ -38,10 +38,10 @@ class RuleFormRequest extends Request
public function getRuleData(): array public function getRuleData(): array
{ {
return [ return [
'title' => trim($this->get('title')), 'title' => $this->getFieldOrEmptyString('title'),
'active' => intval($this->get('active')) == 1, 'active' => intval($this->get('active')) == 1,
'trigger' => trim($this->get('trigger')), 'trigger' => $this->getFieldOrEmptyString('trigger'),
'description' => trim($this->get('description')), 'description' => $this->getFieldOrEmptyString('description'),
'rule-triggers' => $this->get('rule-trigger'), 'rule-triggers' => $this->get('rule-trigger'),
'rule-trigger-values' => $this->get('rule-trigger-value'), 'rule-trigger-values' => $this->get('rule-trigger-value'),
'rule-trigger-stop' => $this->get('rule-trigger-stop'), 'rule-trigger-stop' => $this->get('rule-trigger-stop'),

View File

@@ -38,8 +38,8 @@ class RuleGroupFormRequest extends Request
public function getRuleGroupData(): array public function getRuleGroupData(): array
{ {
return [ return [
'title' => trim($this->input('title')), 'title' => $this->getFieldOrEmptyString('title'),
'description' => trim($this->input('description')), 'description' => $this->getFieldOrEmptyString('description'),
]; ];
} }