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

View File

@ -42,12 +42,23 @@ class CreateRequest extends FormRequest
$responses = array_flip(config('firefly.webhooks.responses')); $responses = array_flip(config('firefly.webhooks.responses'));
$deliveries = array_flip(config('firefly.webhooks.deliveries')); $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 [ return [
'active' => $this->boolean('active'), 'active' => $return['active'],
'trigger' => $triggers[$this->string('trigger')] ?? 0, 'trigger' => $triggers[$return['trigger']] ?? 0,
'response' => $responses[$this->string('response')] ?? 0, 'response' => $responses[$return['response']] ?? 0,
'delivery' => $deliveries[$this->string('delivery')] ?? 0, 'delivery' => $deliveries[$return['delivery']] ?? 0,
'url' => $this->string('url'), 'url' => $return['url'],
]; ];
} }

View File

@ -42,12 +42,23 @@ class UpdateRequest extends FormRequest
$responses = array_flip(config('firefly.webhooks.responses')); $responses = array_flip(config('firefly.webhooks.responses'));
$deliveries = array_flip(config('firefly.webhooks.deliveries')); $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 [ return [
'active' => $this->boolean('active'), 'active' => $return['active'],
'trigger' => $triggers[$this->string('trigger')] ?? 0, 'trigger' => $triggers[$return['trigger']] ?? 0,
'response' => $responses[$this->string('response')] ?? 0, 'response' => $responses[$return['response']] ?? 0,
'delivery' => $deliveries[$this->string('delivery')] ?? 0, 'delivery' => $deliveries[$return['delivery']] ?? 0,
'url' => $this->string('url'), 'url' => $return['url'],
]; ];
} }

View File

@ -33,6 +33,27 @@ use Log;
trait ConvertsDataTypes 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. * Return date or NULL.
* *
@ -142,7 +163,7 @@ trait ConvertsDataTypes
return null; return null;
} }
Log::debug(sprintf('Date object: %s (%s)',$carbon->toW3cString() , $carbon->getTimezone())); Log::debug(sprintf('Date object: %s (%s)', $carbon->toW3cString(), $carbon->getTimezone()));
return $carbon; return $carbon;
} }