mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-22 12:11:19 +00:00
First basic remodelling of request classes
This commit is contained in:
153
app/Support/Request/AppendsLocationData.php
Normal file
153
app/Support/Request/AppendsLocationData.php
Normal file
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
/**
|
||||
* AppendsLocationData.php
|
||||
* Copyright (c) 2020 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Request;
|
||||
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Trait AppendsLocationData
|
||||
*/
|
||||
trait AppendsLocationData
|
||||
{
|
||||
/**
|
||||
* Abstract method stolen from "InteractsWithInput".
|
||||
*
|
||||
* @param null $key
|
||||
* @param bool $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function boolean($key = null, $default = false);
|
||||
|
||||
/**
|
||||
* Abstract method.
|
||||
*
|
||||
* @param $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function has($key);
|
||||
|
||||
/**
|
||||
* Abstract method.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function method();
|
||||
|
||||
/**
|
||||
* Abstract method.
|
||||
*
|
||||
* @param mixed ...$patterns
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function routeIs(...$patterns);
|
||||
|
||||
/**
|
||||
* Read the submitted Request data and add new or updated Location data to the array.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @param string|null $prefix
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function appendLocationData(array $data, ?string $prefix): array
|
||||
{
|
||||
Log::debug(sprintf('Now in appendLocationData("%s")', $prefix), $data);
|
||||
$data['store_location'] = false;
|
||||
$data['update_location'] = false;
|
||||
$data['longitude'] = null;
|
||||
$data['latitude'] = null;
|
||||
$data['zoom_level'] = null;
|
||||
|
||||
$longitudeKey = $this->getLocationKey($prefix, 'longitude');
|
||||
$latitudeKey = $this->getLocationKey($prefix, 'latitude');
|
||||
$zoomLevelKey = $this->getLocationKey($prefix, 'zoom_level');
|
||||
$hasLocationKey = $this->getLocationKey($prefix, 'has_location');
|
||||
$hasLocation = $this->boolean($hasLocationKey);
|
||||
|
||||
// for a POST (store), all fields must be present and accounted for:
|
||||
if (
|
||||
('POST' === $this->method() && $this->routeIs('*.store'))
|
||||
&& ($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey))
|
||||
) {
|
||||
Log::debug('Method is POST and all fields present.');
|
||||
$data['store_location'] = $this->boolean($hasLocationKey);
|
||||
$data['longitude'] = $this->nullableString($longitudeKey);
|
||||
$data['latitude'] = $this->nullableString($latitudeKey);
|
||||
$data['zoom_level'] = $this->nullableString($zoomLevelKey);
|
||||
}
|
||||
if (
|
||||
($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey))
|
||||
&& (
|
||||
('PUT' === $this->method() && $this->routeIs('*.update'))
|
||||
|| ('POST' === $this->method() && $this->routeIs('*.update'))
|
||||
)
|
||||
) {
|
||||
Log::debug('Method is PUT and all fields present.');
|
||||
$data['update_location'] = true;
|
||||
$data['longitude'] = $this->nullableString($longitudeKey);
|
||||
$data['latitude'] = $this->nullableString($latitudeKey);
|
||||
$data['zoom_level'] = $this->nullableString($zoomLevelKey);
|
||||
}
|
||||
if (false === $hasLocation || null === $data['longitude'] || null === $data['latitude'] || null === $data['zoom_level']) {
|
||||
Log::debug('One of the fields is NULL or hasLocation is false, wont save.');
|
||||
$data['store_location'] = false;
|
||||
$data['update_location'] = true; // update is always true, but the values are null:
|
||||
$data['longitude'] = null;
|
||||
$data['latitude'] = null;
|
||||
$data['zoom_level'] = null;
|
||||
}
|
||||
Log::debug(sprintf('Returning longitude: "%s", latitude: "%s", zoom level: "%s"', $data['longitude'], $data['latitude'], $data['zoom_level']));
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method to ensure filling later.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
abstract protected function nullableString(string $field): ?string;
|
||||
|
||||
/**
|
||||
* @param string|null $prefix
|
||||
* @param string $key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getLocationKey(?string $prefix, string $key): string
|
||||
{
|
||||
if (null === $prefix) {
|
||||
return $key;
|
||||
}
|
||||
|
||||
return sprintf('%s_%s', $prefix, $key);
|
||||
}
|
||||
|
||||
}
|
32
app/Support/Request/ConvertAPIDataTypes.php
Normal file
32
app/Support/Request/ConvertAPIDataTypes.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* ConvertAPIDataTypes.php
|
||||
* Copyright (c) 2020 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Request;
|
||||
|
||||
/**
|
||||
* Trait ConvertAPIDataTypes
|
||||
*/
|
||||
trait ConvertAPIDataTypes
|
||||
{
|
||||
|
||||
}
|
147
app/Support/Request/ConvertsDataTypes.php
Normal file
147
app/Support/Request/ConvertsDataTypes.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
/**
|
||||
* ConvertsDataTypes.php
|
||||
* Copyright (c) 2020 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Request;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Trait ConvertsDataTypes
|
||||
*/
|
||||
trait ConvertsDataTypes
|
||||
{
|
||||
|
||||
/**
|
||||
* Return date or NULL.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return Carbon|null
|
||||
*/
|
||||
protected function date(string $field): ?Carbon
|
||||
{
|
||||
$result = null;
|
||||
try {
|
||||
$result = $this->get($field) ? new Carbon($this->get($field)) : null;
|
||||
} catch (Exception $e) {
|
||||
Log::debug(sprintf('Exception when parsing date. Not interesting: %s', $e->getMessage()));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return integer value.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function integer(string $field): int
|
||||
{
|
||||
return (int) $this->get($field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return string value, but keep newlines.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function nlString(string $field): string
|
||||
{
|
||||
return app('steam')->nlCleanString((string) ($this->get($field) ?? ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return integer value, or NULL when it's not set.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
protected function nullableInteger(string $field): ?int
|
||||
{
|
||||
if (!$this->has($field)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$value = (string) $this->get($field);
|
||||
if ('' === $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (int) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return string value, but keep newlines, or NULL if empty.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function nullableNlString(string $field): ?string
|
||||
{
|
||||
if (!$this->has($field)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return app('steam')->nlCleanString((string) ($this->get($field) ?? ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return string value, or NULL if empty.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
protected function nullableString(string $field): ?string
|
||||
{
|
||||
if (!$this->has($field)) {
|
||||
return null;
|
||||
}
|
||||
$res = trim(app('steam')->cleanString((string) ($this->get($field) ?? '')));
|
||||
if ('' === $res) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return string value.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function string(string $field): string
|
||||
{
|
||||
return app('steam')->cleanString((string) ($this->get($field) ?? ''));
|
||||
}
|
||||
}
|
63
app/Support/Request/GetRecurrenceData.php
Normal file
63
app/Support/Request/GetRecurrenceData.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* GetRecurrenceData.php
|
||||
* Copyright (c) 2020 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Request;
|
||||
|
||||
/**
|
||||
* Trait GetRecurrenceData
|
||||
*/
|
||||
trait GetRecurrenceData
|
||||
{
|
||||
/**
|
||||
* @param array $transaction
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getSingleRecurrenceData(array $transaction): array
|
||||
{
|
||||
return [
|
||||
'amount' => $transaction['amount'],
|
||||
'currency_id' => isset($transaction['currency_id']) ? (int) $transaction['currency_id'] : null,
|
||||
'currency_code' => $transaction['currency_code'] ?? null,
|
||||
'foreign_amount' => $transaction['foreign_amount'] ?? null,
|
||||
'foreign_currency_id' => isset($transaction['foreign_currency_id']) ? (int) $transaction['foreign_currency_id'] : null,
|
||||
'foreign_currency_code' => $transaction['foreign_currency_code'] ?? null,
|
||||
'source_id' => isset($transaction['source_id']) ? (int) $transaction['source_id'] : null,
|
||||
'source_name' => isset($transaction['source_name']) ? (string) $transaction['source_name'] : null,
|
||||
'destination_id' => isset($transaction['destination_id']) ? (int) $transaction['destination_id'] : null,
|
||||
'destination_name' => isset($transaction['destination_name']) ? (string) $transaction['destination_name'] : null,
|
||||
'description' => $transaction['description'],
|
||||
'type' => $this->string('type'),
|
||||
|
||||
// new and updated fields:
|
||||
'piggy_bank_id' => isset($transaction['piggy_bank_id']) ? (int) $transaction['piggy_bank_id'] : null,
|
||||
'piggy_bank_name' => $transaction['piggy_bank_name'] ?? null,
|
||||
'tags' => $transaction['tags'] ?? [],
|
||||
'budget_id' => isset($transaction['budget_id']) ? (int) $transaction['budget_id'] : null,
|
||||
'budget_name' => $transaction['budget_name'] ?? null,
|
||||
'category_id' => isset($transaction['category_id']) ? (int) $transaction['category_id'] : null,
|
||||
'category_name' => $transaction['category_name'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user