2023-03-25 11:32:33 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* AutocompleteRequest.php
|
|
|
|
* Copyright (c) 2023 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\Api\V2\Request\Autocomplete;
|
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
use Carbon\Carbon;
|
2024-05-12 17:50:54 +02:00
|
|
|
use Carbon\Exceptions\InvalidFormatException;
|
2024-05-12 13:31:33 +02:00
|
|
|
use FireflyIII\JsonApi\Rules\IsValidFilter;
|
|
|
|
use FireflyIII\JsonApi\Rules\IsValidPage;
|
|
|
|
use FireflyIII\Support\Http\Api\AccountFilter;
|
2023-03-25 11:32:33 +01:00
|
|
|
use FireflyIII\Support\Request\ChecksLogin;
|
|
|
|
use FireflyIII\Support\Request\ConvertsDataTypes;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2024-05-19 06:36:31 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-05-12 13:31:33 +02:00
|
|
|
use LaravelJsonApi\Core\Query\QueryParameters;
|
|
|
|
use LaravelJsonApi\Validation\Rule as JsonApiRule;
|
2024-05-13 05:10:16 +02:00
|
|
|
|
2023-03-25 11:32:33 +01:00
|
|
|
/**
|
|
|
|
* Class AutocompleteRequest
|
|
|
|
*/
|
|
|
|
class AutocompleteRequest extends FormRequest
|
|
|
|
{
|
2024-05-13 05:10:16 +02:00
|
|
|
use AccountFilter;
|
2023-03-25 11:32:33 +01:00
|
|
|
use ChecksLogin;
|
2023-11-04 14:18:49 +01:00
|
|
|
use ConvertsDataTypes;
|
2023-10-08 16:11:04 +02:00
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
/**
|
|
|
|
* Loops over all possible query parameters (these are shared over ALL auto complete requests)
|
|
|
|
* and returns a validated array of parameters.
|
|
|
|
*
|
|
|
|
* The advantage is a single class. But you may also submit "account types" to an endpoint that doesn't use these.
|
|
|
|
*/
|
|
|
|
public function getParameters(): array
|
|
|
|
{
|
|
|
|
$queryParameters = QueryParameters::cast($this->all());
|
2024-05-13 05:10:16 +02:00
|
|
|
|
2024-05-12 17:50:54 +02:00
|
|
|
try {
|
|
|
|
$date = Carbon::createFromFormat('Y-m-d', $queryParameters->filter()?->value('date', date('Y-m-d')), config('app.timezone'));
|
2024-05-13 05:10:16 +02:00
|
|
|
} catch (InvalidFormatException $e) {
|
2024-05-12 17:50:54 +02:00
|
|
|
Log::debug(sprintf('Invalid date format in autocomplete request. Using today: %s', $e->getMessage()));
|
|
|
|
$date = today();
|
|
|
|
}
|
2024-05-19 06:36:31 +02:00
|
|
|
$query = $queryParameters->filter()?->value('query', []) ?? [];
|
|
|
|
$query = is_string($query) ? [$query] : $query;
|
|
|
|
$size = (int) ($queryParameters->page()['size'] ?? 50);
|
|
|
|
$accountTypeRequest = $queryParameters->filter()?->value('account_types', []) ?? [];
|
|
|
|
$accountTypeRequest = is_string($accountTypeRequest) ? [$accountTypeRequest] : $accountTypeRequest;
|
|
|
|
$accountTypes = $this->getAccountTypeParameter($accountTypeRequest);
|
2024-05-12 13:31:33 +02:00
|
|
|
|
|
|
|
return [
|
|
|
|
'date' => $date,
|
|
|
|
'query' => $query,
|
|
|
|
'size' => $size,
|
|
|
|
'account_types' => $accountTypes,
|
|
|
|
];
|
|
|
|
}
|
2023-03-25 11:32:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
2024-05-12 13:31:33 +02:00
|
|
|
'fields' => JsonApiRule::notSupported(),
|
2024-05-13 05:10:16 +02:00
|
|
|
'filter' => ['nullable', 'array', new IsValidFilter(['query', 'date', 'account_types'])],
|
2024-05-12 13:31:33 +02:00
|
|
|
'include' => JsonApiRule::notSupported(),
|
2024-05-13 05:10:16 +02:00
|
|
|
'page' => ['nullable', 'array', new IsValidPage(['size'])],
|
2024-05-12 13:31:33 +02:00
|
|
|
'sort' => JsonApiRule::notSupported(),
|
2023-03-25 11:32:33 +01:00
|
|
|
];
|
|
|
|
}
|
2024-05-12 13:31:33 +02:00
|
|
|
|
|
|
|
private function getAccountTypeParameter(mixed $types): array
|
|
|
|
{
|
|
|
|
if (is_string($types) && str_contains($types, ',')) {
|
|
|
|
$types = explode(',', $types);
|
|
|
|
}
|
|
|
|
if (!is_iterable($types)) {
|
|
|
|
$types = [$types];
|
|
|
|
}
|
|
|
|
$return = [];
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$return = array_merge($return, $this->mapAccountTypes($type));
|
|
|
|
}
|
2024-05-13 05:10:16 +02:00
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
return array_unique($return);
|
|
|
|
}
|
2023-03-25 11:32:33 +01:00
|
|
|
}
|