Clean up form requests

This commit is contained in:
James Cole
2020-10-24 07:55:09 +02:00
parent ab4bcd3703
commit dbc878950c
33 changed files with 265 additions and 211 deletions

View File

@@ -29,24 +29,26 @@ use FireflyIII\Models\Recurrence;
use FireflyIII\Models\TransactionType;
use FireflyIII\Rules\ValidRecurrenceRepetitionType;
use FireflyIII\Rules\ValidRecurrenceRepetitionValue;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\Validation\AccountValidator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use Log;
/**
* Class RecurrenceFormRequest
*/
class RecurrenceFormRequest extends LoggedInRequest
class RecurrenceFormRequest extends FormRequest
{
use ConvertsDataTypes;
use ConvertsDataTypes, ChecksLogin;
/**
* Get the data required by the controller.
*
* @return array
* @throws FireflyException
*
* @return array
*/
public function getAll(): array
{
@@ -125,12 +127,48 @@ class RecurrenceFormRequest extends LoggedInRequest
}
/**
* The rules for this request.
*
* @throws FireflyException
* Parses repetition data.
*
* @return array
*/
private function parseRepetitionData(): array
{
$value = $this->string('repetition_type');
$return = [
'type' => '',
'moment' => '',
];
if ('daily' === $value) {
$return['type'] = $value;
}
//monthly,17
//ndom,3,7
if (in_array(substr($value, 0, 6), ['yearly', 'weekly'])) {
$return['type'] = substr($value, 0, 6);
$return['moment'] = substr($value, 7);
}
if (0 === strpos($value, 'monthly')) {
$return['type'] = substr($value, 0, 7);
$return['moment'] = substr($value, 8);
}
if (0 === strpos($value, 'ndom')) {
$return['type'] = substr($value, 0, 4);
$return['moment'] = substr($value, 5);
}
return $return;
}
/**
* The rules for this request.
*
* @return array
* @throws FireflyException
*
*/
public function rules(): array
{
$today = today(config('app.timezone'));
@@ -216,6 +254,23 @@ class RecurrenceFormRequest extends LoggedInRequest
return $rules;
}
/**
* Configure the validator instance with special rules for after the basic validation rules.
*
* @param Validator $validator
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
// validate all account info
$this->validateAccountInformation($validator);
}
);
}
/**
* Validates the given account information. Switches on given transaction type.
*
@@ -243,16 +298,16 @@ class RecurrenceFormRequest extends LoggedInRequest
default:
throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->string('transaction_type'))); // @codeCoverageIgnore
case 'withdrawal':
$sourceId = (int) $data['source_id'];
$destinationId = (int) $data['withdrawal_destination_id'];
$sourceId = (int)$data['source_id'];
$destinationId = (int)$data['withdrawal_destination_id'];
break;
case 'deposit':
$sourceId = (int) $data['deposit_source_id'];
$destinationId = (int) $data['destination_id'];
$sourceId = (int)$data['deposit_source_id'];
$destinationId = (int)$data['destination_id'];
break;
case 'transfer':
$sourceId = (int) $data['source_id'];
$destinationId = (int) $data['destination_id'];
$sourceId = (int)$data['source_id'];
$destinationId = (int)$data['destination_id'];
break;
}
@@ -262,7 +317,7 @@ class RecurrenceFormRequest extends LoggedInRequest
// do something with result:
if (false === $validSource) {
$message = (string) trans('validation.generic_invalid_source');
$message = (string)trans('validation.generic_invalid_source');
$validator->errors()->add('source_id', $message);
$validator->errors()->add('deposit_source_id', $message);
@@ -273,64 +328,11 @@ class RecurrenceFormRequest extends LoggedInRequest
$validDestination = $accountValidator->validateDestination($destinationId, null, null);
// do something with result:
if (false === $validDestination) {
$message = (string) trans('validation.generic_invalid_destination');
$message = (string)trans('validation.generic_invalid_destination');
$validator->errors()->add('destination_id', $message);
$validator->errors()->add('withdrawal_destination_id', $message);
return;
}
}
/**
* Configure the validator instance with special rules for after the basic validation rules.
*
* @param Validator $validator
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
// validate all account info
$this->validateAccountInformation($validator);
}
);
}
/**
* Parses repetition data.
*
* @return array
*/
private function parseRepetitionData(): array
{
$value = $this->string('repetition_type');
$return = [
'type' => '',
'moment' => '',
];
if ('daily' === $value) {
$return['type'] = $value;
}
//monthly,17
//ndom,3,7
if (in_array(substr($value, 0, 6), ['yearly', 'weekly'])) {
$return['type'] = substr($value, 0, 6);
$return['moment'] = substr($value, 7);
}
if (0 === strpos($value, 'monthly')) {
$return['type'] = substr($value, 0, 7);
$return['moment'] = substr($value, 8);
}
if (0 === strpos($value, 'ndom')) {
$return['type'] = substr($value, 0, 4);
$return['moment'] = substr($value, 5);
}
return $return;
}
}