Skip to content

Firefly III: a personal finances manager

New method for consistent data collection.

James Cole35c93678198c
diff --git a/app/Api/V1/Requests/PiggyBankUpdateRequest.php b/app/Api/V1/Requests/PiggyBankUpdateRequest.php
index cf22063559..e62203b4c0 100644
--- a/app/Api/V1/Requests/PiggyBankUpdateRequest.php
+++ b/app/Api/V1/Requests/PiggyBankUpdateRequest.php
@@ -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);
}
/**
diff --git a/app/Api/V1/Requests/Webhook/CreateRequest.php b/app/Api/V1/Requests/Webhook/CreateRequest.php
index 393ca1739a..4f9ff745ce 100644
--- a/app/Api/V1/Requests/Webhook/CreateRequest.php
+++ b/app/Api/V1/Requests/Webhook/CreateRequest.php
@@ -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'],
];
}
diff --git a/app/Api/V1/Requests/Webhook/UpdateRequest.php b/app/Api/V1/Requests/Webhook/UpdateRequest.php
index b9749f9ea5..039d7d6074 100644
--- a/app/Api/V1/Requests/Webhook/UpdateRequest.php
+++ b/app/Api/V1/Requests/Webhook/UpdateRequest.php
@@ -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'],
];
}
diff --git a/app/Support/Request/ConvertsDataTypes.php b/app/Support/Request/ConvertsDataTypes.php
index a4793810d2..695a5d9db6 100644
--- a/app/Support/Request/ConvertsDataTypes.php
+++ b/app/Support/Request/ConvertsDataTypes.php
@@ -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.
*
@@ -142,7 +163,7 @@ trait ConvertsDataTypes
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;
}