Code cleanup.

This commit is contained in:
James Cole
2023-12-20 19:35:52 +01:00
parent c4f6366642
commit 64ec0cf62e
997 changed files with 12908 additions and 28136 deletions

View File

@@ -37,25 +37,14 @@ abstract class AbstractTransformer extends TransformerAbstract
/**
* This method is called exactly ONCE from FireflyIII\Api\V2\Controllers\Controller::jsonApiList
*
* @param Collection $objects
*
* @return void
*/
abstract public function collectMetaData(Collection $objects): void;
/**
* @return ParameterBag
*/
final public function getParameters(): ParameterBag
{
return $this->parameters;
}
/**
* @param ParameterBag $parameters
*/
final public function setParameters(ParameterBag $parameters): void
{
$this->parameters = $parameters;

View File

@@ -46,7 +46,6 @@ class AccountTransformer extends AbstractTransformer
private TransactionCurrency $default;
/**
* @inheritDoc
* @throws FireflyException
*/
public function collectMetaData(Collection $objects): void
@@ -64,8 +63,9 @@ class AccountTransformer extends AbstractTransformer
// get currencies:
$accountIds = $objects->pluck('id')->toArray();
$meta = AccountMeta::whereIn('account_id', $accountIds)
->where('name', 'currency_id')
->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data']);
->where('name', 'currency_id')
->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])
;
$currencyIds = $meta->pluck('data')->toArray();
$currencies = $repository->getByIds($currencyIds);
@@ -80,33 +80,18 @@ class AccountTransformer extends AbstractTransformer
// get account types:
// select accounts.id, account_types.type from account_types left join accounts on accounts.account_type_id = account_types.id;
$accountTypes = AccountType::leftJoin('accounts', 'accounts.account_type_id', '=', 'account_types.id')
->whereIn('accounts.id', $accountIds)
->get(['accounts.id', 'account_types.type']);
->whereIn('accounts.id', $accountIds)
->get(['accounts.id', 'account_types.type'])
;
/** @var AccountType $row */
foreach ($accountTypes as $row) {
$this->accountTypes[$row->id] = (string)config(sprintf('firefly.shortNamesByFullName.%s', $row->type));
}
}
/**
* @return Carbon
*/
private function getDate(): Carbon
{
$date = today(config('app.timezone'));
if (null !== $this->parameters->get('date')) {
$date = $this->parameters->get('date');
}
return $date;
}
/**
* Transform the account.
*
* @param Account $account
*
* @return array
*/
public function transform(Account $account): array
{
@@ -178,9 +163,19 @@ class AccountTransformer extends AbstractTransformer
'links' => [
[
'rel' => 'self',
'uri' => '/accounts/' . $account->id,
'uri' => '/accounts/'.$account->id,
],
],
];
}
private function getDate(): Carbon
{
$date = today(config('app.timezone'));
if (null !== $this->parameters->get('date')) {
$date = $this->parameters->get('date');
}
return $date;
}
}

View File

