<commit_before><?php
/**
 * TransactionJournal.php
 * Copyright (c) 2017 <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\Support\Twig\Extension;

use Carbon\Carbon;
use FireflyIII\Models\Transaction as TransactionModel;
use FireflyIII\Models\TransactionJournal as JournalModel;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Twig_Extension;

/**
 * Class TransactionJournal
 */
class TransactionJournal extends Twig_Extension
{
    /**
     * @param JournalModel $journal
     * @param string       $field
     *
     * @return null|Carbon
     */
    public function getMetaDate(JournalModel $journal, string $field): ?Carbon
    {
        /** @var JournalRepositoryInterface $repository */
        $repository = app(JournalRepositoryInterface::class);

        return $repository->getMetaDate($journal, $field);
    }

    /**
     * @param JournalModel $journal
     * @param string       $field
     *
     * @return string
     */
    public function getMetaField(JournalModel $journal, string $field): string
    {
        /** @var JournalRepositoryInterface $repository */
        $repository = app(JournalRepositoryInterface::class);

        return $repository->getMetaField($journal, $field);
    }

    /**
     * @param JournalModel $journal
     *
     * @return string
     */
    public function totalAmount(JournalModel $journal): string
    {
        $transactions = $journal->transactions()->where('amount', '>', 0)->get();
        $totals       = [];
        $type         = $journal->transactionType->type;
        /** @var TransactionModel $transaction */
        foreach ($transactions as $transaction) {
            $currencyId = $transaction->transaction_currency_id;
            $currency   = $transaction->transactionCurrency;

            if (!isset($totals[$currencyId])) {
                $totals[$currencyId] = [
                    'amount'   => '0',
                    'currency' => $currency,
                ];
            }
            $totals[$currencyId]['amount'] = bcadd($transaction->amount, $totals[$currencyId]['amount']);

            if (null !== $transaction->foreign_currency_id) {
                $foreignId = $transaction->foreign_currency_id;
                $foreign   = $transaction->foreignCurrency;
                if (!isset($totals[$foreignId])) {
                    $totals[$foreignId] = [<commit_msg>Catch null pointer [skip ci]<commit_after><?php
/**
 * TransactionJournal.php
 * Copyright (c) 2017 <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\Support\Twig\Extension;

use Carbon\Carbon;
use FireflyIII\Models\Transaction as TransactionModel;
use FireflyIII\Models\TransactionJournal as JournalModel;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Twig_Extension;

/**
 * Class TransactionJournal
 */
class TransactionJournal extends Twig_Extension
{
    /**
     * @param JournalModel $journal
     * @param string       $field
     *
     * @return null|Carbon
     */
    public function getMetaDate(JournalModel $journal, string $field): ?Carbon
    {
        /** @var JournalRepositoryInterface $repository */
        $repository = app(JournalRepositoryInterface::class);

        return $repository->getMetaDate($journal, $field);
    }

    /**
     * @param JournalModel $journal
     * @param string       $field
     *
     * @return string
     */
    public function getMetaField(JournalModel $journal, string $field): string
    {
        /** @var JournalRepositoryInterface $repository */
        $repository = app(JournalRepositoryInterface::class);
        $result     = $repository->getMetaField($journal, $field);
        if (is_null($result)) {
            return '';
        }

        return $result;
    }

    /**
     * @param JournalModel $journal
     *
     * @return string
     */
    public function totalAmount(JournalModel $journal): string
    {
        $transactions = $journal->transactions()->where('amount', '>', 0)->get();
        $totals       = [];
        $type         = $journal->transactionType->type;
        /** @var TransactionModel $transaction */
        foreach ($transactions as $transaction) {
            $currencyId = $transaction->transaction_currency_id;
            $currency   = $transaction->transactionCurrency;

            if (!isset($totals[$currencyId])) {
                $totals[$currencyId] = [
                    'amount'   => '0',
                    'currency' => $currency,
                ];
            }
            $totals[$currencyId]['amount'] = bcadd($transaction->amount, $totals[$currencyId]['amount']);

            if (null !== $transaction->foreign_currency_id) {
                $foreignId = $transaction->foreign_currency_id;
                $foreign   = $transaction->foreignCurrency;
                if (!isset($totals[$foreignId])) {
                    $totals[$foreignId] = [
{'fraction_non_alphanumeric': 0.11228512852862324, 'fraction_numerical': 0.0025232613152499604, 'mean_word_length': 2.670138888888889, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, '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': '7052458', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1604, 'n_words': 469}