New method for consistent data collection.

This commit is contained in:
James Cole 2020-12-02 17:12:58 +01:00
parent cbe046ba07
commit 35c9367819
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
4 changed files with 57 additions and 20 deletions

View File

@ -49,7 +49,8 @@ class PiggyBankUpdateRequest extends FormRequest
// TODO this should be the way to collect fields for all API things.
// TODO make sure piggy bank uses 'start_date' etc. until right up to DB update.
// TODO can we configure this and return it from config?
$return = [];
// TODO this is the way.
$fields = [
'name' => ['name', 'string'],
'account_id' => ['account_id', 'integer'],
@ -62,14 +63,7 @@ class PiggyBankUpdateRequest extends FormRequest
'object_group' => ['object_group', 'string'],
'object_group_id' => ['object_group_id', 'integer'],
];
foreach ($fields as $field => $info) {
if ($this->has($info[0])) {
$method = $info[1];
$return[$field] = $this->$method($info[0]);
}
}
return $return;
return $this->getAllData($fields);
}
/**

View File

@ -42,12 +42,23 @@ class CreateRequest extends FormRequest
$responses = array_flip(config('firefly.webhooks.responses'));
$deliveries = array_flip(config('firefly.webhooks.deliveries'));
$fields = [
'active' => ['active', 'boolean'],
'trigger' => ['trigger', 'string'],
'response' => ['response', 'string'],
'delivery' => ['delivery', 'string'],
'url' => ['url', 'string'],
];
// this is the way.
$return = $this->getAllData($fields);
return [
'active' => $this->boolean('active'),
'trigger' => $triggers[$this->string('trigger')] ?? 0,
'response' => $responses[$this->string('response')] ?? 0,
'delivery' => $deliveries[$this->string('delivery')] ?? 0,
'url' => $this->string('url'),
'active' => $return['active'],
'trigger' => $triggers[$return['trigger']] ?? 0,
'response' => $responses[$return['response']] ?? 0,
'delivery' => $deliveries[$return['delivery']] ?? 0,
'url' => $return['url'],
];
}

View File

@ -42,12 +42,23 @@ class UpdateRequest extends FormRequest
$responses = array_flip(config('firefly.webhooks.responses'));
$deliveries = array_flip(config('firefly.webhooks.deliveries'));
$fields = [
'active' => ['active', 'boolean'],
'trigger' => ['trigger', 'string'],
'response' => ['response', 'string'],
'delivery' => ['delivery', 'string'],
'url' => ['url', 'string'],
];
// this is the way.
$return = $this->getAllData($fields);
return [
'active' => $this->boolean('active'),
'trigger' => $triggers[$this->string('trigger')] ?? 0,
'response' => $responses[$this->string('response')] ?? 0,
'delivery' => $deliveries[$this->string('delivery')] ?? 0,
'url' => $this->string('url'),
'active' => $return['active'],
'trigger' => $triggers[$return['trigger']] ?? 0,
'response' => $responses[$return['response']] ?? 0,
'delivery' => $deliveries[$return['delivery']] ?? 0,
'url' => $return['url'],
];
}

View File

@ -33,6 +33,27 @@ use Log;
trait ConvertsDataTypes
{
/**
* Returns all data in the request, or omits the field if not set,
* according to the config from the request. This is the way.
*
* @param array $fields
*
* @return array
*/
protected function getAllData(array $fields): array
{
$return = [];
foreach ($fields as $field => $info) {
if ($this->has($info[0])) {
$method = $info[1];
$return[$field] = $this->$method($info[0]);
}
}
return $return;
}
/**
* Return date or NULL.
*