mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Various phpstan fixes [skip ci]
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* CollectsCustomParameters.php
|
||||
* Copyright (c) 2024 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\JsonApi;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
trait CollectsCustomParameters
|
||||
{
|
||||
protected function getOtherParams(array $params): array
|
||||
{
|
||||
$return = [];
|
||||
if (array_key_exists('startPeriod', $params)) {
|
||||
$return['start'] = Carbon::parse($params['startPeriod']);
|
||||
}
|
||||
if (array_key_exists('endPeriod', $params)) {
|
||||
$return['end'] = Carbon::parse($params['endPeriod']);
|
||||
}
|
||||
if (array_key_exists('currentMoment', $params)) {
|
||||
$return['today'] = Carbon::parse($params['currentMoment']);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
@@ -1,63 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* UserGroupDetectable.php
|
||||
* Copyright (c) 2024 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\JsonApi\Concerns;
|
||||
|
||||
use FireflyIII\Models\UserGroup;
|
||||
use FireflyIII\User;
|
||||
|
||||
trait UserGroupDetectable
|
||||
{
|
||||
/**
|
||||
* Return the user group or NULL if none is set.
|
||||
* Will throw exception if invalid.
|
||||
* TODO Duplicate from API v2 code.
|
||||
*/
|
||||
public function detectUserGroup(): ?UserGroup
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
app('log')->debug('Now in detectUserGroup()');
|
||||
|
||||
/** @var null|UserGroup $userGroup */
|
||||
$userGroup = request()->route()?->parameter('userGroup');
|
||||
if (null === $userGroup) {
|
||||
app('log')->debug('Request class has no userGroup parameter, but perhaps there is a parameter.');
|
||||
$userGroupId = (int) request()->get('user_group_id');
|
||||
if (0 === $userGroupId) {
|
||||
app('log')->debug(sprintf('Request class has no user_group_id parameter, grab default from user (group #%d).', $user->user_group_id));
|
||||
$userGroupId = (int) $user->user_group_id;
|
||||
}
|
||||
$userGroup = UserGroup::find($userGroupId);
|
||||
if (null === $userGroup) {
|
||||
app('log')->error(sprintf('Request class has user_group_id (#%d), but group does not exist.', $userGroupId));
|
||||
|
||||
return null;
|
||||
}
|
||||
app('log')->debug('Request class has valid user_group_id.');
|
||||
}
|
||||
|
||||
return $userGroup;
|
||||
}
|
||||
}
|
@@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* UsergroupAware.php
|
||||
* Copyright (c) 2024 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\JsonApi\Concerns;
|
||||
|
||||
use FireflyIII\Models\UserGroup;
|
||||
|
||||
trait UsergroupAware
|
||||
{
|
||||
protected UserGroup $userGroup;
|
||||
|
||||
public function getUserGroup(): UserGroup
|
||||
{
|
||||
return $this->userGroup;
|
||||
}
|
||||
|
||||
public function setUserGroup(UserGroup $userGroup): void
|
||||
{
|
||||
$this->userGroup = $userGroup;
|
||||
}
|
||||
|
||||
public function withUserGroup(UserGroup $userGroup): self
|
||||
{
|
||||
$this->userGroup = $userGroup;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@@ -1,138 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ExpandsQuery.php
|
||||
* Copyright (c) 2024 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\JsonApi;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Support\Http\Api\AccountFilter;
|
||||
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
trait ExpandsQuery
|
||||
{
|
||||
use AccountFilter;
|
||||
|
||||
final protected function addFilterParams(string $class, Builder $query, ?FilterParameters $filters): Builder
|
||||
{
|
||||
Log::debug(__METHOD__);
|
||||
if (null === $filters) {
|
||||
return $query;
|
||||
}
|
||||
if (0 === count($filters->all())) {
|
||||
return $query;
|
||||
}
|
||||
// parse filters valid for this class.
|
||||
$parsed = $this->parseAllFilters($class, $filters);
|
||||
|
||||
// expand query for each query filter
|
||||
$config = config('api.valid_query_filters')[$class];
|
||||
$query->where(function (Builder $q) use ($config, $parsed): void {
|
||||
foreach ($parsed as $key => $filter) {
|
||||
if (in_array($key, $config, true)) {
|
||||
Log::debug(sprintf('Add query filter "%s"', $key));
|
||||
// add type to query:
|
||||
foreach ($filter as $value) {
|
||||
$q->whereLike($key, sprintf('%%%s%%', $value));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// TODO this is special treatment, but alas, unavoidable right now.
|
||||
if (Account::class === $class && array_key_exists('type', $parsed)) {
|
||||
if (count($parsed['type']) > 0) {
|
||||
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
||||
$query->whereIn('account_types.type', $parsed['type']);
|
||||
}
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
private function parseAllFilters(string $class, FilterParameters $filters): array
|
||||
{
|
||||
$config = config('api.valid_api_filters')[$class];
|
||||
$parsed = [];
|
||||
foreach ($filters->all() as $filter) {
|
||||
$key = $filter->key();
|
||||
if (!in_array($key, $config, true)) {
|
||||
continue;
|
||||
}
|
||||
// make array if not array:
|
||||
$value = $filter->value();
|
||||
if (null === $value) {
|
||||
continue;
|
||||
}
|
||||
if (!is_array($value)) {
|
||||
$value = [$value];
|
||||
}
|
||||
|
||||
switch ($filter->key()) {
|
||||
case 'name':
|
||||
$parsed['name'] = $value;
|
||||
|
||||
break;
|
||||
|
||||
case 'type':
|
||||
$parsed['type'] = $this->parseAccountTypeFilter($value);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
private function parseAccountTypeFilter(array $value): array
|
||||
{
|
||||
$return = [];
|
||||
foreach ($value as $entry) {
|
||||
$return = array_merge($return, $this->mapAccountTypes($entry));
|
||||
}
|
||||
|
||||
return array_unique($return);
|
||||
}
|
||||
|
||||
final protected function addPagination(Builder $query, array $pagination): Builder
|
||||
{
|
||||
$skip = ($pagination['number'] - 1) * $pagination['size'];
|
||||
|
||||
return $query->skip($skip)->take($pagination['size']);
|
||||
}
|
||||
|
||||
final protected function addSortParams(string $class, Builder $query, ?SortFields $sort): Builder
|
||||
{
|
||||
$config = config('api.valid_query_sort')[$class] ?? [];
|
||||
if (null === $sort) {
|
||||
return $query;
|
||||
}
|
||||
foreach ($sort->all() as $sortField) {
|
||||
if (in_array($sortField->name(), $config, true)) {
|
||||
$query->orderBy($sortField->name(), $sortField->isAscending() ? 'ASC' : 'DESC');
|
||||
}
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
}
|
@@ -1,56 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* FiltersPagination.php
|
||||
* Copyright (c) 2024 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\JsonApi;
|
||||
|
||||
trait FiltersPagination
|
||||
{
|
||||
protected function filtersPagination(?array $pagination): array
|
||||
{
|
||||
if (null === $pagination) {
|
||||
return [
|
||||
'number' => 1,
|
||||
'size' => $this->getPageSize(),
|
||||
];
|
||||
}
|
||||
// cleanup page number
|
||||
$pagination['number'] = (int) ($pagination['number'] ?? 1);
|
||||
$pagination['number'] = min(65536, max($pagination['number'], 1));
|
||||
|
||||
// clean up page size
|
||||
$pagination['size'] = (int) ($pagination['size'] ?? $this->getPageSize());
|
||||
$pagination['size'] = min(1337, max($pagination['size'], 1));
|
||||
|
||||
return $pagination;
|
||||
}
|
||||
|
||||
private function getPageSize(): int
|
||||
{
|
||||
if (auth()->check()) {
|
||||
return (int) app('preferences')->get('listPageSize', 50)->data;
|
||||
}
|
||||
|
||||
return 50;
|
||||
}
|
||||
}
|
@@ -1,48 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* SortsCollection.php
|
||||
* Copyright (c) 2024 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\JsonApi;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
trait SortsCollection
|
||||
{
|
||||
protected function sortCollection(string $class, Collection $collection, ?SortFields $sortFields): Collection
|
||||
{
|
||||
Log::debug(__METHOD__);
|
||||
$config = config('api.valid_api_sort')[$class] ?? [];
|
||||
if (null === $sortFields) {
|
||||
return $collection;
|
||||
}
|
||||
foreach ($sortFields->all() as $sortField) {
|
||||
if (in_array($sortField->name(), $config, true)) {
|
||||
Log::debug(sprintf('Sort collection by "%s"', $sortField->name()));
|
||||
$collection = $sortField->isAscending() ? $collection->sortBy($sortField->name()) : $collection->sortByDesc($sortField->name());
|
||||
}
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
}
|
@@ -1,130 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* SortsQueryResults.php
|
||||
* Copyright (c) 2024 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\JsonApi;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
trait SortsQueryResults
|
||||
{
|
||||
final protected function postQuerySort(string $class, Collection $collection, SortFields $parameters): Collection
|
||||
{
|
||||
Log::debug(__METHOD__);
|
||||
foreach ($parameters->all() as $field) {
|
||||
$collection = $this->sortQueryCollection($class, $collection, $field);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO improve this.
|
||||
*/
|
||||
private function sortQueryCollection(string $class, Collection $collection, SortField $field): Collection
|
||||
{
|
||||
// here be custom sort things.
|
||||
// sort by balance
|
||||
if (Account::class === $class && 'balance' === $field->name()) {
|
||||
$ascending = $field->isAscending();
|
||||
$collection = $collection->sort(function (Account $left, Account $right) use ($ascending): int {
|
||||
$leftSum = $this->sumBalance($left->balance);
|
||||
$rightSum = $this->sumBalance($right->balance);
|
||||
|
||||
return $ascending ? bccomp($leftSum, $rightSum) : bccomp($rightSum, $leftSum);
|
||||
});
|
||||
}
|
||||
if (Account::class === $class && 'balance_difference' === $field->name()) {
|
||||
$ascending = $field->isAscending();
|
||||
$collection = $collection->sort(function (Account $left, Account $right) use ($ascending): int {
|
||||
$leftSum = $this->sumBalanceDifference($left->balance);
|
||||
$rightSum = $this->sumBalanceDifference($right->balance);
|
||||
|
||||
return $ascending ? bccomp($leftSum, $rightSum) : bccomp($rightSum, $leftSum);
|
||||
});
|
||||
}
|
||||
// sort by account number
|
||||
if (Account::class === $class && 'account_number' === $field->name()) {
|
||||
$ascending = $field->isAscending();
|
||||
$collection = $collection->sort(function (Account $left, Account $right) use ($ascending): int {
|
||||
$leftNr = sprintf('%s%s', $left->iban, $left->account_number);
|
||||
$rightNr = sprintf('%s%s', $right->iban, $right->account_number);
|
||||
|
||||
return $ascending ? strcmp($leftNr, $rightNr) : strcmp($rightNr, $leftNr);
|
||||
});
|
||||
}
|
||||
|
||||
// sort by last activity
|
||||
if (Account::class === $class && 'last_activity' === $field->name()) {
|
||||
$ascending = $field->isAscending();
|
||||
$collection = $collection->sort(function (Account $left, Account $right) use ($ascending): int {
|
||||
$leftNr = (int) $left->last_activity?->format('U');
|
||||
$rightNr = (int) $right->last_activity?->format('U');
|
||||
if ($ascending) {
|
||||
return $leftNr <=> $rightNr;
|
||||
}
|
||||
|
||||
return $rightNr <=> $leftNr;
|
||||
// return (int) ($ascending ? $rightNr < $leftNr : $leftNr < $rightNr );
|
||||
});
|
||||
}
|
||||
|
||||
// sort by balance difference.
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
private function sumBalance(?array $balance): string
|
||||
{
|
||||
if (null === $balance) {
|
||||
return '-10000000000'; // minus one billion
|
||||
}
|
||||
if (0 === count($balance)) {
|
||||
return '-10000000000'; // minus one billion
|
||||
}
|
||||
$sum = '0';
|
||||
foreach ($balance as $entry) {
|
||||
$sum = bcadd($sum, $entry['balance']);
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
private function sumBalanceDifference(?array $balance): string
|
||||
{
|
||||
if (null === $balance) {
|
||||
return '-10000000000'; // minus one billion
|
||||
}
|
||||
if (0 === count($balance)) {
|
||||
return '-10000000000'; // minus one billion
|
||||
}
|
||||
$sum = '0';
|
||||
foreach ($balance as $entry) {
|
||||
$sum = bcadd($sum, $entry['balance_difference']);
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
}
|
@@ -1,48 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ValidateSortParameters.php
|
||||
* Copyright (c) 2024 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\JsonApi;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
trait ValidateSortParameters
|
||||
{
|
||||
public function needsFullDataset(string $class, ?SortFields $params): bool
|
||||
{
|
||||
Log::debug(__METHOD__);
|
||||
if (null === $params) {
|
||||
return false;
|
||||
}
|
||||
$config = config('api.full_data_set')[$class] ?? [];
|
||||
foreach ($params->all() as $field) {
|
||||
if (in_array($field->name(), $config, true)) {
|
||||
Log::debug('TRUE');
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user