@@ -47,9 +47,6 @@ class BillTransformer extends AbstractTransformer
private array $notes;
private array $paidDates;
/**
* @inheritDoc
*/
public function collectMetaData(Collection $objects): void
{
$currencies = [];
@@ -58,7 +55,6 @@ class BillTransformer extends AbstractTransformer
$this->groups = [];
$this->paidDates = [];
// start with currencies:
/** @var Bill $object */
foreach ($objects as $object) {
@@ -70,6 +66,7 @@ class BillTransformer extends AbstractTransformer
// continue with notes
$notes = Note::whereNoteableType(Bill::class)->whereIn('noteable_id', array_keys($bills))->get();
/** @var Note $note */
foreach ($notes as $note) {
$id = $note->noteable_id;
@@ -77,9 +74,11 @@ class BillTransformer extends AbstractTransformer
}
// grab object groups:
$set = DB::table('object_groupables')
->leftJoin('object_groups', 'object_groups.id', '=', 'object_groupables.object_group_id')
->where('object_groupables.object_groupable_type', Bill::class)
->get(['object_groupables.*', 'object_groups.title', 'object_groups.order']);
->leftJoin('object_groups', 'object_groups.id', '=', 'object_groupables.object_group_id')
->where('object_groupables.object_groupable_type', Bill::class)
->get(['object_groupables.*', 'object_groups.title', 'object_groups.order'])
;
/** @var ObjectGroup $entry */
foreach ($set as $entry) {
$billId = (int)$entry->object_groupable_id;
@@ -90,7 +89,6 @@ class BillTransformer extends AbstractTransformer
'object_group_title' => $entry->title,
'object_group_order' => $order,
];
}
$this->default = app('amount')->getDefaultCurrency();
$this->converter = new ExchangeRateConverter();
@@ -98,22 +96,26 @@ class BillTransformer extends AbstractTransformer
// grab all paid dates:
if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
$journals = TransactionJournal::whereIn('bill_id', $bills)
->where('date', '>=', $this->parameters->get('start'))
->where('date', '<=', $this->parameters->get('end'))
->get(['transaction_journals.id', 'transaction_journals.transaction_group_id', 'transaction_journals.date', 'transaction_journals.bill_id']);
->where('date', '>=', $this->parameters->get('start'))
->where('date', '<=', $this->parameters->get('end'))
->get(['transaction_journals.id', 'transaction_journals.transaction_group_id', 'transaction_journals.date', 'transaction_journals.bill_id'])
;
$journalIds = $journals->pluck('id')->toArray();
// grab transactions for amount:
$set = Transaction::whereIn('transaction_journal_id', $journalIds)
->where('transactions.amount', '<', 0)
->get(['transactions.id', 'transactions.transaction_journal_id', 'transactions.amount', 'transactions.foreign_amount', 'transactions.transaction_currency_id', 'transactions.foreign_currency_id']);
->where('transactions.amount', '<', 0)
->get(['transactions.id', 'transactions.transaction_journal_id', 'transactions.amount', 'transactions.foreign_amount', 'transactions.transaction_currency_id', 'transactions.foreign_currency_id'])
;
// convert to array for easy finding:
$transactions = [];
/** @var Transaction $transaction */
foreach ($set as $transaction) {
$journalId = $transaction->transaction_journal_id;
$transactions[$journalId] = $transaction->toArray();
}
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
app('log')->debug(sprintf('Processing journal #%d', $journal->id));
@@ -173,10 +175,6 @@ class BillTransformer extends AbstractTransformer
/**
* Transform the bill.
*
* @param Bill $bill
*
* @return array
*/
public function transform(Bill $bill): array
{
@@ -187,13 +185,14 @@ class BillTransformer extends AbstractTransformer
$group = $this->groups[$bill->id] ?? null;
// date for currency conversion
/** @var Carbon|null $startParam */
/** @var null|Carbon $startParam */
$startParam = $this->parameters->get('start');
/** @var Carbon|null $date */
/** @var null|Carbon $date */
$date = null === $startParam ? today() : clone $startParam;
$nextExpectedMatchDiff = $this->getNextExpectedMatchDiff($nextExpectedMatch, $payDates);
return [
'id' => $bill->id,
'created_at' => $bill->created_at->toAtomString(),
@@ -239,11 +238,6 @@ class BillTransformer extends AbstractTransformer
/**
* Get the data the bill was paid and predict the next expected match.
*
* @param Bill $bill
* @param array $dates
*
* @return Carbon
*/
protected function nextExpectedMatch(Bill $bill, array $dates): Carbon
{
@@ -251,9 +245,10 @@ class BillTransformer extends AbstractTransformer
// 2023-07-18 this particular date is used to search for the last paid date.
// 2023-07-18 the cloned $searchDate is used to grab the correct transactions.
/** @var Carbon|null $startParam */
/** @var null|Carbon $startParam */
$startParam = $this->parameters->get('start');
/** @var Carbon|null $start */
/** @var null|Carbon $start */
$start = null === $startParam ? today() : clone $startParam;
$start->subDay();
@@ -267,21 +262,15 @@ class BillTransformer extends AbstractTransformer
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
}
if ($nextMatch->isSameDay($lastPaidDate)) {
/*
* Add another period because it's the same day as the last paid date.
*/
// Add another period because it's the same day as the last paid date.
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
}
return $nextMatch;
}
/**
* Returns the latest date in the set, or start when set is empty.
*
* @param array $dates
* @param Carbon $default
*
* @return Carbon
*/
protected function lastPaidDate(array $dates, Carbon $default): Carbon
{
@@ -289,6 +278,7 @@ class BillTransformer extends AbstractTransformer
return $default;
}
$latest = $dates[0]['date'];
/** @var array $row */
foreach ($dates as $row) {
$carbon = new Carbon($row['date']);
@@ -300,16 +290,11 @@ class BillTransformer extends AbstractTransformer
return new Carbon($latest);
}
/**
* @param Bill $bill
*
* @return array
*/
protected function payDates(Bill $bill): array
{
//app('log')->debug(sprintf('Now in payDates() for bill #%d', $bill->id));
// app('log')->debug(sprintf('Now in payDates() for bill #%d', $bill->id));
if (null === $this->parameters->get('start') || null === $this->parameters->get('end')) {
//app('log')->debug('No start or end date, give empty array.');
// app('log')->debug('No start or end date, give empty array.');
return [];
}
@@ -328,7 +313,7 @@ class BillTransformer extends AbstractTransformer
$set->push(clone $nextExpectedMatch);
$nextExpectedMatch->addDay();
$currentStart = clone $nextExpectedMatch;
$loop++;
++$loop;
if ($loop > 4) {
break;
}
@@ -347,32 +332,21 @@ class BillTransformer extends AbstractTransformer
* transaction. Whether or not it is there already, is not relevant.
*
* TODO this method is bad compared to the v1 one.
*
* @param Bill $bill
* @param Carbon $date
*
* @return Carbon
*/
protected function nextDateMatch(Bill $bill, Carbon $date): Carbon
{
//app('log')->debug(sprintf('Now in nextDateMatch(%d, %s)', $bill->id, $date->format('Y-m-d')));
// app('log')->debug(sprintf('Now in nextDateMatch(%d, %s)', $bill->id, $date->format('Y-m-d')));
$start = clone $bill->date;
//app('log')->debug(sprintf('Bill start date is %s', $start->format('Y-m-d')));
// app('log')->debug(sprintf('Bill start date is %s', $start->format('Y-m-d')));
while ($start < $date) {
$start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
}
//app('log')->debug(sprintf('End of loop, bill start date is now %s', $start->format('Y-m-d')));
// app('log')->debug(sprintf('End of loop, bill start date is now %s', $start->format('Y-m-d')));
return $start;
}
/**
* @param Carbon $next
* @param array $dates
*
* @return string
*/
private function getNextExpectedMatchDiff(Carbon $next, array $dates): string
{
if ($next->isToday()) {
@@ -383,8 +357,7 @@ class BillTransformer extends AbstractTransformer
return trans('firefly.not_expected_period');
}
$carbon = new Carbon($current);
return $carbon->diffForHumans(today(config('app.timezone')), CarbonInterface::DIFF_RELATIVE_TO_NOW);
}
}

View File

@@ -37,9 +37,6 @@ class BudgetLimitTransformer extends AbstractTransformer
'budget',
];
/**
* @inheritDoc
*/
public function collectMetaData(Collection $objects): void
{
// TODO: Implement collectMetaData() method.
@@ -48,8 +45,6 @@ class BudgetLimitTransformer extends AbstractTransformer
/**
* Include Budget
*
* @param BudgetLimit $limit
*
* @return Item
*/
public function includeBudget(BudgetLimit $limit)
@@ -59,10 +54,6 @@ class BudgetLimitTransformer extends AbstractTransformer
/**
* Transform the note.
*
* @param BudgetLimit $budgetLimit
*
* @return array
*/
public function transform(BudgetLimit $budgetLimit): array
{
@@ -102,7 +93,7 @@ class BudgetLimitTransformer extends AbstractTransformer
'currency_symbol' => $currencySymbol,
'amount' => $amount,
'period' => $budgetLimit->period,
//'spent' => $expenses[$currencyId]['sum'] ?? '0',
// 'spent' => $expenses[$currencyId]['sum'] ?? '0',
'links' => [
[
'rel' => 'self',

View File

@@ -32,24 +32,19 @@ use Symfony\Component\HttpFoundation\ParameterBag;
*/
class BudgetTransformer extends AbstractTransformer
{
//private OperationsRepositoryInterface $opsRepository;
//private BudgetRepositoryInterface $repository;
// private OperationsRepositoryInterface $opsRepository;
// private BudgetRepositoryInterface $repository;
/**
* BudgetTransformer constructor.
*
*/
public function __construct()
{
//$this->opsRepository = app(OperationsRepositoryInterface::class);
//$this->repository = app(BudgetRepositoryInterface::class);
// $this->opsRepository = app(OperationsRepositoryInterface::class);
// $this->repository = app(BudgetRepositoryInterface::class);
$this->parameters = new ParameterBag();
}
/**
* @inheritDoc
*/
public function collectMetaData(Collection $objects): void
{
// TODO: Implement collectMetaData() method.
@@ -57,17 +52,13 @@ class BudgetTransformer extends AbstractTransformer
/**
* Transform a budget.
*
* @param Budget $budget
*
* @return array
*/
public function transform(Budget $budget): array
{
//$this->opsRepository->setUser($budget->user);
//$start = $this->parameters->get('start');
//$end = $this->parameters->get('end');
//$autoBudget = $this->repository->getAutoBudget($budget);
// $this->opsRepository->setUser($budget->user);
// $start = $this->parameters->get('start');
// $end = $this->parameters->get('end');
// $autoBudget = $this->repository->getAutoBudget($budget);
// $spent = [];
// if (null !== $start && null !== $end) {
// $spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget])));
@@ -115,5 +106,4 @@ class BudgetTransformer extends AbstractTransformer
],
];
}
}

View File

@@ -31,17 +31,10 @@ use Illuminate\Support\Collection;
*/
class CurrencyTransformer extends AbstractTransformer
{
/**
* @inheritDoc
*/
public function collectMetaData(Collection $objects): void {}
/**
* Transform the currency.
*
* @param TransactionCurrency $currency
*
* @return array
*/
public function transform(TransactionCurrency $currency): array
{
@@ -58,7 +51,7 @@ class CurrencyTransformer extends AbstractTransformer
'links' => [
[
'rel' => 'self',
'uri' => '/currencies/' . $currency->id,
'uri' => '/currencies/'.$currency->id,
],
],
];

View File

@@ -36,7 +36,6 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use JsonException;
/**
* Class PiggyBankTransformer
@@ -56,8 +55,6 @@ class PiggyBankTransformer extends AbstractTransformer
/**
* PiggyBankTransformer constructor.
*
*/
public function __construct()
{
@@ -71,9 +68,6 @@ class PiggyBankTransformer extends AbstractTransformer
// $this->piggyRepos = app(PiggyBankRepositoryInterface::class);
}
/**
* @inheritDoc
*/
public function collectMetaData(Collection $objects): void
{
// TODO move to repository (does not exist yet)
@@ -81,6 +75,7 @@ class PiggyBankTransformer extends AbstractTransformer
$accountInfo = Account::whereIn('id', $objects->pluck('account_id')->toArray())->get();
$currencyPreferences = AccountMeta::where('name', '"currency_id"')->whereIn('account_id', $objects->pluck('account_id')->toArray())->get();
$currencies = [];
/** @var Account $account */
foreach ($accountInfo as $account) {
$id = $account->id;
@@ -88,6 +83,7 @@ class PiggyBankTransformer extends AbstractTransformer
'name' => $account->name,
];
}
/** @var AccountMeta $preference */
foreach ($currencyPreferences as $preference) {
$currencyId = (int)$preference->data;
@@ -98,9 +94,11 @@ class PiggyBankTransformer extends AbstractTransformer
// grab object groups:
$set = DB::table('object_groupables')
->leftJoin('object_groups', 'object_groups.id', '=', 'object_groupables.object_group_id')
->where('object_groupables.object_groupable_type', PiggyBank::class)
->get(['object_groupables.*', 'object_groups.title', 'object_groups.order']);
->leftJoin('object_groups', 'object_groups.id', '=', 'object_groupables.object_group_id')
->where('object_groupables.object_groupable_type', PiggyBank::class)
->get(['object_groupables.*', 'object_groups.title', 'object_groups.order'])
;
/** @var ObjectGroup $entry */
foreach ($set as $entry) {
$piggyBankId = (int)$entry->object_groupable_id;
@@ -111,11 +109,11 @@ class PiggyBankTransformer extends AbstractTransformer
'object_group_title' => $entry->title,
'object_group_order' => $order,
];
}
// grab repetitions (for current amount):
$repetitions = PiggyBankRepetition::whereIn('piggy_bank_id', $piggyBanks)->get();
/** @var PiggyBankRepetition $repetition */
foreach ($repetitions as $repetition) {
$this->repetitions[$repetition->piggy_bank_id] = [
@@ -126,6 +124,7 @@ class PiggyBankTransformer extends AbstractTransformer
// grab notes
// continue with notes
$notes = Note::whereNoteableType(PiggyBank::class)->whereIn('noteable_id', array_keys($piggyBanks))->get();
/** @var Note $note */
foreach ($notes as $note) {
$id = $note->noteable_id;
@@ -139,11 +138,8 @@ class PiggyBankTransformer extends AbstractTransformer
/**
* Transform the piggy bank.
*
* @param PiggyBank $piggyBank
*
* @return array
* @throws FireflyException
* @throws JsonException
* @throws \JsonException
*/
public function transform(PiggyBank $piggyBank): array
{
@@ -233,20 +229,12 @@ class PiggyBankTransformer extends AbstractTransformer
'links' => [
[
'rel' => 'self',
'uri' => '/piggy_banks/' . $piggyBank->id,
'uri' => '/piggy_banks/'.$piggyBank->id,
],
],
];
}
/**
* @param string $currentAmount
* @param string $targetAmount
* @param Carbon|null $startDate
* @param Carbon|null $targetDate
*
* @return string
*/
private function getSuggestedMonthlyAmount(string $currentAmount, string $targetAmount, ?Carbon $startDate, ?Carbon $targetDate): string
{
$savePerMonth = '0';

View File

@@ -31,9 +31,6 @@ use Illuminate\Support\Collection;
*/
class PreferenceTransformer extends AbstractTransformer
{
/**
* @inheritDoc
*/
public function collectMetaData(Collection $objects): void
{
// TODO: Implement collectMetaData() method.
@@ -41,10 +38,6 @@ class PreferenceTransformer extends AbstractTransformer
/**
* Transform the preference
*
* @param Preference $preference
*
* @return array
*/
public function transform(Preference $preference): array
{

View File

@@ -35,7 +35,6 @@ use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use FireflyIII\Support\NullArrayObject;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use stdClass;
/**
* Class TransactionGroupTransformer
@@ -49,20 +48,19 @@ class TransactionGroupTransformer extends AbstractTransformer
private array $notes;
private array $tags;
/**
* @inheritDoc
*/
public function collectMetaData(Collection $objects): void
{
// start with currencies:
$currencies = [];
$journals = [];
/** @var array $object */
foreach ($objects as $object) {
foreach ($object['sums'] as $sum) {
$id = (int)$sum['currency_id'];
$currencies[$id] ??= TransactionCurrency::find($sum['currency_id']);
}
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
$id = (int)$transaction['transaction_journal_id'];
@@ -74,6 +72,7 @@ class TransactionGroupTransformer extends AbstractTransformer
// grab meta for all journals:
$meta = TransactionJournalMeta::whereIn('transaction_journal_id', array_keys($journals))->get();
/** @var TransactionJournalMeta $entry */
foreach ($meta as $entry) {
$id = $entry->transaction_journal_id;
@@ -82,6 +81,7 @@ class TransactionGroupTransformer extends AbstractTransformer
// grab all notes for all journals:
$notes = Note::whereNoteableType(TransactionJournal::class)->whereIn('noteable_id', array_keys($journals))->get();
/** @var Note $note */
foreach ($notes as $note) {
$id = $note->noteable_id;
@@ -90,10 +90,12 @@ class TransactionGroupTransformer extends AbstractTransformer
// grab all tags for all journals:
$tags = DB::table('tag_transaction_journal')
->leftJoin('tags', 'tags.id', 'tag_transaction_journal.tag_id')
->whereIn('tag_transaction_journal.transaction_journal_id', array_keys($journals))
->get(['tag_transaction_journal.transaction_journal_id', 'tags.tag']);
/** @var stdClass $tag */
->leftJoin('tags', 'tags.id', 'tag_transaction_journal.tag_id')
->whereIn('tag_transaction_journal.transaction_journal_id', array_keys($journals))
->get(['tag_transaction_journal.transaction_journal_id', 'tags.tag'])
;
/** @var \stdClass $tag */
foreach ($tags as $tag) {
$id = (int)$tag->transaction_journal_id;
$this->tags[$id][] = $tag->tag;
@@ -103,14 +105,10 @@ class TransactionGroupTransformer extends AbstractTransformer
$this->converter = new ExchangeRateConverter();
}
/**
* @param array $group
*
* @return array
*/
public function transform(array $group): array
{
$first = reset($group['transactions']);
return [
'id' => (string)$group['id'],
'created_at' => $first['created_at']->toAtomString(),
@@ -128,25 +126,19 @@ class TransactionGroupTransformer extends AbstractTransformer
];
}
/**
* @param array $transactions
*
* @return array
*/
private function transformTransactions(array $transactions): array
{
$return = [];
/** @var array $transaction */
foreach ($transactions as $transaction) {
$return[] = $this->transformTransaction($transaction);
}
return $return;
}
/**
* @param array $transaction
*
* @return array
* @throws FireflyException
*/
private function transformTransaction(array $transaction): array
@@ -258,16 +250,10 @@ class TransactionGroupTransformer extends AbstractTransformer
*
* Used to extract a value from the given array, and fall back on a sensible default or NULL
* if it can't be helped.
*
* @param NullArrayObject $array
* @param string $key
* @param string|null $default
*
* @return string|null
*/
private function stringFromArray(NullArrayObject $array, string $key, ?string $default): ?string
{
//app('log')->debug(sprintf('%s: %s', $key, var_export($array[$key], true)));
// app('log')->debug(sprintf('%s: %s', $key, var_export($array[$key], true)));
if (null === $array[$key] && null === $default) {
return null;
}
@@ -288,11 +274,6 @@ class TransactionGroupTransformer extends AbstractTransformer
return null;
}
/**
* @param string|null $string
*
* @return Carbon|null
*/
private function date(?string $string): ?Carbon
{
if (null === $string) {
@@ -304,6 +285,7 @@ class TransactionGroupTransformer extends AbstractTransformer
if (false === $res) {
return null;
}
return $res;
}
if (25 === strlen($string)) {
@@ -314,6 +296,7 @@ class TransactionGroupTransformer extends AbstractTransformer
if (false === $res) {
return null;
}
return $res;
}
@@ -322,6 +305,7 @@ class TransactionGroupTransformer extends AbstractTransformer
if (false === $res) {
return null;
}
return $res;
}
}

View File

@@ -1,6 +1,5 @@
<?php
/*
* UserGroupTransformer.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -38,29 +37,25 @@ class UserGroupTransformer extends AbstractTransformer
{
private array $memberships;
/**
*
*/
public function __construct()
{
$this->memberships = [];
}
/**
* @inheritDoc
*/
public function collectMetaData(Collection $objects): void
{
if (auth()->check()) {
// collect memberships so they can be listed in the group.
/** @var User $user */
$user = auth()->user();
/** @var UserGroup $userGroup */
foreach ($objects as $userGroup) {
$userGroupId = $userGroup->id;
$access = $user->hasRoleInGroupOrOwner($userGroup, UserRoleEnum::VIEW_MEMBERSHIPS) || $user->hasRole('owner');
if ($access) {
$groupMemberships = $userGroup->groupMemberships()->get();
/** @var GroupMembership $groupMembership */
foreach ($groupMemberships as $groupMembership) {
$this->memberships[$userGroupId][] = [
@@ -76,14 +71,10 @@ class UserGroupTransformer extends AbstractTransformer
/**
* Transform the user group.
*
* @param UserGroup $userGroup
*
* @return array
*/
public function transform(UserGroup $userGroup): array
{
$return = [
return [
'id' => $userGroup->id,
'created_at' => $userGroup->created_at->toAtomString(),
'updated_at' => $userGroup->updated_at->toAtomString(),
@@ -91,7 +82,5 @@ class UserGroupTransformer extends AbstractTransformer
'members' => $this->memberships[$userGroup->id] ?? [],
];
// if the user has a specific role in this group, then collect the memberships.
return $return;
}
}