First basic remodelling of request classes

This commit is contained in:
James Cole 2020-07-18 08:25:25 +02:00
parent d55cc03edf
commit 427bcaa522
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
40 changed files with 398 additions and 258 deletions

View File

@ -26,14 +26,18 @@ namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Models\Location;
use FireflyIII\Rules\IsBoolean;
use FireflyIII\Support\Request\AppendsLocationData;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class AccountStoreRequest
*
* @codeCoverageIgnore
*/
class AccountStoreRequest extends Request
class AccountStoreRequest extends FormRequest
{
use ConvertsDataTypes, AppendsLocationData;
/**
* Authorize logged in users.

View File

@ -26,15 +26,18 @@ namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Models\Location;
use FireflyIII\Rules\IsBoolean;
use FireflyIII\Support\Request\AppendsLocationData;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class AccountUpdateRequest
*
* @codeCoverageIgnore
*/
class AccountUpdateRequest extends Request
class AccountUpdateRequest extends FormRequest
{
use ConvertsDataTypes, AppendsLocationData;
/**
* Authorize logged in users.
*

View File

@ -24,13 +24,14 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Rules\IsValidAttachmentModel;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class AttachmentStoreRequest
*
* @codeCoverageIgnore
*/
class AttachmentStoreRequest extends Request
class AttachmentStoreRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -23,12 +23,14 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use Illuminate\Foundation\Http\FormRequest;
/**
* ClassAttachmentUpdateRequest
*
* @codeCoverageIgnore
*/
class AttachmentUpdateRequest extends Request
class AttachmentUpdateRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -23,12 +23,14 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class AvailableBudgetRequest
*
* @codeCoverageIgnore
*/
class AvailableBudgetRequest extends Request
class AvailableBudgetRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Rules\IsBoolean;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
/**
@ -34,7 +35,7 @@ use Illuminate\Validation\Validator;
*
* @codeCoverageIgnore
*/
class BillRequest extends Request
class BillRequest extends FormRequest
{
/**

View File

@ -23,13 +23,15 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class BudgetLimitRequest
*
* @codeCoverageIgnore
* TODO AFTER 4.8,0: split this into two request classes.
*/
class BudgetLimitRequest extends Request
class BudgetLimitRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Rules\IsBoolean;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
/**
@ -31,7 +32,7 @@ use Illuminate\Validation\Validator;
*
* @codeCoverageIgnore
*/
class BudgetStoreRequest extends Request
class BudgetStoreRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Rules\IsBoolean;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
/**
@ -31,7 +32,7 @@ use Illuminate\Validation\Validator;
*
* @codeCoverageIgnore
*/
class BudgetUpdateRequest extends Request
class BudgetUpdateRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Models\Category;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class CategoryRequest
@ -31,7 +32,7 @@ use FireflyIII\Models\Category;
* @codeCoverageIgnore
* TODO AFTER 4.8,0: split this into two request classes.
*/
class CategoryRequest extends Request
class CategoryRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -25,13 +25,14 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Rules\IsBoolean;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class ConfigurationRequest
*
* @codeCoverageIgnore
*/
class ConfigurationRequest extends Request
class ConfigurationRequest extends FormRequest
{
/**

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Rules\IsBoolean;
use Illuminate\Foundation\Http\FormRequest;
/**
@ -32,7 +33,7 @@ use FireflyIII\Rules\IsBoolean;
* @codeCoverageIgnore
* TODO AFTER 4.8,0: split this into two request classes.
*/
class CurrencyRequest extends Request
class CurrencyRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -23,10 +23,12 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class DataDestroyRequest
*/
class DataDestroyRequest extends Request
class DataDestroyRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -25,12 +25,14 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use Illuminate\Foundation\Http\FormRequest;
/**
* Request class for end points that require date parameters.
*
* Class DateRequest
*/
class DateRequest extends Request
class DateRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Models\LinkType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
/**
@ -33,7 +34,7 @@ use Illuminate\Validation\Rule;
* @codeCoverageIgnore
* TODO AFTER 4.8,0: split this into two request classes.
*/
class LinkTypeRequest extends Request
class LinkTypeRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -24,12 +24,14 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class AccountObjectGroupUpdateRequestUpdateRequest
*
* @codeCoverageIgnore
*/
class ObjectGroupUpdateRequest extends Request
class ObjectGroupUpdateRequest extends FormRequest
{
/**

View File

@ -27,6 +27,7 @@ use FireflyIII\Models\PiggyBank;
use FireflyIII\Rules\IsAssetAccountId;
use FireflyIII\Rules\LessThanPiggyTarget;
use FireflyIII\Rules\ZeroOrMore;
use Illuminate\Foundation\Http\FormRequest;
/**
*
@ -35,7 +36,7 @@ use FireflyIII\Rules\ZeroOrMore;
* @codeCoverageIgnore
* TODO AFTER 4.8,0: split this into two request classes.
*/
class PiggyBankRequest extends Request
class PiggyBankRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Rules\ZeroOrMore;
use Illuminate\Foundation\Http\FormRequest;
/**
*
@ -31,7 +32,7 @@ use FireflyIII\Rules\ZeroOrMore;
*
* @codeCoverageIgnore
*/
class PiggyBankStoreRequest extends Request
class PiggyBankStoreRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -23,13 +23,15 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use Illuminate\Foundation\Http\FormRequest;
/**
*
* Class PreferenceRequest
*
* @codeCoverageIgnore
*/
class PreferenceRequest extends Request
class PreferenceRequest extends FormRequest
{

View File

@ -26,17 +26,19 @@ namespace FireflyIII\Api\V1\Requests;
use Carbon\Carbon;
use FireflyIII\Rules\BelongsUser;
use FireflyIII\Rules\IsBoolean;
use FireflyIII\Support\Request\GetRecurrenceData;
use FireflyIII\Validation\CurrencyValidation;
use FireflyIII\Validation\RecurrenceValidation;
use FireflyIII\Validation\TransactionValidation;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
/**
* Class RecurrenceStoreRequest
*/
class RecurrenceStoreRequest extends Request
class RecurrenceStoreRequest extends FormRequest
{
use RecurrenceValidation, TransactionValidation, CurrencyValidation;
use RecurrenceValidation, TransactionValidation, CurrencyValidation, GetRecurrenceData;
/**
* Authorize logged in users.

View File

@ -29,12 +29,13 @@ use FireflyIII\Rules\IsBoolean;
use FireflyIII\Validation\CurrencyValidation;
use FireflyIII\Validation\RecurrenceValidation;
use FireflyIII\Validation\TransactionValidation;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
/**
* Class RecurrenceUpdateRequest
*/
class RecurrenceUpdateRequest extends Request
class RecurrenceUpdateRequest extends FormRequest
{
use RecurrenceValidation, TransactionValidation, CurrencyValidation;

View File

@ -25,6 +25,7 @@ namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Rules\IsBoolean;
use Illuminate\Foundation\Http\FormRequest;
/**
@ -32,7 +33,7 @@ use FireflyIII\Rules\IsBoolean;
* Class RuleGroupRequest
* TODO AFTER 4.8,0: split this into two request classes.
*/
class RuleGroupRequest extends Request
class RuleGroupRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -30,13 +30,14 @@ use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
use Log;
/**
* Class RuleGroupTestRequest
*/
class RuleGroupTestRequest extends Request
class RuleGroupTestRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -30,13 +30,14 @@ use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
use Log;
/**
* Class RuleGroupTriggerRequest
*/
class RuleGroupTriggerRequest extends Request
class RuleGroupTriggerRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Rules\IsBoolean;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use function is_array;
@ -32,7 +33,7 @@ use function is_array;
* Class RuleStoreRequest
*
*/
class RuleStoreRequest extends Request
class RuleStoreRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -30,13 +30,14 @@ use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
use Log;
/**
* Class RuleTestRequest
*/
class RuleTestRequest extends Request
class RuleTestRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -29,13 +29,14 @@ use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
use Log;
/**
* Class RuleTriggerRequest
*/
class RuleTriggerRequest extends Request
class RuleTriggerRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Rules\IsBoolean;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use function is_array;
@ -32,7 +33,7 @@ use function is_array;
* Class RuleUpdateRequest
*
*/
class RuleUpdateRequest extends Request
class RuleUpdateRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Models\Location;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class TagStoreRequest
@ -31,7 +32,7 @@ use FireflyIII\Models\Location;
* @codeCoverageIgnore
*
*/
class TagStoreRequest extends Request
class TagStoreRequest extends FormRequest
{
/**

View File

@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Models\Location;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class TagUpdateRequest
@ -32,7 +33,7 @@ use FireflyIII\Models\Location;
* @codeCoverageIgnore
*
*/
class TagUpdateRequest extends Request
class TagUpdateRequest extends FormRequest
{
/**

View File

@ -26,13 +26,14 @@ namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
use FireflyIII\User;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
/**
*
* Class TransactionLinkRequest
*/
class TransactionLinkRequest extends Request
class TransactionLinkRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -31,13 +31,14 @@ use FireflyIII\Support\NullArrayObject;
use FireflyIII\Validation\CurrencyValidation;
use FireflyIII\Validation\GroupValidation;
use FireflyIII\Validation\TransactionValidation;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use Log;
/**
* Class TransactionStoreRequest
*/
class TransactionStoreRequest extends Request
class TransactionStoreRequest extends FormRequest
{
use TransactionValidation, GroupValidation, CurrencyValidation;

View File

@ -30,13 +30,14 @@ use FireflyIII\Rules\IsBoolean;
use FireflyIII\Rules\IsDateOrTime;
use FireflyIII\Validation\GroupValidation;
use FireflyIII\Validation\TransactionValidation;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use Log;
/**
* Class TransactionUpdateRequest
*/
class TransactionUpdateRequest extends Request
class TransactionUpdateRequest extends FormRequest
{
use TransactionValidation, GroupValidation;

View File

@ -27,12 +27,13 @@ namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Rules\IsBoolean;
use FireflyIII\User;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class UserStoreRequest
*/
class UserStoreRequest extends Request
class UserStoreRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -27,12 +27,13 @@ namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Rules\IsBoolean;
use FireflyIII\User;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class UserUpdateRequest
*/
class UserUpdateRequest extends Request
class UserUpdateRequest extends FormRequest
{
/**
* Authorize logged in users.

View File

@ -25,6 +25,7 @@ namespace FireflyIII\Http\Requests;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use Exception;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use Log;
@ -37,6 +38,7 @@ use Log;
*/
class Request extends FormRequest
{
use ConvertsDataTypes;
/**
* @param $array
*
@ -127,17 +129,7 @@ class Request extends FormRequest
return (float) $res;
}
/**
* Return integer value.
*
* @param string $field
*
* @return int
*/
public function integer(string $field): int
{
return (int) $this->get($field);
}
/**
* Parse to integer
@ -158,17 +150,7 @@ class Request extends FormRequest
return (int) $string;
}
/**
* Return string value, but keep newlines.
*
* @param string $field
*
* @return string
*/
public function nlString(string $field): string
{
return app('steam')->nlCleanString((string) ($this->get($field) ?? ''));
}
/**
* Parse and clean a string, but keep the newlines.
@ -188,74 +170,10 @@ class Request extends FormRequest
}
/**
* Return integer value, or NULL when it's not set.
*
* @param string $field
*
* @return int|null
*/
public 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
*/
public 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
*/
public 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
*/
public function string(string $field): string
{
return app('steam')->cleanString((string) ($this->get($field) ?? ''));
}
/**
* Parse and clean a string.
@ -275,86 +193,6 @@ class Request extends FormRequest
}
/**
* 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;
}
/**
* 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 date time or NULL.
*
@ -424,20 +262,7 @@ class Request extends FormRequest
}
}
/**
* @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);
}
}

View 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);
}
}

View File

@ -1,7 +1,6 @@
<?php
/**
* TransferRequest.php
* ConvertAPIDataTypes.php
* Copyright (c) 2020 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@ -22,38 +21,12 @@
declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests\Search;
use FireflyIII\Api\V1\Requests\Request;
use FireflyIII\Rules\IsTransferAccount;
namespace FireflyIII\Support\Request;
/**
* Class TransferRequest
* Trait ConvertAPIDataTypes
*/
class TransferRequest extends Request
trait ConvertAPIDataTypes
{
/**
* Authorize logged in users.
*
* @return bool
*/
public function authorize(): bool
{
// Only allow authenticated users
return auth()->check();
}
/**
* @return array
*/
public function rules(): array
{
return [
'source' => ['required', new IsTransferAccount],
'destination' => ['required', new IsTransferAccount],
'amount' => 'required|numeric|gt:0',
'description' => 'required|min:1',
'date' => 'required|date',
];
}
}

View 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) ?? ''));
}
}

View File

@ -1,8 +1,7 @@
<?php
/**
* Request.php
* Copyright (c) 2019 james@firefly-iii.org
* GetRecurrenceData.php
* Copyright (c) 2020 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
@ -22,17 +21,12 @@
declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Http\Requests\Request as FireflyIIIRequest;
namespace FireflyIII\Support\Request;
/**
* Class Request.
*
* Technically speaking this class does not have to be extended like this but who knows what the future brings.
*
* Trait GetRecurrenceData
*/
class Request extends FireflyIIIRequest
trait GetRecurrenceData
{
/**
* @param array $transaction