Make request code more uniform.

This commit is contained in:
James Cole 2017-01-21 08:32:23 +01:00
parent d1d573c408
commit 71f6ba3418
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
15 changed files with 172 additions and 129 deletions

View File

@ -13,7 +13,6 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Requests; namespace FireflyIII\Http\Requests;
use Carbon\Carbon;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/** /**
@ -39,21 +38,21 @@ class AccountFormRequest extends Request
public function getAccountData(): array public function getAccountData(): array
{ {
return [ return [
'name' => $this->getFieldOrEmptyString('name'), 'name' => $this->string('name'),
'active' => intval($this->input('active')) === 1, 'active' => $this->boolean('active'),
'accountType' => $this->getFieldOrEmptyString('what'), 'accountType' => $this->string('what'),
'currency_id' => intval($this->input('currency_id')), 'currency_id' => $this->integer('currency_id'),
'virtualBalance' => round($this->input('virtualBalance'), 12), 'virtualBalance' => $this->float('virtualBalance'),
'virtualBalanceCurrency' => intval($this->input('amount_currency_id_virtualBalance')), 'virtualBalanceCurrency' => $this->integer('amount_currency_id_virtualBalance'),
'iban' => $this->getFieldOrEmptyString('iban'), 'iban' => $this->string('iban'),
'BIC' => $this->getFieldOrEmptyString('BIC'), 'BIC' => $this->string('BIC'),
'accountNumber' => $this->getFieldOrEmptyString('accountNumber'), 'accountNumber' => $this->string('accountNumber'),
'accountRole' => $this->getFieldOrEmptyString('accountRole'), 'accountRole' => $this->string('accountRole'),
'openingBalance' => round($this->input('openingBalance'), 12), 'openingBalance' => $this->float('openingBalance'),
'openingBalanceDate' => new Carbon((string)$this->input('openingBalanceDate')), 'openingBalanceDate' => $this->date('openingBalanceDate'),
'openingBalanceCurrency' => intval($this->input('amount_currency_id_openingBalance')), 'openingBalanceCurrency' => $this->integer('amount_currency_id_openingBalance'),
'ccType' => $this->getFieldOrEmptyString('ccType'), 'ccType' => $this->string('ccType'),
'ccMonthlyPaymentDate' => $this->getFieldOrEmptyString('ccMonthlyPaymentDate'), 'ccMonthlyPaymentDate' => $this->string('ccMonthlyPaymentDate'),
]; ];
} }

View File

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

View File

@ -13,8 +13,6 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Requests; namespace FireflyIII\Http\Requests;
use Carbon\Carbon;
/** /**
* Class BillFormRequest * Class BillFormRequest
* *
@ -38,17 +36,17 @@ class BillFormRequest extends Request
public function getBillData() public function getBillData()
{ {
return [ return [
'name' => $this->getFieldOrEmptyString('name'), 'name' => $this->string('name'),
'match' => $this->getFieldOrEmptyString('match'), 'match' => $this->string('match'),
'amount_min' => round($this->get('amount_min'), 12), 'amount_min' => $this->float('amount_min'),
'amount_currency_id_amount_min' => intval($this->get('amount_currency_id_amount_min')), 'amount_currency_id_amount_min' => $this->integer('amount_currency_id_amount_min'),
'amount_currency_id_amount_max' => intval($this->get('amount_currency_id_amount_max')), 'amount_currency_id_amount_max' => $this->integer('amount_currency_id_amount_max'),
'amount_max' => round($this->get('amount_max'), 12), 'amount_max' => $this->float('amount_max'),
'date' => new Carbon($this->get('date')), 'date' => $this->date('date'),
'repeat_freq' => $this->getFieldOrEmptyString('repeat_freq'), 'repeat_freq' => $this->string('repeat_freq'),
'skip' => intval($this->get('skip')), 'skip' => $this->integer('skip'),
'automatch' => intval($this->get('automatch')) === 1, 'automatch' => $this->boolean('automatch'),
'active' => intval($this->get('active')) === 1, 'active' => $this->boolean('active'),
]; ];
} }

View File

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

View File

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

View File

@ -36,8 +36,8 @@ class ConfigurationRequest extends Request
public function getConfigurationData(): array public function getConfigurationData(): array
{ {
return [ return [
'single_user_mode' => intval($this->get('single_user_mode')) === 1, 'single_user_mode' => $this->boolean('single_user_mode'),
'is_demo_site' => intval($this->get('is_demo_site')) === 1, 'is_demo_site' => $this->boolean('is_demo_site'),
]; ];
} }

View File

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

View File

@ -13,7 +13,6 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Requests; namespace FireflyIII\Http\Requests;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
@ -43,30 +42,30 @@ class JournalFormRequest extends Request
{ {
$data = [ $data = [
'what' => $this->get('what'), // type. can be 'deposit', 'withdrawal' or 'transfer' 'what' => $this->get('what'), // type. can be 'deposit', 'withdrawal' or 'transfer'
'date' => new Carbon($this->get('date')), 'date' => $this->date('date'),
'tags' => explode(',', $this->getFieldOrEmptyString('tags')), 'tags' => explode(',', $this->string('tags')),
'currency_id' => intval($this->get('amount_currency_id_amount')), 'currency_id' => $this->integer('amount_currency_id_amount'),
// all custom fields: // all custom fields:
'interest_date' => $this->getDateOrNull('interest_date'), 'interest_date' => $this->date('interest_date'),
'book_date' => $this->getDateOrNull('book_date'), 'book_date' => $this->date('book_date'),
'process_date' => $this->getDateOrNull('process_date'), 'process_date' => $this->date('process_date'),
'due_date' => $this->getDateOrNull('due_date'), 'due_date' => $this->date('due_date'),
'payment_date' => $this->getDateOrNull('payment_date'), 'payment_date' => $this->date('payment_date'),
'invoice_date' => $this->getDateOrNull('invoice_date'), 'invoice_date' => $this->date('invoice_date'),
'internal_reference' => $this->getFieldOrEmptyString('internal_reference'), 'internal_reference' => $this->string('internal_reference'),
'notes' => $this->getFieldOrEmptyString('notes'), 'notes' => $this->string('notes'),
// transaction / journal data: // transaction / journal data:
'description' => $this->getFieldOrEmptyString('description'), 'description' => $this->string('description'),
'amount' => round($this->get('amount'), 12), 'amount' => $this->float('amount'),
'budget_id' => intval($this->get('budget_id')), 'budget_id' => $this->integer('budget_id'),
'category' => $this->getFieldOrEmptyString('category'), 'category' => $this->string('category'),
'source_account_id' => intval($this->get('source_account_id')), 'source_account_id' => $this->integer('source_account_id'),
'source_account_name' => $this->getFieldOrEmptyString('source_account_name'), 'source_account_name' => $this->string('source_account_name'),
'destination_account_id' => $this->getFieldOrEmptyString('destination_account_id'), 'destination_account_id' => $this->string('destination_account_id'),
'destination_account_name' => $this->getFieldOrEmptyString('destination_account_name'), 'destination_account_name' => $this->string('destination_account_name'),
'piggy_bank_id' => intval($this->get('piggy_bank_id')), 'piggy_bank_id' => $this->integer('piggy_bank_id'),
]; ];
@ -142,14 +141,4 @@ class JournalFormRequest extends Request
return $rules; return $rules;
} }
/**
* @param string $field
*
* @return Carbon|null
*/
private function getDateOrNull(string $field)
{
return $this->get($field) ? new Carbon($this->get($field)) : null;
}
} }

