Small code changes for @ctrl-f5's PR.

- Dropped some "setParameters" calls if unused in transformer.
- Add correct copyright
- Sprintf instead of string concat
- Small ident changes courtesy of phpstorm.
This commit is contained in:
James Cole
2025-10-10 18:50:39 +02:00
parent 7cfc4c2671
commit 83f3eddf44
10 changed files with 40 additions and 42 deletions

View File

@@ -70,48 +70,48 @@ class ShowController extends Controller
*/ */
public function index(ShowRequest $request): JsonResponse public function index(ShowRequest $request): JsonResponse
{ {
$manager = $this->getManager(); $manager = $this->getManager();
[ [
'types' => $types, 'types' => $types,
'page' => $page, 'page' => $page,
'limit' => $limit, 'limit' => $limit,
'offset' => $offset, 'offset' => $offset,
'sort' => $sort, 'sort' => $sort,
'start' => $start, 'start' => $start,
'end' => $end, 'end' => $end,
'date' => $date, 'date' => $date,
] = $request->attributes->all(); ]
= $request->attributes->all();
// get list of accounts. Count it and split it. // get list of accounts. Count it and split it.
$this->repository->resetAccountOrder(); $this->repository->resetAccountOrder();
$collection = $this->repository->getAccountsByType($types, $sort); $collection = $this->repository->getAccountsByType($types, $sort);
$count = $collection->count(); $count = $collection->count();
// continue sort: // continue sort:
// TODO if the user sorts on DB dependent field there must be no slice before enrichment, only after. // TODO if the user sorts on DB dependent field there must be no slice before enrichment, only after.
// TODO still need to figure out how to do this easily. // TODO still need to figure out how to do this easily.
$accounts = $collection->slice($offset, $limit); $accounts = $collection->slice($offset, $limit);
// enrich // enrich
/** @var User $admin */ /** @var User $admin */
$admin = auth()->user(); $admin = auth()->user();
$enrichment = new AccountEnrichment(); $enrichment = new AccountEnrichment();
$enrichment->setSort($sort); $enrichment->setSort($sort);
$enrichment->setDate($date); $enrichment->setDate($date);
$enrichment->setStart($start); $enrichment->setStart($start);
$enrichment->setEnd($end); $enrichment->setEnd($end);
$enrichment->setUser($admin); $enrichment->setUser($admin);
$accounts = $enrichment->enrich($accounts); $accounts = $enrichment->enrich($accounts);
// make paginator: // make paginator:
$paginator = new LengthAwarePaginator($accounts, $count, $limit, $page); $paginator = new LengthAwarePaginator($accounts, $count, $limit, $page);
$paginator->setPath(route('api.v1.accounts.index').$this->buildParams()); $paginator->setPath(route('api.v1.accounts.index') . $this->buildParams());
/** @var AccountTransformer $transformer */ /** @var AccountTransformer $transformer */
$transformer = app(AccountTransformer::class); $transformer = app(AccountTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($accounts, $transformer, self::RESOURCE_KEY); $resource = new FractalCollection($accounts, $transformer, self::RESOURCE_KEY);
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
@@ -128,27 +128,22 @@ class ShowController extends Controller
// get list of accounts. Count it and split it. // get list of accounts. Count it and split it.
$this->repository->resetAccountOrder(); $this->repository->resetAccountOrder();
$account->refresh(); $account->refresh();
$manager = $this->getManager(); $manager = $this->getManager();
[ [$start, $end, $date,] = $request->attributes->all();
'start' => $start,
'end' => $end,
'date' => $date,
] = $request->attributes->all();
// enrich // enrich
/** @var User $admin */ /** @var User $admin */
$admin = auth()->user(); $admin = auth()->user();
$enrichment = new AccountEnrichment(); $enrichment = new AccountEnrichment();
$enrichment->setDate($date); $enrichment->setDate($date);
$enrichment->setStart($start); $enrichment->setStart($start);
$enrichment->setEnd($end); $enrichment->setEnd($end);
$enrichment->setUser($admin); $enrichment->setUser($admin);
$account = $enrichment->enrichSingle($account); $account = $enrichment->enrichSingle($account);
/** @var AccountTransformer $transformer */ /** @var AccountTransformer $transformer */
$transformer = app(AccountTransformer::class); $transformer = app(AccountTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($account, $transformer, self::RESOURCE_KEY); $resource = new Item($account, $transformer, self::RESOURCE_KEY);
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);

View File

@@ -65,8 +65,6 @@ class IndexController extends Controller
$paginator = new LengthAwarePaginator($administrations, $count, $limit, $page); $paginator = new LengthAwarePaginator($administrations, $count, $limit, $page);
$transformer = new UserGroupTransformer(); $transformer = new UserGroupTransformer();
$transformer->setParameters($request->attributes); // give params to transformer
return response() return response()
->json($this->jsonApiList(self::RESOURCE_KEY, $paginator, $transformer)) ->json($this->jsonApiList(self::RESOURCE_KEY, $paginator, $transformer))
->header('Content-Type', self::CONTENT_TYPE) ->header('Content-Type', self::CONTENT_TYPE)

View File

@@ -1,7 +1,7 @@
<?php <?php
/* /*
* Copyright (c) 2021 james@firefly-iii.org * Copyright (c) 2025 https://github.com/ctrl-f5
* *
* This file is part of Firefly III (https://github.com/firefly-iii). * This file is part of Firefly III (https://github.com/firefly-iii).
* *

View File

@@ -1,7 +1,7 @@
<?php <?php
/* /*
* Copyright (c) 2021 james@firefly-iii.org * Copyright (c) 2025 https://github.com/ctrl-f5
* *
* This file is part of Firefly III (https://github.com/firefly-iii). * This file is part of Firefly III (https://github.com/firefly-iii).
* *

View File

@@ -1,7 +1,7 @@
<?php <?php
/* /*
* Copyright (c) 2021 james@firefly-iii.org * Copyright (c) 2025 https://github.com/ctrl-f5
* *
* This file is part of Firefly III (https://github.com/firefly-iii). * This file is part of Firefly III (https://github.com/firefly-iii).
* *
@@ -27,14 +27,15 @@ use FireflyIII\Exceptions\ValidationException;
use FireflyIII\Support\Request\ChecksLogin; use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes; use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
class DateRangeRequest extends ApiRequest class DateRangeRequest extends ApiRequest
{ {
public function rules(): array public function rules(): array
{ {
return [ return [
'start' => 'date|after:1970-01-02|before:2038-01-17|before:end|required_with:end|' . $this->required, 'start' => sprintf('date|after:1970-01-02|before:2038-01-17|before:end|required_with:end|' , $this->required),
'end' => 'date|after:1970-01-02|before:2038-01-17|after:start|required_with:start|' . $this->required, 'end' => sprintf('date|after:1970-01-02|before:2038-01-17|after:start|required_with:start|' , $this->required),
]; ];
} }

View File

@@ -1,7 +1,7 @@
<?php <?php
/* /*
* Copyright (c) 2021 james@firefly-iii.org * Copyright (c) 2025 https://github.com/ctrl-f5
* *
* This file is part of Firefly III (https://github.com/firefly-iii). * This file is part of Firefly III (https://github.com/firefly-iii).
* *

View File

@@ -2,8 +2,8 @@
declare(strict_types=1); declare(strict_types=1);
/* /*
* ShowRequest.php * AccountTypeApiRequest.php
* Copyright (c) 2025 james@firefly-iii.org * Copyright (c) 2025 https://github.com/ctrl-f5
* *
* This file is part of Firefly III (https://github.com/firefly-iii). * This file is part of Firefly III (https://github.com/firefly-iii).
* *

View File

@@ -1,7 +1,7 @@
<?php <?php
/* /*
* Copyright (c) 2021 james@firefly-iii.org * Copyright (c) 2025 https://github.com/ctrl-f5
* *
* This file is part of Firefly III (https://github.com/firefly-iii). * This file is part of Firefly III (https://github.com/firefly-iii).
* *

View File

@@ -34,11 +34,17 @@ abstract class AbstractTransformer extends TransformerAbstract
{ {
protected ParameterBag $parameters; protected ParameterBag $parameters;
/**
* @deprecated
*/
final public function getParameters(): ParameterBag final public function getParameters(): ParameterBag
{ {
return $this->parameters; return $this->parameters;
} }
/**
* @deprecated
*/
final public function setParameters(ParameterBag $parameters): void final public function setParameters(ParameterBag $parameters): void
{ {
$this->parameters = $parameters; $this->parameters = $parameters;

View File

@@ -30,7 +30,6 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\Facades\Amount; use FireflyIII\Support\Facades\Amount;
use Symfony\Component\HttpFoundation\ParameterBag;
/** /**
* Class AccountTransformer * Class AccountTransformer
@@ -46,7 +45,6 @@ class AccountTransformer extends AbstractTransformer
*/ */
public function __construct() public function __construct()
{ {
$this->parameters = new ParameterBag();
$this->repository = app(AccountRepositoryInterface::class); $this->repository = app(AccountRepositoryInterface::class);
$this->convertToPrimary = Amount::convertToPrimary(); $this->convertToPrimary = Amount::convertToPrimary();
$this->primary = Amount::getPrimaryCurrency(); $this->primary = Amount::getPrimaryCurrency();