firefly-iii/app/Validation/CurrencyValidation.php

83 lines
2.9 KiB
PHP
Raw Normal View History

2020-03-21 00:00:36 -05:00
<?php
2020-06-30 12:05:35 -05:00
2020-03-21 00:00:36 -05:00
/**
* CurrencyValidation.php
2020-06-30 12:05:35 -05:00
* Copyright (c) 2020 james@firefly-iii.org
2020-03-21 00:00:36 -05:00
*
* 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/>.
*/
2020-06-30 12:05:35 -05:00
declare(strict_types=1);
2020-03-21 00:00:36 -05:00
namespace FireflyIII\Validation;
2021-04-06 01:51:27 -05:00
2023-05-29 06:56:55 -05:00
use Illuminate\Validation\Validator;
2020-03-21 00:00:36 -05:00
/**
* Trait CurrencyValidation
*
* This trait contains validation methods that have to do with currencies.
*/
trait CurrencyValidation
{
2023-12-02 05:56:48 -06:00
public const string TEST = 'Test';
2023-02-22 11:14:14 -06:00
2020-03-21 00:00:36 -05:00
/**
* If the transactions contain foreign amounts, there must also be foreign currency information.
*/
protected function validateForeignCurrencyInformation(Validator $validator): void
{
2023-03-08 23:33:23 -06:00
if ($validator->errors()->count() > 0) {
return;
}
2023-10-29 00:33:43 -05:00
app('log')->debug('Now in validateForeignCurrencyInformation()');
2020-03-21 00:00:36 -05:00
$transactions = $this->getTransactionsArray($validator);
foreach ($transactions as $index => $transaction) {
2023-01-29 08:29:42 -06:00
if (!is_array($transaction)) {
2023-01-21 05:21:06 -06:00
continue;
}
2020-03-21 00:00:36 -05:00
// if foreign amount is present, then the currency must be as well.
2021-04-06 01:51:27 -05:00
if (array_key_exists('foreign_amount', $transaction)
&& !(array_key_exists('foreign_currency_id', $transaction)
|| array_key_exists(
2022-10-30 08:24:37 -05:00
'foreign_currency_code',
$transaction
2021-04-06 01:51:27 -05:00
))
2020-03-21 00:00:36 -05:00
&& 0 !== bccomp('0', $transaction['foreign_amount'])
) {
$validator->errors()->add(
2023-12-20 12:35:52 -06:00
'transactions.'.$index.'.foreign_amount',
2022-12-29 12:42:40 -06:00
(string)trans('validation.require_currency_info')
2020-03-21 00:00:36 -05:00
);
}
// if the currency is present, then the amount must be present as well.
2021-04-06 01:51:27 -05:00
if ((array_key_exists('foreign_currency_id', $transaction) || array_key_exists('foreign_currency_code', $transaction))
&& !array_key_exists(
2022-10-30 08:24:37 -05:00
'foreign_amount',
$transaction
2021-04-06 01:51:27 -05:00
)) {
2020-03-21 00:00:36 -05:00
$validator->errors()->add(
2023-12-20 12:35:52 -06:00
'transactions.'.$index.'.foreign_amount',
2022-12-29 12:42:40 -06:00
(string)trans('validation.require_currency_amount')
2020-03-21 00:00:36 -05:00
);
}
}
}
2023-06-21 05:34:58 -05:00
abstract protected function getTransactionsArray(Validator $validator): array;
}