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;
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'),
];
}

View File

@ -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'),
];
}

View File

@ -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'),
];
}

View File

@ -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'),
];
}

View File

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

View File

@ -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'),
];
}

View File

@ -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'),
];
}

View File

@ -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;
}
}

View File

@ -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'),
];
}

View File

@ -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) ?? '';

View File

@ -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'),
];
}

View File

@ -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'),
];
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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'),
];
}