Skip to content

Firefly III: a personal finances manager

Make request code more uniform.

James Cole71f6ba34189f
diff --git a/app/Http/Requests/AccountFormRequest.php b/app/Http/Requests/AccountFormRequest.php
index 95def39b47..91b05abcb0 100644
--- a/app/Http/Requests/AccountFormRequest.php
+++ b/app/Http/Requests/AccountFormRequest.php
@@ -13,7 +13,6 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Requests;
-use Carbon\Carbon;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/**
@@ -39,21 +38,21 @@ class AccountFormRequest extends Request
public function getAccountData(): array
{
return [
- 'name' => $this->getFieldOrEmptyString('name'),
- 'active' => intval($this->input('active')) === 1,
- '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' => $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->getFieldOrEmptyString('ccType'),
- 'ccMonthlyPaymentDate' => $this->getFieldOrEmptyString('ccMonthlyPaymentDate'),
+ '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'),
];
}
diff --git a/app/Http/Requests/AttachmentFormRequest.php b/app/Http/Requests/AttachmentFormRequest.php
index b2da7b7f83..6f7608bc46 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' => $this->getFieldOrEmptyString('title'),
- 'description' => $this->getFieldOrEmptyString('description'),
- 'notes' => $this->getFieldOrEmptyString('notes'),
+ 'title' => $this->string('title'),
+ 'description' => $this->string('description'),
+ 'notes' => $this->string('notes'),
];
}
diff --git a/app/Http/Requests/BillFormRequest.php b/app/Http/Requests/BillFormRequest.php
index 2ef7c2ae88..992542eca1 100644
--- a/app/Http/Requests/BillFormRequest.php
+++ b/app/Http/Requests/BillFormRequest.php
@@ -13,8 +13,6 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Requests;
-use Carbon\Carbon;
-
/**
* Class BillFormRequest
*
@@ -38,17 +36,17 @@ class BillFormRequest extends Request
public function getBillData()
{
return [
- '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->getFieldOrEmptyString('repeat_freq'),
- 'skip' => intval($this->get('skip')),
- 'automatch' => intval($this->get('automatch')) === 1,
- 'active' => intval($this->get('active')) === 1,
+ 'name' => $this->string('name'),
+ 'match' => $this->string('match'),
+ 'amount_min' => $this->float('amount_min'),
+ 'amount_currency_id_amount_min' => $this->integer('amount_currency_id_amount_min'),
+ 'amount_currency_id_amount_max' => $this->integer('amount_currency_id_amount_max'),
+ 'amount_max' => $this->float('amount_max'),
+ 'date' => $this->date('date'),
+ 'repeat_freq' => $this->string('repeat_freq'),
+ 'skip' => $this->integer('skip'),
+ 'automatch' => $this->boolean('automatch'),
+ 'active' => $this->boolean('active'),
];
}
diff --git a/app/Http/Requests/BudgetFormRequest.php b/app/Http/Requests/BudgetFormRequest.php
index cfe113aadc..c7d9d0de63 100644
--- a/app/Http/Requests/BudgetFormRequest.php
+++ b/app/Http/Requests/BudgetFormRequest.php
@@ -37,8 +37,8 @@ class BudgetFormRequest extends Request
public function getBudgetData(): array
{
return [
- 'name' => $this->getFieldOrEmptyString('name'),
- 'active' => intval($this->input('active')) == 1,
+ 'name' => $this->string('name'),
+ 'active' => $this->boolean('active'),
];
}
diff --git a/app/Http/Requests/CategoryFormRequest.php b/app/Http/Requests/CategoryFormRequest.php
index 8a53b6a6ea..b7b5e94a5e 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' => $this->getFieldOrEmptyString('name'),
+ 'name' => $this->string('name'),
];
}
diff --git a/app/Http/Requests/ConfigurationRequest.php b/app/Http/Requests/ConfigurationRequest.php
index d6bd5d7ca2..452e613e73 100644
--- a/app/Http/Requests/ConfigurationRequest.php
+++ b/app/Http/Requests/ConfigurationRequest.php
@@ -36,8 +36,8 @@ class ConfigurationRequest extends Request
public function getConfigurationData(): array
{
return [
- 'single_user_mode' => intval($this->get('single_user_mode')) === 1,
- 'is_demo_site' => intval($this->get('is_demo_site')) === 1,
+ 'single_user_mode' => $this->boolean('single_user_mode'),
+ 'is_demo_site' => $this->boolean('is_demo_site'),
];
}
diff --git a/app/Http/Requests/CurrencyFormRequest.php b/app/Http/Requests/CurrencyFormRequest.php
index d2131c1eff..0449f9b4ab 100644
--- a/app/Http/Requests/CurrencyFormRequest.php
+++ b/app/Http/Requests/CurrencyFormRequest.php
@@ -36,10 +36,10 @@ class CurrencyFormRequest extends Request
public function getCurrencyData()
{
return [
- 'name' => $this->getFieldOrEmptyString('name'),
- 'code' => $this->getFieldOrEmptyString('code'),
- 'symbol' => $this->getFieldOrEmptyString('symbol'),
- 'decimal_places' => intval($this->get('decimal_places')),
+ 'name' => $this->string('name'),
+ 'code' => $this->string('code'),
+ 'symbol' => $this->string('symbol'),
+ 'decimal_places' => $this->integer('decimal_places'),
];
}
diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php
index 864242d22c..62dc5656d2 100644
--- a/app/Http/Requests/JournalFormRequest.php
+++ b/app/Http/Requests/JournalFormRequest.php
@@ -13,7 +13,6 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Requests;
-use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionType;
@@ -43,30 +42,30 @@ class JournalFormRequest extends Request
{
$data = [
'what' => $this->get('what'), // type. can be 'deposit', 'withdrawal' or 'transfer'
- 'date' => new Carbon($this->get('date')),
- 'tags' => explode(',', $this->getFieldOrEmptyString('tags')),
- 'currency_id' => intval($this->get('amount_currency_id_amount')),
+ 'date' => $this->date('date'),
+ 'tags' => explode(',', $this->string('tags')),
+ 'currency_id' => $this->integer('amount_currency_id_amount'),
// all custom fields:
- 'interest_date' => $this->getDateOrNull('interest_date'),
- 'book_date' => $this->getDateOrNull('book_date'),
- 'process_date' => $this->getDateOrNull('process_date'),
- 'due_date' => $this->getDateOrNull('due_date'),
- 'payment_date' => $this->getDateOrNull('payment_date'),
- 'invoice_date' => $this->getDateOrNull('invoice_date'),
- 'internal_reference' => $this->getFieldOrEmptyString('internal_reference'),
- 'notes' => $this->getFieldOrEmptyString('notes'),
+ 'interest_date' => $this->date('interest_date'),
+ 'book_date' => $this->date('book_date'),
+ 'process_date' => $this->date('process_date'),
+ 'due_date' => $this->date('due_date'),
+ 'payment_date' => $this->date('payment_date'),
+ 'invoice_date' => $this->date('invoice_date'),
+ 'internal_reference' => $this->string('internal_reference'),
+ 'notes' => $this->string('notes'),
// transaction / journal data:
- 'description' => $this->getFieldOrEmptyString('description'),
- 'amount' => round($this->get('amount'), 12),
- 'budget_id' => intval($this->get('budget_id')),
- 'category' => $this->getFieldOrEmptyString('category'),
- 'source_account_id' => intval($this->get('source_account_id')),
- 'source_account_name' => $this->getFieldOrEmptyString('source_account_name'),
- 'destination_account_id' => $this->getFieldOrEmptyString('destination_account_id'),
- 'destination_account_name' => $this->getFieldOrEmptyString('destination_account_name'),
- 'piggy_bank_id' => intval($this->get('piggy_bank_id')),
+ 'description' => $this->string('description'),
+ 'amount' => $this->float('amount'),
+ 'budget_id' => $this->integer('budget_id'),
+ 'category' => $this->string('category'),
+ 'source_account_id' => $this->integer('source_account_id'),
+ 'source_account_name' => $this->string('source_account_name'),
+ 'destination_account_id' => $this->string('destination_account_id'),
+ 'destination_account_name' => $this->string('destination_account_name'),
+ 'piggy_bank_id' => $this->integer('piggy_bank_id'),
];
@@ -142,14 +141,4 @@ class JournalFormRequest extends Request
return $rules;
}
-
- /**
- * @param string $field
- *
- * @return Carbon|null
- */
- private function getDateOrNull(string $field)
- {
- return $this->get($field) ? new Carbon($this->get($field)) : null;
- }
}
diff --git a/app/Http/Requests/PiggyBankFormRequest.php b/app/Http/Requests/PiggyBankFormRequest.php
index 70e7ce9d7a..ba969dcf64 100644
--- a/app/Http/Requests/PiggyBankFormRequest.php
+++ b/app/Http/Requests/PiggyBankFormRequest.php
@@ -38,12 +38,12 @@ class PiggyBankFormRequest extends Request
public function getPiggyBankData(): array
{
return [
- 'name' => $this->getFieldOrEmptyString('name'),
+ 'name' => $this->string('name'),
'startdate' => new Carbon,
- 'account_id' => intval($this->get('account_id')),
- 'targetamount' => round($this->get('targetamount'), 12),
- 'targetdate' => strlen(strval($this->get('targetdate'))) > 0 ? new Carbon($this->get('targetdate')) : null,
- 'note' => trim(strval($this->get('note'))),
+ 'account_id' => $this->integer('account_id'),
+ 'targetamount' => $this->float('targetamount'),
+ 'targetdate' => $this->date('targetdate'),
+ 'note' => $this->string('note'),
];
}
diff --git a/app/Http/Requests/Request.php b/app/Http/Requests/Request.php
index 8a137ff254..78a741cb1e 100644
--- a/app/Http/Requests/Request.php
+++ b/app/Http/Requests/Request.php
@@ -13,6 +13,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Requests;
+use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;
/**
@@ -22,12 +23,69 @@ use Illuminate\Foundation\Http\FormRequest;
*/
class Request extends FormRequest
{
+ /**
+ * @param string $field
+ *
+ * @return bool
+ */
+ protected function boolean(string $field): bool
+ {
+ return intval($this->input($field)) === 1;
+ }
+
+ /**
+ * @param string $field
+ *
+ * @return Carbon|null
+ */
+ protected function date(string $field)
+ {
+ return $this->get($field) ? new Carbon($this->get($field)) : null;
+ }
+
+ /**
+ * @param string $field
+ *
+ * @return float
+ */
+ protected function float(string $field): float
+ {
+ return round($this->input($field), 12);
+ }
+
+ /**
+ * @param string $field
+ * @param string $type
+ *
+ * @return array
+ */
+ protected function getArray(string $field, string $type): array
+ {
+ $original = $this->get($field);
+ $return = [];
+ foreach ($original as $index => $value) {
+ $return[$index] = $this->$type($value);
+ }
+
+ return $return;
+ }
+
+ /**
+ * @param string $field
+ *
+ * @return int
+ */
+ protected function integer(string $field): int
+ {
+ return intval($this->get($field));
+ }
+
/**
* @param string $field
*
* @return string
*/
- protected function getFieldOrEmptyString(string $field): string
+ protected function string(string $field): string
{
$string = $this->get($field) ?? '';
diff --git a/app/Http/Requests/RuleFormRequest.php b/app/Http/Requests/RuleFormRequest.php
index 5906d62967..6295bd9e3f 100644
--- a/app/Http/Requests/RuleFormRequest.php
+++ b/app/Http/Requests/RuleFormRequest.php
@@ -38,17 +38,17 @@ class RuleFormRequest extends Request
public function getRuleData(): array
{
return [
- 'title' => $this->getFieldOrEmptyString('title'),
- 'active' => intval($this->get('active')) == 1,
- 'trigger' => $this->getFieldOrEmptyString('trigger'),
- 'description' => $this->getFieldOrEmptyString('description'),
+ 'title' => $this->string('title'),
+ 'active' => $this->boolean('active'),
+ 'trigger' => $this->string('trigger'),
+ 'description' => $this->string('description'),
'rule-triggers' => $this->get('rule-trigger'),
'rule-trigger-values' => $this->get('rule-trigger-value'),
'rule-trigger-stop' => $this->get('rule-trigger-stop'),
'rule-actions' => $this->get('rule-action'),
'rule-action-values' => $this->get('rule-action-value'),
'rule-action-stop' => $this->get('rule-action-stop'),
- 'stop_processing' => intval($this->get('stop_processing')) === 1,
+ 'stop_processing' => $this->boolean('stop_processing'),
];
}
diff --git a/app/Http/Requests/RuleGroupFormRequest.php b/app/Http/Requests/RuleGroupFormRequest.php
index ca0de4c947..fcf92d0105 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' => $this->getFieldOrEmptyString('title'),
- 'description' => $this->getFieldOrEmptyString('description'),
+ 'title' => $this->string('title'),
+ 'description' => $this->string('description'),
];
}
diff --git a/app/Http/Requests/SplitJournalFormRequest.php b/app/Http/Requests/SplitJournalFormRequest.php
index 2bf7100677..5efba69243 100644
--- a/app/Http/Requests/SplitJournalFormRequest.php
+++ b/app/Http/Requests/SplitJournalFormRequest.php
@@ -13,8 +13,6 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Requests;
-use Carbon\Carbon;
-
/**
* Class SplitJournalFormRequest
@@ -38,18 +36,18 @@ class SplitJournalFormRequest extends Request
public function getSplitData(): array
{
$data = [
- 'id' => $this->get('id') ?? 0,
- 'journal_description' => $this->get('journal_description'),
- 'journal_currency_id' => intval($this->get('journal_currency_id')),
- 'journal_source_account_id' => intval($this->get('journal_source_account_id')),
- 'journal_source_account_name' => $this->get('journal_source_account_name'),
- 'journal_destination_account_id' => intval($this->get('journal_destination_account_id')),
- 'journal_destination_account_name' => $this->get('journal_source_destination_name'),
- 'date' => new Carbon($this->get('date')),
- 'what' => $this->get('what'),
- 'interest_date' => $this->get('interest_date') ? new Carbon($this->get('interest_date')) : null,
- 'book_date' => $this->get('book_date') ? new Carbon($this->get('book_date')) : null,
- 'process_date' => $this->get('process_date') ? new Carbon($this->get('process_date')) : null,
+ 'id' => $this->integer('id'),
+ 'journal_description' => $this->string('journal_description'),
+ 'journal_currency_id' => $this->integer('journal_currency_id'),
+ 'journal_source_account_id' => $this->integer('journal_source_account_id'),
+ 'journal_source_account_name' => $this->string('journal_source_account_name'),
+ 'journal_destination_account_id' => $this->integer('journal_destination_account_id'),
+ 'journal_destination_account_name' => $this->string('journal_source_destination_name'),
+ 'date' => $this->date('date'),
+ 'what' => $this->string('what'),
+ 'interest_date' => $this->date('interest_date'),
+ 'book_date' => $this->date('book_date'),
+ 'process_date' => $this->date('process_date'),
'transactions' => $this->getTransactionData(),
];
@@ -87,28 +85,30 @@ class SplitJournalFormRequest extends Request
*/
private function getTransactionData(): array
{
+ $descriptions = $this->getArray('description', 'string');
+ $categories = $this->getArray('category', 'string');
+ $amounts = $this->getArray('amount', 'float');
+ $budgets = $this->getArray('amount', 'integer');
+ $srcAccountIds = $this->getArray('source_account_id', 'integer');
+ $srcAccountNames = $this->getArray('source_account_name', 'string');
+ $dstAccountIds = $this->getArray('destination_account_id', 'integer');
+ $dstAccountNames = $this->getArray('destination_account_name', 'string');
+ $piggyBankIds = $this->getArray('piggy_bank_id', 'integer');
+
$return = [];
// description is leading because it is one of the mandatory fields.
- foreach ($this->get('description') as $index => $description) {
- $category = $this->get('category')[$index] ?? '';
+ foreach ($descriptions as $index => $description) {
+ $category = $categories[$index] ?? '';
$transaction = [
'description' => $description,
- 'amount' => round($this->get('amount')[$index], 12),
- 'budget_id' => $this->get('budget_id')[$index] ? intval($this->get('budget_id')[$index]) : 0,
- 'category' => trim($category),
- 'source_account_id' => isset($this->get('source_account_id')[$index])
- ? intval($this->get('source_account_id')[$index])
- : intval(
- $this->get('journal_source_account_id')
- ),
- 'source_account_name' => $this->get('source_account_name')[$index] ?? '',
- 'piggy_bank_id' => isset($this->get('piggy_bank_id')[$index]) ? intval($this->get('piggy_bank_id')[$index]) : 0,
- 'destination_account_id' => isset($this->get('destination_account_id')[$index])
- ? intval($this->get('destination_account_id')[$index])
- : intval(
- $this->get('journal_destination_account_id')
- ),
- 'destination_account_name' => $this->get('destination_account_name')[$index] ?? '',
+ 'amount' => $amounts[$index],
+ 'budget_id' => $budgets[$index] ?? 0,
+ 'category' => $category,
+ 'source_account_id' => $srcAccountIds[$index] ?? $this->get('journal_source_account_id'),
+ 'source_account_name' => $srcAccountNames[$index] ?? '',
+ 'piggy_bank_id' => $piggyBankIds[$index] ?? 0,
+ 'destination_account_id' => $dstAccountIds[$index] ?? $this->get('journal_destination_account_id'),
+ 'destination_account_name' => $dstAccountNames[$index] ?? '',
];
$return[] = $transaction;
}
diff --git a/app/Http/Requests/TagFormRequest.php b/app/Http/Requests/TagFormRequest.php
index d7bdb63f54..544b53c0b0 100644
--- a/app/Http/Requests/TagFormRequest.php
+++ b/app/Http/Requests/TagFormRequest.php
@@ -38,9 +38,9 @@ class TagFormRequest extends Request
public function collectTagData(): array
{
if ($this->get('setTag') == 'true') {
- $latitude = $this->get('latitude');
- $longitude = $this->get('longitude');
- $zoomLevel = $this->get('zoomLevel');
+ $latitude = $this->string('latitude');
+ $longitude = $this->string('longitude');
+ $zoomLevel = $this->integer('zoomLevel');
} else {
$latitude = null;
$longitude = null;
@@ -49,13 +49,13 @@ class TagFormRequest extends Request
$date = $this->get('date') ?? '';
$data = [
- 'tag' => $this->get('tag'),
- 'date' => strlen($date) > 0 ? new Carbon($date) : null,
- 'description' => $this->get('description') ?? '',
+ 'tag' => $this->string('tag'),
+ 'date' => $this->date($date),
+ 'description' => $this->string('description'),
'latitude' => $latitude,
'longitude' => $longitude,
'zoomLevel' => $zoomLevel,
- 'tagMode' => $this->get('tagMode'),
+ 'tagMode' => $this->string('tagMode'),
];
return $data;
diff --git a/app/Http/Requests/UserFormRequest.php b/app/Http/Requests/UserFormRequest.php
index faf12136e1..436e3c9d33 100644
--- a/app/Http/Requests/UserFormRequest.php
+++ b/app/Http/Requests/UserFormRequest.php
@@ -36,11 +36,10 @@ class UserFormRequest extends Request
public function getUserData(): array
{
return [
- 'email' => trim($this->get('email')),
- 'blocked' => intval($this->get('blocked')),
- 'blocked_code' => trim($this->get('blocked_code')),
- 'password' => trim($this->get('password')),
-
+ 'email' => $this->string('email'),
+ 'blocked' => $this->integer('blocked'),
+ 'blocked_code' => $this->string('blocked_code'),
+ 'password' => $this->string('password'),
];
}