firefly-iii/app/Transformers/PiggyBankEventTransformer.php

101 lines
3.6 KiB
PHP
Raw Normal View History

<?php
/**
* PiggyBankEventTransformer.php
2020-02-16 06:57:18 -06:00
* Copyright (c) 2019 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\Transformers;
use FireflyIII\Models\PiggyBankEvent;
2018-07-22 09:35:46 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-02-17 03:47:06 -06:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2018-12-19 22:46:05 -06:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
/**
* Class PiggyBankEventTransformer
*/
class PiggyBankEventTransformer extends AbstractTransformer
{
2021-03-06 05:45:49 -06:00
private CurrencyRepositoryInterface $currencyRepos;
private PiggyBankRepositoryInterface $piggyRepos;
private AccountRepositoryInterface $repository;
2018-12-19 22:46:05 -06:00
/**
2018-02-17 03:47:06 -06:00
* PiggyBankEventTransformer constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
2018-12-19 22:46:05 -06:00
$this->repository = app(AccountRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
$this->piggyRepos = app(PiggyBankRepositoryInterface::class);
}
2018-02-17 03:47:06 -06:00
/**
* Convert piggy bank event.
*
* @param PiggyBankEvent $event
*
* @return array
*/
public function transform(PiggyBankEvent $event): array
{
2018-12-19 22:46:05 -06:00
// get account linked to piggy bank
2018-07-22 09:35:46 -05:00
$account = $event->piggyBank->account;
2018-12-19 22:46:05 -06:00
// set up repositories.
$this->repository->setUser($account->user);
$this->currencyRepos->setUser($account->user);
$this->piggyRepos->setUser($account->user);
// get associated currency or fall back to the default:
2020-07-12 10:32:48 -05:00
$currency = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
2018-12-19 22:46:05 -06:00
// get associated journal and transaction, if any:
$journalId = $event->transaction_journal_id;
2019-09-22 15:10:39 -05:00
$groupId = null;
2022-03-29 08:00:29 -05:00
if (0 !== (int) $journalId) {
$groupId = (int) $event->transactionJournal->transaction_group_id;
$journalId = (int) $journalId;
2019-09-22 15:10:39 -05:00
}
2021-03-06 05:45:49 -06:00
2020-10-23 12:11:25 -05:00
return [
2022-03-29 08:00:29 -05:00
'id' => (string) $event->id,
2018-12-19 22:46:05 -06:00
'created_at' => $event->created_at->toAtomString(),
'updated_at' => $event->updated_at->toAtomString(),
2022-03-29 08:00:29 -05:00
'amount' => number_format((float) $event->amount, $currency->decimal_places, '.', ''),
'currency_id' => (string) $currency->id,
2018-12-19 22:46:05 -06:00
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
2022-03-29 08:00:29 -05:00
'currency_decimal_places' => (int) $currency->decimal_places,
'transaction_journal_id' => $journalId ? (string) $journalId : null,
'transaction_group_id' => $groupId ? (string) $groupId : null,
2018-12-19 22:46:05 -06:00
'links' => [
[
'rel' => 'self',
'uri' => '/piggy_bank_events/' . $event->id,
],
],
];
}
2018-03-05 12:35:58 -06:00
}