<commit_before><?php

/**
 * TransactionUpdateRequest.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\Api\V1\Requests;

use FireflyIII\Models\TransactionGroup;
use FireflyIII\Rules\BelongsUser;
use FireflyIII\Rules\IsBoolean;
use FireflyIII\Rules\IsDateOrTime;
use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\Validation\GroupValidation;
use FireflyIII\Validation\TransactionValidation;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use Log;

/**
 * Class TransactionUpdateRequest
 */
class TransactionUpdateRequest extends FormRequest
{
    use TransactionValidation, GroupValidation, ConvertsDataTypes;

    private array $arrayFields;
    private array $booleanFields;
    private array $dateFields;
    private array $integerFields;
    private array $stringFields;
    private array $textareaFields;


    /**
     * Authorize logged in users.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        // Only allow authenticated users
        return auth()->check();
    }

    /**
     * Get all data. Is pretty complex because of all the ??-statements.
     *
     * @return array
     */
    public function getAll(): array
    {
        $this->integerFields = [
            'order',
            'currency_id',
            'foreign_currency_id',
            'transaction_journal_id',
            'source_id',
            'destination_id',
            'budget_id',
            'category_id',
            'bill_id',
            'recurrence_id',
        ];

        $this->dateFields = [
            'date',
            'interest_date',
            'book_date',
            'process_date',
            'due_date',
            'payment_date',
            'invoice_date',
        ];

        $this->textareaFields = [
            'notes',
        ];

        $this->stringFields  = [
            'type',
            'currency_code',
            'foreign_currency_code',
            'amount',
            'foreign_amount',
            'description',
            'source_name',
            'source_iban',
            'source_number',
            'source_bic',
            'destination_name',
            'destination_iban',
            'destination_number',
            'destination_bic',
            'budget_name',
            'category_name',
            'bill_name',
            'internal_reference',
            'external_id',
            'bunq_payment_id',
            'sepa_cc',
            'sepa_ct_op',
            'sepa_ct_id',
            'sepa_db',
            'sepa_country',
            'sepa_ep',
            'sepa_ci',
            'sepa_batch_id',
            'external_uri',
        ];
        $this->booleanFields = [
            'reconciled',
        ];

        $this->arrayFields = [
            'tags',
        ];


        $data = [
            'transactions' => $this->getTransactionData(),
            'apply_rules'  => $this->boolean('apply_rules', true),
        ];
        if ($this->has('group_title')) {
            $data['group_title'] = $this->string('group_title');
        }

        return $data;
    }

