Skip to content

Firefly III: a personal finances manager

Update requests to use uniform methods.

James Cole8635fe7ebbb2
diff --git a/app/Export/Entry/Entry.php b/app/Export/Entry/Entry.php
index f9c324582b..0afc40ada4 100644
--- a/app/Export/Entry/Entry.php
+++ b/app/Export/Entry/Entry.php
@@ -51,7 +51,6 @@ final class Entry
public $destination_account_id;
public $destination_account_name;
-
public $budget_id;
public $budget_name;
public $category_id;
diff --git a/app/Http/Requests/AccountFormRequest.php b/app/Http/Requests/AccountFormRequest.php
index 364898614c..95def39b47 100644
--- a/app/Http/Requests/AccountFormRequest.php
+++ b/app/Http/Requests/AccountFormRequest.php
@@ -39,21 +39,21 @@ class AccountFormRequest extends Request
public function getAccountData(): array
{
return [
- 'name' => trim(strval($this->input('name'))),
+ 'name' => $this->getFieldOrEmptyString('name'),
'active' => intval($this->input('active')) === 1,
- 'accountType' => $this->input('what'),
+ 'accountType' => $this->getFieldOrEmptyString('what'),
'currency_id' => intval($this->input('currency_id')),
'virtualBalance' => round($this->input('virtualBalance'), 12),
'virtualBalanceCurrency' => intval($this->input('amount_currency_id_virtualBalance')),
- 'iban' => trim(strval($this->input('iban'))),
- 'BIC' => trim(strval($this->input('BIC'))),
- 'accountNumber' => trim(strval($this->input('accountNumber'))),
- 'accountRole' => $this->input('accountRole'),
+ 'iban' => $this->getFieldOrEmptyString('iban'),
+ 'BIC' => $this->getFieldOrEmptyString('BIC'),
+ 'accountNumber' => $this->getFieldOrEmptyString('accountNumber'),
+ 'accountRole' => $this->getFieldOrEmptyString('accountRole'),
'openingBalance' => round($this->input('openingBalance'), 12),
'openingBalanceDate' => new Carbon((string)$this->input('openingBalanceDate')),
'openingBalanceCurrency' => intval($this->input('amount_currency_id_openingBalance')),
- 'ccType' => $this->input('ccType'),
- 'ccMonthlyPaymentDate' => $this->input('ccMonthlyPaymentDate'),
+ 'ccType' => $this->getFieldOrEmptyString('ccType'),
+ 'ccMonthlyPaymentDate' => $this->getFieldOrEmptyString('ccMonthlyPaymentDate'),
];
}
@@ -72,7 +72,7 @@ class AccountFormRequest extends Request
$idRule = '';
if (!is_null($repository->find(intval($this->get('id')))->id)) {
$idRule = 'belongsToUser:accounts';
- $nameRule = 'required|min:1|uniqueAccountForUser:' . $this->get('id');
+ $nameRule = 'required|min:1|uniqueAccountForUser:' . intval($this->get('id'));
}
return [
diff --git a/app/Http/Requests/AttachmentFormRequest.php b/app/Http/Requests/AttachmentFormRequest.php
index 16028cab37..b2da7b7f83 100644
--- a/app/Http/Requests/AttachmentFormRequest.php
+++ b/app/Http/Requests/AttachmentFormRequest.php
@@ -36,9 +36,9 @@ class AttachmentFormRequest extends Request
public function getAttachmentData(): array
{
return [
- 'title' => trim($this->input('title')),
- 'description' => trim($this->input('description')),
- 'notes' => trim($this->input('notes')),
+ 'title' => $this->getFieldOrEmptyString('title'),
+ 'description' => $this->getFieldOrEmptyString('description'),
+ 'notes' => $this->getFieldOrEmptyString('notes'),
];
}
diff --git a/app/Http/Requests/BillFormRequest.php b/app/Http/Requests/BillFormRequest.php
index 0b0b85e29d..2ef7c2ae88 100644
--- a/app/Http/Requests/BillFormRequest.php
+++ b/app/Http/Requests/BillFormRequest.php
@@ -38,14 +38,14 @@ class BillFormRequest extends Request
public function getBillData()
{
return [
- 'name' => $this->get('name'),
- 'match' => $this->get('match'),
+ 'name' => $this->getFieldOrEmptyString('name'),
+ 'match' => $this->getFieldOrEmptyString('match'),
'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_max' => intval($this->get('amount_currency_id_amount_max')),
'amount_max' => round($this->get('amount_max'), 12),
'date' => new Carbon($this->get('date')),
- 'repeat_freq' => $this->get('repeat_freq'),
+ 'repeat_freq' => $this->getFieldOrEmptyString('repeat_freq'),
'skip' => intval($this->get('skip')),
'automatch' => intval($this->get('automatch')) === 1,
'active' => intval($this->get('active')) === 1,
diff --git a/app/Http/Requests/BudgetFormRequest.php b/app/Http/Requests/BudgetFormRequest.php
index 3ca9d0fb37..cfe113aadc 100644
--- a/app/Http/Requests/BudgetFormRequest.php
+++ b/app/Http/Requests/BudgetFormRequest.php
@@ -37,7 +37,7 @@ class BudgetFormRequest extends Request
public function getBudgetData(): array
{
return [
- 'name' => trim($this->input('name')),
+ 'name' => $this->getFieldOrEmptyString('name'),
'active' => intval($this->input('active')) == 1,
];
}
diff --git a/app/Http/Requests/CategoryFormRequest.php b/app/Http/Requests/CategoryFormRequest.php
index 34347608af..8a53b6a6ea 100644
--- a/app/Http/Requests/CategoryFormRequest.php
+++ b/app/Http/Requests/CategoryFormRequest.php
@@ -38,7 +38,7 @@ class CategoryFormRequest extends Request
public function getCategoryData(): array
{
return [
- 'name' => trim($this->input('name')),
+ 'name' => $this->getFieldOrEmptyString('name'),
];
}
diff --git a/app/Http/Requests/CurrencyFormRequest.php b/app/Http/Requests/CurrencyFormRequest.php
index ffbfa7e399..d2131c1eff 100644
--- a/app/Http/Requests/CurrencyFormRequest.php
+++ b/app/Http/Requests/CurrencyFormRequest.php
@@ -36,9 +36,9 @@ class CurrencyFormRequest extends Request
public function getCurrencyData()
{
return [
- 'name' => $this->get('name'),
- 'code' => $this->get('code'),
- 'symbol' => $this->get('symbol'),
+ 'name' => $this->getFieldOrEmptyString('name'),
+ 'code' => $this->getFieldOrEmptyString('code'),
+ 'symbol' => $this->getFieldOrEmptyString('symbol'),
'decimal_places' => intval($this->get('decimal_places')),
];
}
diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php
index bde5f20166..864242d22c 100644
--- a/app/Http/Requests/JournalFormRequest.php
+++ b/app/Http/Requests/JournalFormRequest.php
@@ -54,8 +54,8 @@ class JournalFormRequest extends Request
'due_date' => $this->getDateOrNull('due_date'),
'payment_date' => $this->getDateOrNull('payment_date'),
'invoice_date' => $this->getDateOrNull('invoice_date'),
- 'internal_reference' => trim(strval($this->get('internal_reference'))),
- 'notes' => trim(strval($this->get('notes'))),
+ 'internal_reference' => $this->getFieldOrEmptyString('internal_reference'),
+ 'notes' => $this->getFieldOrEmptyString('notes'),
// transaction / journal data:
'description' => $this->getFieldOrEmptyString('description'),
@@ -152,40 +152,4 @@ class JournalFormRequest extends Request
{
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);
- }
}
diff --git a/app/Http/Requests/PiggyBankFormRequest.php b/app/Http/Requests/PiggyBankFormRequest.php
index d0e6889d62..70e7ce9d7a 100644
--- a/app/Http/Requests/PiggyBankFormRequest.php
+++ b/app/Http/Requests/PiggyBankFormRequest.php
@@ -38,7 +38,7 @@ class PiggyBankFormRequest extends Request
public function getPiggyBankData(): array
{
return [
- 'name' => trim($this->get('name')),
+ 'name' => $this->getFieldOrEmptyString('name'),
'startdate' => new Carbon,
'account_id' => intval($this->get('account_id')),
'targetamount' => round($this->get('targetamount'), 12),
diff --git a/app/Http/Requests/Request.php b/app/Http/Requests/Request.php
index 84cb318d3e..8a137ff254 100644
--- a/app/Http/Requests/Request.php
+++ b/app/Http/Requests/Request.php
@@ -20,7 +20,41 @@ use Illuminate\Foundation\Http\FormRequest;
*
* @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);
+ }
}
diff --git a/app/Http/Requests/RuleFormRequest.php b/app/Http/Requests/RuleFormRequest.php
index 814f0c560d..5906d62967 100644
--- a/app/Http/Requests/RuleFormRequest.php
+++ b/app/Http/Requests/RuleFormRequest.php
@@ -38,10 +38,10 @@ class RuleFormRequest extends Request
public function getRuleData(): array
{
return [
- 'title' => trim($this->get('title')),
+ 'title' => $this->getFieldOrEmptyString('title'),
'active' => intval($this->get('active')) == 1,
- 'trigger' => trim($this->get('trigger')),
- 'description' => trim($this->get('description')),
+ 'trigger' => $this->getFieldOrEmptyString('trigger'),
+ 'description' => $this->getFieldOrEmptyString('description'),
'rule-triggers' => $this->get('rule-trigger'),
'rule-trigger-values' => $this->get('rule-trigger-value'),
'rule-trigger-stop' => $this->get('rule-trigger-stop'),
diff --git a/app/Http/Requests/RuleGroupFormRequest.php b/app/Http/Requests/RuleGroupFormRequest.php
index b5420c5ce5..ca0de4c947 100644
--- a/app/Http/Requests/RuleGroupFormRequest.php
+++ b/app/Http/Requests/RuleGroupFormRequest.php
@@ -38,8 +38,8 @@ class RuleGroupFormRequest extends Request
public function getRuleGroupData(): array
{
return [
- 'title' => trim($this->input('title')),
- 'description' => trim($this->input('description')),
+ 'title' => $this->getFieldOrEmptyString('title'),
+ 'description' => $this->getFieldOrEmptyString('description'),
];
}