View File

@ -38,12 +38,12 @@ class PiggyBankFormRequest extends Request
public function getPiggyBankData(): array public function getPiggyBankData(): array
{ {
return [ return [
'name' => $this->getFieldOrEmptyString('name'), 'name' => $this->string('name'),
'startdate' => new Carbon, 'startdate' => new Carbon,
'account_id' => intval($this->get('account_id')), 'account_id' => $this->integer('account_id'),
'targetamount' => round($this->get('targetamount'), 12), 'targetamount' => $this->float('targetamount'),
'targetdate' => strlen(strval($this->get('targetdate'))) > 0 ? new Carbon($this->get('targetdate')) : null, 'targetdate' => $this->date('targetdate'),
'note' => trim(strval($this->get('note'))), 'note' => $this->string('note'),
]; ];
} }

View File

@ -13,6 +13,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Requests; namespace FireflyIII\Http\Requests;
use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
/** /**
@ -22,12 +23,69 @@ use Illuminate\Foundation\Http\FormRequest;
*/ */
class Request extends 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 * @param string $field
* *
* @return string * @return string
*/ */
protected function getFieldOrEmptyString(string $field): string protected function string(string $field): string
{ {
$string = $this->get($field) ?? ''; $string = $this->get($field) ?? '';

View File

@ -38,17 +38,17 @@ class RuleFormRequest extends Request
public function getRuleData(): array public function getRuleData(): array
{ {
return [ return [
'title' => $this->getFieldOrEmptyString('title'), 'title' => $this->string('title'),
'active' => intval($this->get('active')) == 1, 'active' => $this->boolean('active'),
'trigger' => $this->getFieldOrEmptyString('trigger'), 'trigger' => $this->string('trigger'),
'description' => $this->getFieldOrEmptyString('description'), 'description' => $this->string('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'),
'rule-actions' => $this->get('rule-action'), 'rule-actions' => $this->get('rule-action'),
'rule-action-values' => $this->get('rule-action-value'), 'rule-action-values' => $this->get('rule-action-value'),
'rule-action-stop' => $this->get('rule-action-stop'), 'rule-action-stop' => $this->get('rule-action-stop'),
'stop_processing' => intval($this->get('stop_processing')) === 1, 'stop_processing' => $this->boolean('stop_processing'),
]; ];
} }

View File

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

View File

@ -13,8 +13,6 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Requests; namespace FireflyIII\Http\Requests;
use Carbon\Carbon;
/** /**
* Class SplitJournalFormRequest * Class SplitJournalFormRequest
@ -38,18 +36,18 @@ class SplitJournalFormRequest extends Request
public function getSplitData(): array public function getSplitData(): array
{ {
$data = [ $data = [
'id' => $this->get('id') ?? 0, 'id' => $this->integer('id'),
'journal_description' => $this->get('journal_description'), 'journal_description' => $this->string('journal_description'),
'journal_currency_id' => intval($this->get('journal_currency_id')), 'journal_currency_id' => $this->integer('journal_currency_id'),
'journal_source_account_id' => intval($this->get('journal_source_account_id')), 'journal_source_account_id' => $this->integer('journal_source_account_id'),
'journal_source_account_name' => $this->get('journal_source_account_name'), 'journal_source_account_name' => $this->string('journal_source_account_name'),
'journal_destination_account_id' => intval($this->get('journal_destination_account_id')), 'journal_destination_account_id' => $this->integer('journal_destination_account_id'),
'journal_destination_account_name' => $this->get('journal_source_destination_name'), 'journal_destination_account_name' => $this->string('journal_source_destination_name'),
'date' => new Carbon($this->get('date')), 'date' => $this->date('date'),
'what' => $this->get('what'), 'what' => $this->string('what'),
'interest_date' => $this->get('interest_date') ? new Carbon($this->get('interest_date')) : null, 'interest_date' => $this->date('interest_date'),
'book_date' => $this->get('book_date') ? new Carbon($this->get('book_date')) : null, 'book_date' => $this->date('book_date'),
'process_date' => $this->get('process_date') ? new Carbon($this->get('process_date')) : null, 'process_date' => $this->date('process_date'),
'transactions' => $this->getTransactionData(), 'transactions' => $this->getTransactionData(),
]; ];
@ -87,28 +85,30 @@ class SplitJournalFormRequest extends Request
*/ */
private function getTransactionData(): array 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 = []; $return = [];
// description is leading because it is one of the mandatory fields. // description is leading because it is one of the mandatory fields.
foreach ($this->get('description') as $index => $description) { foreach ($descriptions as $index => $description) {
$category = $this->get('category')[$index] ?? ''; $category = $categories[$index] ?? '';
$transaction = [ $transaction = [
'description' => $description, 'description' => $description,
'amount' => round($this->get('amount')[$index], 12), 'amount' => $amounts[$index],
'budget_id' => $this->get('budget_id')[$index] ? intval($this->get('budget_id')[$index]) : 0, 'budget_id' => $budgets[$index] ?? 0,
'category' => trim($category), 'category' => $category,
'source_account_id' => isset($this->get('source_account_id')[$index]) 'source_account_id' => $srcAccountIds[$index] ?? $this->get('journal_source_account_id'),
? intval($this->get('source_account_id')[$index]) 'source_account_name' => $srcAccountNames[$index] ?? '',
: intval( 'piggy_bank_id' => $piggyBankIds[$index] ?? 0,
$this->get('journal_source_account_id') 'destination_account_id' => $dstAccountIds[$index] ?? $this->get('journal_destination_account_id'),
), 'destination_account_name' => $dstAccountNames[$index] ?? '',
'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] ?? '',
]; ];
$return[] = $transaction; $return[] = $transaction;
} }

View File

@ -38,9 +38,9 @@ class TagFormRequest extends Request
public function collectTagData(): array public function collectTagData(): array
{ {
if ($this->get('setTag') == 'true') { if ($this->get('setTag') == 'true') {
$latitude = $this->get('latitude'); $latitude = $this->string('latitude');
$longitude = $this->get('longitude'); $longitude = $this->string('longitude');
$zoomLevel = $this->get('zoomLevel'); $zoomLevel = $this->integer('zoomLevel');
} else { } else {
$latitude = null; $latitude = null;
$longitude = null; $longitude = null;
@ -49,13 +49,13 @@ class TagFormRequest extends Request
$date = $this->get('date') ?? ''; $date = $this->get('date') ?? '';
$data = [ $data = [
'tag' => $this->get('tag'), 'tag' => $this->string('tag'),
'date' => strlen($date) > 0 ? new Carbon($date) : null, 'date' => $this->date($date),
'description' => $this->get('description') ?? '', 'description' => $this->string('description'),
'latitude' => $latitude, 'latitude' => $latitude,
'longitude' => $longitude, 'longitude' => $longitude,
'zoomLevel' => $zoomLevel, 'zoomLevel' => $zoomLevel,
'tagMode' => $this->get('tagMode'), 'tagMode' => $this->string('tagMode'),
]; ];
return $data; return $data;

View File

@ -36,11 +36,10 @@ class UserFormRequest extends Request
public function getUserData(): array public function getUserData(): array
{ {
return [ return [
'email' => trim($this->get('email')), 'email' => $this->string('email'),
'blocked' => intval($this->get('blocked')), 'blocked' => $this->integer('blocked'),
'blocked_code' => trim($this->get('blocked_code')), 'blocked_code' => $this->string('blocked_code'),
'password' => trim($this->get('password')), 'password' => $this->string('password'),
]; ];
} }