    /**
     * The rules that the incoming request must be matched against.
     *
     * @return array
     */
    public function rules(): array
    {
        return [
            // basic fields for group:
            'group_title'                          => 'between:1,1000',
            'apply_rules'                          => [new IsBoolean],

            // transaction rules (in array for splits):
            'transactions.*.type'                  => 'in:withdrawal,deposit,transfer,opening-balance,reconciliation',
            'transactions.*.date'                  => [new IsDateOrTime],
            'transactions.*.order'                 => 'numeric|min:0',

            // currency info
            'transactions.*.currency_id'           => 'numeric|exists:transaction_currencies,id',
            'transactions.*.currency_code'         => 'min:3|max:3|exists:transaction_currencies,code',
            'transactions.*.foreign_currency_id'   => 'numeric|exists:transaction_currencies,id',
            'transactions.*.foreign_currency_code' => 'min:3|max:3|exists:transaction_currencies,code',

            // amount
            'transactions.*.amount'                => 'numeric|gt:0|max:100000000000',
            'transactions.*.foreign_amount'        => 'numeric|gte:0',

            // description
            'transactions.*.description'           => 'nullable|between:1,1000',

            // source of transaction
            'transactions.*.source_id'             => ['numeric', 'nullable', new BelongsUser],
            'transactions.*.source_name'           => 'between:1,255|nullable',

            // destination of transaction
            'transactions.*.destination_id'        => ['numeric', 'nullable', new BelongsUser],<commit_msg>Fix #3715<commit_after><?php

/**
 * TransactionUpdateRequest.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\Api\V1\Requests;

use FireflyIII\Models\TransactionGroup;
use FireflyIII\Rules\BelongsUser;
use FireflyIII\Rules\IsBoolean;
use FireflyIII\Rules\IsDateOrTime;
use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\Validation\GroupValidation;
use FireflyIII\Validation\TransactionValidation;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use Log;

/**
 * Class TransactionUpdateRequest
 */
class TransactionUpdateRequest extends FormRequest
{
    use TransactionValidation, GroupValidation, ConvertsDataTypes;

    private array $arrayFields;
    private array $booleanFields;
    private array $dateFields;
    private array $integerFields;
    private array $stringFields;
    private array $textareaFields;


    /**
     * Authorize logged in users.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        // Only allow authenticated users
        return auth()->check();
    }

    /**
     * Get all data. Is pretty complex because of all the ??-statements.
     *
     * @return array
     */
    public function getAll(): array
    {
        $this->integerFields = [
            'order',
            'currency_id',
            'foreign_currency_id',
            'transaction_journal_id',
            'source_id',
            'destination_id',
            'budget_id',
            'category_id',
            'bill_id',
            'recurrence_id',
        ];

        $this->dateFields = [
            'date',
            'interest_date',
            'book_date',
            'process_date',
            'due_date',
            'payment_date',
            'invoice_date',
        ];

        $this->textareaFields = [
            'notes',
        ];

        $this->stringFields  = [
            'type',
            'currency_code',
            'foreign_currency_code',
            'amount',
            'foreign_amount',
            'description',
            'source_name',
            'source_iban',
            'source_number',
            'source_bic',
            'destination_name',
            'destination_iban',
            'destination_number',
            'destination_bic',
            'budget_name',
            'category_name',
            'bill_name',
            'internal_reference',
            'external_id',
            'bunq_payment_id',
            'sepa_cc',
            'sepa_ct_op',
            'sepa_ct_id',
            'sepa_db',
            'sepa_country',
            'sepa_ep',
            'sepa_ci',
            'sepa_batch_id',
            'external_uri',
        ];
        $this->booleanFields = [
            'reconciled',
        ];

        $this->arrayFields = [
            'tags',
        ];


        $data = [
            'transactions' => $this->getTransactionData(),
            'apply_rules'  => $this->boolean('apply_rules', true),
        ];
        if ($this->has('group_title')) {
            $data['group_title'] = $this->string('group_title');
        }

        return $data;
    }

    /**
     * The rules that the incoming request must be matched against.
     *
     * @return array
     */
    public function rules(): array
    {
        return [
            // basic fields for group:
            'group_title'                          => 'between:1,1000',
            'apply_rules'                          => [new IsBoolean],

            // transaction rules (in array for splits):
            'transactions.*.type'                  => 'in:withdrawal,deposit,transfer,opening-balance,reconciliation',
            'transactions.*.date'                  => [new IsDateOrTime],
            'transactions.*.order'                 => 'numeric|min:0',

            // currency info
            'transactions.*.currency_id'           => 'numeric|exists:transaction_currencies,id',
            'transactions.*.currency_code'         => 'min:3|max:3|exists:transaction_currencies,code',
            'transactions.*.foreign_currency_id'   => 'nullable|numeric|exists:transaction_currencies,id',
            'transactions.*.foreign_currency_code' => 'nullable|min:3|max:3|exists:transaction_currencies,code',

            // amount
            'transactions.*.amount'                => 'numeric|gt:0|max:100000000000',
            'transactions.*.foreign_amount'        => 'nullable|numeric|gte:0',

            // description
            'transactions.*.description'           => 'nullable|between:1,1000',

            // source of transaction
            'transactions.*.source_id'             => ['numeric', 'nullable', new BelongsUser],
            'transactions.*.source_name'           => 'between:1,255|nullable',

            // destination of transaction
            'transactions.*.destination_id'        => ['numeric', 'nullable', new BelongsUser],
{'fraction_non_alphanumeric': 0.11313772138514407, 'fraction_numerical': 0.007401533174729051, 'mean_word_length': 1.9042988741044011, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 63, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5928931', 'n_tokens_mistral': 3106, 'n_tokens_neox': 2998, 'n_words': 684}