mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 01:42:32 +00:00
Clean up some classes, extend API validation.
This commit is contained in:
@@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Api\V1\Requests;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -45,6 +46,7 @@ abstract class AggregateFormRequest extends ApiRequest
|
||||
|
||||
// instantiate all subrequests and share current requests' bags with them
|
||||
Log::debug('Initializing AggregateFormRequest.');
|
||||
|
||||
foreach ($this->getRequests() as $config) {
|
||||
$requestClass = is_array($config) ? array_shift($config) : $config;
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace FireflyIII\Api\V1\Requests\Autocomplete;
|
||||
|
||||
use FireflyIII\Api\V1\Requests\AggregateFormRequest;
|
||||
use FireflyIII\Api\V1\Requests\DateRequest;
|
||||
use FireflyIII\Api\V1\Requests\Generic\ObjectTypeApiRequest;
|
||||
use FireflyIII\Api\V1\Requests\Generic\QueryRequest;
|
||||
use FireflyIII\Api\V1\Requests\Models\Account\AccountTypesApiRequest;
|
||||
use FireflyIII\Api\V1\Requests\PaginationRequest;
|
||||
@@ -39,7 +40,7 @@ class AutocompleteApiRequest extends AggregateFormRequest
|
||||
return [
|
||||
DateRequest::class,
|
||||
[PaginationRequest::class, 'sort_class' => Account::class],
|
||||
AccountTypesApiRequest::class,
|
||||
[ObjectTypeApiRequest::class, 'object_type' => Account::class],
|
||||
QueryRequest::class,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/*
|
||||
* AutocompleteApiRequest.php
|
||||
* Copyright (c) 2025 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/>.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Api\V1\Requests\Autocomplete;
|
||||
|
||||
use FireflyIII\Api\V1\Requests\AggregateFormRequest;
|
||||
use FireflyIII\Api\V1\Requests\DateRequest;
|
||||
use FireflyIII\Api\V1\Requests\Generic\ObjectTypeApiRequest;
|
||||
use FireflyIII\Api\V1\Requests\Generic\QueryRequest;
|
||||
use FireflyIII\Api\V1\Requests\PaginationRequest;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use Override;
|
||||
|
||||
class AutocompleteTransactionApiRequest extends AggregateFormRequest
|
||||
{
|
||||
#[Override]
|
||||
protected function getRequests(): array
|
||||
{
|
||||
return [
|
||||
DateRequest::class,
|
||||
[PaginationRequest::class, 'sort_class' => Account::class],
|
||||
[ObjectTypeApiRequest::class, 'object_type' => Transaction::class],
|
||||
QueryRequest::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
90
app/Api/V1/Requests/Generic/ObjectTypeApiRequest.php
Normal file
90
app/Api/V1/Requests/Generic/ObjectTypeApiRequest.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/*
|
||||
* AccountTypeApiRequest.php
|
||||
* Copyright (c) 2025 https://github.com/ctrl-f5
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Api\V1\Requests\Generic;
|
||||
|
||||
use FireflyIII\Api\V1\Requests\ApiRequest;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Rules\Account\IsValidAccountTypeList;
|
||||
use FireflyIII\Rules\TransactionType\IsValidTransactionTypeList;
|
||||
use FireflyIII\Support\Http\Api\AccountFilter;
|
||||
use FireflyIII\Support\Http\Api\TransactionFilter;
|
||||
use Illuminate\Validation\Validator;
|
||||
use RuntimeException;
|
||||
|
||||
class ObjectTypeApiRequest extends ApiRequest
|
||||
{
|
||||
use AccountFilter;
|
||||
use TransactionFilter;
|
||||
|
||||
private ?string $objectType = null;
|
||||
|
||||
public function handleConfig(array $config): void
|
||||
{
|
||||
parent::handleConfig($config);
|
||||
|
||||
$this->objectType = $config['object_type'] ?? null;
|
||||
|
||||
if (!$this->objectType) {
|
||||
throw new RuntimeException('ObjectTypeApiRequest requires a object_type config');
|
||||
}
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
$rule = null;
|
||||
if (Account::class === $this->objectType) {
|
||||
$rule = new IsValidAccountTypeList();
|
||||
}
|
||||
if (Transaction::class === $this->objectType) {
|
||||
$rule = new IsValidTransactionTypeList();
|
||||
}
|
||||
return [
|
||||
'types' => $rule,
|
||||
];
|
||||
}
|
||||
|
||||
public function withValidator(Validator $validator): void
|
||||
{
|
||||
$validator->after(
|
||||
function (Validator $validator): void {
|
||||
if ($validator->failed()) {
|
||||
return;
|
||||
}
|
||||
$type = $this->convertString('types', 'all');
|
||||
$this->attributes->set('type', $type);
|
||||
switch ($this->objectType) {
|
||||
default:
|
||||
$this->attributes->set('types', []);
|
||||
case Account::class:
|
||||
$this->attributes->set('types', $this->mapAccountTypes($type));
|
||||
break;
|
||||
case Transaction::class:
|
||||
$this->attributes->set('types', $this->mapTransactionTypes($type));
|
||||
break;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ namespace FireflyIII\Api\V1\Requests\Models\Account;
|
||||
use FireflyIII\Api\V1\Requests\AggregateFormRequest;
|
||||
use FireflyIII\Api\V1\Requests\DateRangeRequest;
|
||||
use FireflyIII\Api\V1\Requests\DateRequest;
|
||||
use FireflyIII\Api\V1\Requests\Generic\ObjectTypeApiRequest;
|
||||
use FireflyIII\Api\V1\Requests\PaginationRequest;
|
||||
use FireflyIII\Models\Account;
|
||||
|
||||
@@ -38,6 +39,7 @@ class ShowRequest extends AggregateFormRequest
|
||||
DateRangeRequest::class,
|
||||
DateRequest::class,
|
||||
AccountTypeApiRequest::class,
|
||||
[ObjectTypeApiRequest::class, 'object_type' => Account::class],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user