<commit_before><?php
/**
 * PiggyBankTransformer.php
 * Copyright (c) 2018 <EMAIL>
 *
 * This file is part of Firefly III.
 *
 * Firefly III is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Firefly III 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace FireflyIII\Transformers;


use FireflyIII\Models\Account;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Log;

/**
 * Class PiggyBankTransformer
 */
class PiggyBankTransformer extends AbstractTransformer
{
    /** @var AccountRepositoryInterface */
    private $accountRepos;
    /** @var CurrencyRepositoryInterface */
    private $currencyRepos;
    /** @var PiggyBankRepositoryInterface */
    private $piggyRepos;

    /**
     * PiggyBankTransformer constructor.
     *
     * @codeCoverageIgnore
     */
    public function __construct()
    {
        $this->accountRepos  = app(AccountRepositoryInterface::class);
        $this->currencyRepos = app(CurrencyRepositoryInterface::class);
        $this->piggyRepos    = app(PiggyBankRepositoryInterface::class);
        if ('testing' === config('app.env')) {
            Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
        }
    }


    /**
     * Transform the piggy bank.
     *
     * @param PiggyBank $piggyBank
     *
     * @return array
     */
    public function transform(PiggyBank $piggyBank): array
    {
        /** @var Account $account */
        $account = $piggyBank->account;

        // set up repositories
        $this->accountRepos->setUser($account->user);
        $this->currencyRepos->setUser($account->user);
        $this->piggyRepos->setUser($account->user);

        // get currency from account, or use default.
        $currencyId = (int)$this->accountRepos->getMetaValue($account, 'currency_id');
        $currency   = $this->currencyRepos->findNull($currencyId);
        if (null === $currency) {
            $currency = app('amount')->getDefaultCurrencyByUser($account->user);
        }

        // note
        $notes = $this->piggyRepos->getNoteText($piggyBank);
        $notes = '' === $notes ? null : $notes;

        // get currently saved amount:
        $currentAmountStr = $this->piggyRepos->getCurrentAmount($piggyBank);
        $currentAmount    = round($currentAmountStr, $currency->decimal_places);

        // left to save:
        $leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr);
        $startDate  = null === $piggyBank->startdate ? null : $piggyBank->startdate->format('Y-m-d');
        $targetDate = null === $piggyBank->targetdate ? null : $piggyBank->targetdate->format('Y-m-d');

        // target and percentage:
        $targetAmount = round($piggyBank->targetamount, $currency->decimal_places);
        $percentage   = (int)(0 !== bccomp('0', $currentAmountStr) ? $currentAmount / $targetAmount * 100 : 0);
        $data         = [
            'id'                      => (int)$piggyBank->id,
            'created_at'              => $piggyBank->created_at->toAtomString(),
            'updated_at'              => $piggyBank->updated_at->toAtomString(),
            'account_id'              => $piggyBank->account_id,
            'name'                    => $piggyBank->name,
            'currency_id'             => $currency->id,
            'currency_code'           => $currency->code,
            'currency_symbol'         => $currency->symbol,<commit_msg>Add account name to piggy bank transformer API.<commit_after><?php
/**
 * PiggyBankTransformer.php
 * Copyright (c) 2018 <EMAIL>
 *
 * This file is part of Firefly III.
 *
 * Firefly III is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Firefly III 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace FireflyIII\Transformers;


use FireflyIII\Models\Account;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Log;

/**
 * Class PiggyBankTransformer
 */
class PiggyBankTransformer extends AbstractTransformer
{
    /** @var AccountRepositoryInterface */
    private $accountRepos;
    /** @var CurrencyRepositoryInterface */
    private $currencyRepos;
    /** @var PiggyBankRepositoryInterface */
    private $piggyRepos;

    /**
     * PiggyBankTransformer constructor.
     *
     * @codeCoverageIgnore
     */
    public function __construct()
    {
        $this->accountRepos  = app(AccountRepositoryInterface::class);
        $this->currencyRepos = app(CurrencyRepositoryInterface::class);
        $this->piggyRepos    = app(PiggyBankRepositoryInterface::class);
        if ('testing' === config('app.env')) {
            Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
        }
    }


    /**
     * Transform the piggy bank.
     *
     * @param PiggyBank $piggyBank
     *
     * @return array
     */
    public function transform(PiggyBank $piggyBank): array
    {
        /** @var Account $account */
        $account = $piggyBank->account;

        // set up repositories
        $this->accountRepos->setUser($account->user);
        $this->currencyRepos->setUser($account->user);
        $this->piggyRepos->setUser($account->user);

        // get currency from account, or use default.
        $currencyId = (int)$this->accountRepos->getMetaValue($account, 'currency_id');
        $currency   = $this->currencyRepos->findNull($currencyId);
        if (null === $currency) {
            $currency = app('amount')->getDefaultCurrencyByUser($account->user);
        }

        // note
        $notes = $this->piggyRepos->getNoteText($piggyBank);
        $notes = '' === $notes ? null : $notes;

        // get currently saved amount:
        $currentAmountStr = $this->piggyRepos->getCurrentAmount($piggyBank);
        $currentAmount    = round($currentAmountStr, $currency->decimal_places);

        // left to save:
        $leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr);
        $startDate  = null === $piggyBank->startdate ? null : $piggyBank->startdate->format('Y-m-d');
        $targetDate = null === $piggyBank->targetdate ? null : $piggyBank->targetdate->format('Y-m-d');

        // target and percentage:
        $targetAmount = round($piggyBank->targetamount, $currency->decimal_places);
        $percentage   = (int)(0 !== bccomp('0', $currentAmountStr) ? $currentAmount / $targetAmount * 100 : 0);
        $data         = [
            'id'                      => (int)$piggyBank->id,
            'created_at'              => $piggyBank->created_at->toAtomString(),
            'updated_at'              => $piggyBank->updated_at->toAtomString(),
            'account_id'              => $piggyBank->account_id,
            'account_name'            => $piggyBank->account->name,
            'name'                    => $piggyBank->name,
            'currency_id'             => $currency->id,
            'currency_code'           => $currency->code,
            'currency_symbol'         => $currency->symbol,
{'fraction_non_alphanumeric': 0.1258633007859014, 'fraction_numerical': 0.0028578232912598238, 'mean_word_length': 2.7279183311140702, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1775667', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2373, 'n_words': 558}