<commit_before><?php
/**
 * BillTransformer.php
 * Copyright (c) 2019 <EMAIL>
 *
 * 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 Carbon\Carbon;
use FireflyIII\Models\Bill;
use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;

/**
 * Class BillTransformer
 */
class BillTransformer extends AbstractTransformer
{
    /** @var BillRepositoryInterface */
    private $repository;

    /**
     * BillTransformer constructor.
     *
     * @codeCoverageIgnore
     */
    public function __construct()
    {
        $this->repository = app(BillRepositoryInterface::class);
    }

    /**
     * Transform the bill.
     *
     * @param Bill $bill
     *
     * @return array
     */
    public function transform(Bill $bill): array
    {
        $paidData = $this->paidData($bill);
        $payDates = $this->payDates($bill);

        $currency = $bill->transactionCurrency;
        $notes    = $this->repository->getNoteText($bill);
        $notes    = '' === $notes ? null : $notes;
        $this->repository->setUser($bill->user);

        $objectGroupId    = null;
        $objectGroupOrder = null;
        $objectGroupTitle = null;
        /** @var ObjectGroup $objectGroup */
        $objectGroup = $bill->objectGroups->first();
        if (null !== $objectGroup) {
            $objectGroupId    = (int)$objectGroup->id;
            $objectGroupOrder = (int)$objectGroup->order;
            $objectGroupTitle = $objectGroup->title;
        }

        $paidDataFormatted = [];
        $payDatesFormatted = [];
        foreach($paidData['paid_dates'] as $object) {
            $object['date'] = Carbon::createFromFormat('!Y-m-d', $object['date'], config('app.timezone'))->toAtomString();
                $paidDataFormatted[] = $object;
        }

        foreach ($payDates as $string) {
            $payDatesFormatted[] = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone'))->toAtomString();
        }

        return [
            'id'                      => (int)$bill->id,
            'created_at'              => $bill->created_at->toAtomString(),
            'updated_at'              => $bill->updated_at->toAtomString(),
            'currency_id'             => (string)$bill->transaction_currency_id,
            'currency_code'           => $currency->code,
            'currency_symbol'         => $currency->symbol,
            'currency_decimal_places' => (int)$currency->decimal_places,
            'name'                    => $bill->name,
            'amount_min'              => number_format((float)$bill->amount_min, $currency->decimal_places, '.', ''),
            'amount_max'              => number_format((float)$bill->amount_max, $currency->decimal_places, '.', ''),
            'date'                    => $bill->date->toAtomString(),
            'repeat_freq'             => $bill->repeat_freq,
            'skip'                    => (int)$bill->skip,
            'active'                  => $bill->active,
            'order'                   => (int)$bill->order,
            'notes'                   => $notes,
            'next_expected_match'     => Carbon::createFromFormat('!Y-m-d', $paidData['next_expected_match'], config('app.timezone'))->toAtomString(),
            'pay_dates'               => $payDatesFormatted,
            'paid_dates'              => $paidDataFormatted,
            'object_group_id'         => $objectGroupId ? (string)$objectGroupId : null,
            'object_group_order'      => $objectGroupOrder,
            'object_group_title'      => $objectGroupTitle,
            'links'                   => [
                [
                    'rel' => 'self',
                    'uri' => '/bills/' . $bill->id,
                ],
            ],
        ];<commit_msg>Fix nullpointer in bill transformer.<commit_after><?php
/**
 * BillTransformer.php
 * Copyright (c) 2019 <EMAIL>
 *
 * 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 Carbon\Carbon;
use FireflyIII\Models\Bill;
use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;

/**
 * Class BillTransformer
 */
class BillTransformer extends AbstractTransformer
{
    /** @var BillRepositoryInterface */
    private $repository;

    /**
     * BillTransformer constructor.
     *
     * @codeCoverageIgnore
     */
    public function __construct()
    {
        $this->repository = app(BillRepositoryInterface::class);
    }

    /**
     * Transform the bill.
     *
     * @param Bill $bill
     *
     * @return array
     */
    public function transform(Bill $bill): array
    {
        $paidData = $this->paidData($bill);
        $payDates = $this->payDates($bill);

        $currency = $bill->transactionCurrency;
        $notes    = $this->repository->getNoteText($bill);
        $notes    = '' === $notes ? null : $notes;
        $this->repository->setUser($bill->user);

        $objectGroupId    = null;
        $objectGroupOrder = null;
        $objectGroupTitle = null;
        /** @var ObjectGroup $objectGroup */
        $objectGroup = $bill->objectGroups->first();
        if (null !== $objectGroup) {
            $objectGroupId    = (int)$objectGroup->id;
            $objectGroupOrder = (int)$objectGroup->order;
            $objectGroupTitle = $objectGroup->title;
        }

        $paidDataFormatted = [];
        $payDatesFormatted = [];
        foreach($paidData['paid_dates'] as $object) {
            $object['date'] = Carbon::createFromFormat('!Y-m-d', $object['date'], config('app.timezone'))->toAtomString();
                $paidDataFormatted[] = $object;
        }

        foreach ($payDates as $string) {
            $payDatesFormatted[] = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone'))->toAtomString();
        }
        $nextExpectedMatch = null;
        if(null !== $paidData['next_expected_match'] ) {
            $nextExpectedMatch = Carbon::createFromFormat('!Y-m-d', $paidData['next_expected_match'], config('app.timezone'))->toAtomString();
        }

        return [
            'id'                      => (int)$bill->id,
            'created_at'              => $bill->created_at->toAtomString(),
            'updated_at'              => $bill->updated_at->toAtomString(),
            'currency_id'             => (string)$bill->transaction_currency_id,
            'currency_code'           => $currency->code,
            'currency_symbol'         => $currency->symbol,
            'currency_decimal_places' => (int)$currency->decimal_places,
            'name'                    => $bill->name,
            'amount_min'              => number_format((float)$bill->amount_min, $currency->decimal_places, '.', ''),
            'amount_max'              => number_format((float)$bill->amount_max, $currency->decimal_places, '.', ''),
            'date'                    => $bill->date->toAtomString(),
            'repeat_freq'             => $bill->repeat_freq,
            'skip'                    => (int)$bill->skip,
            'active'                  => $bill->active,
            'order'                   => (int)$bill->order,
            'notes'                   => $notes,
            'next_expected_match'     => $nextExpectedMatch,
            'pay_dates'               => $payDatesFormatted,
            'paid_dates'              => $paidDataFormatted,
            'object_group_id'         => $objectGroupId ? (string)$objectGroupId : null,
            'object_group_order'      => $objectGroupOrder,
            'object_group_title'      => $objectGroupTitle,
            'links'                   => [
                [
                    'rel' => 'self',
                    'uri' => '/bills/' . $bill->id,
                ],
            ],
        ];
{'fraction_non_alphanumeric': 0.13456240728968002, 'fraction_numerical': 0.0012714558169103624, 'mean_word_length': 2.062621674237508, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 133, 'https://': 4, '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': '4023741', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2545, 'n_words': 552}