firefly-iii/app/Validation/Api/Data/Bulk/ValidatesBulkTransactionQuery.php

78 lines
2.8 KiB
PHP
Raw Normal View History

2021-08-10 11:43:21 -05:00
<?php
2021-08-10 12:31:55 -05:00
/*
* ValidatesBulkTransactionQuery.php
* Copyright (c) 2021 james@firefly-iii.org
*
* 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);
2021-08-10 11:43:21 -05:00
namespace FireflyIII\Validation\Api\Data\Bulk;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Validation\Validator;
2021-09-17 22:26:31 -05:00
use JsonException;
2021-08-10 11:43:21 -05:00
/**
*
*/
trait ValidatesBulkTransactionQuery
{
/**
* @param Validator $validator
2021-09-17 22:26:31 -05:00
*
* @throws JsonException
2021-08-10 11:43:21 -05:00
*/
protected function validateTransactionQuery(Validator $validator): void
{
$data = $validator->getData();
// assumption is all validation has already taken place and the query key exists.
2021-09-17 22:26:31 -05:00
$json = json_decode($data['query'], true, 8, JSON_THROW_ON_ERROR);
2021-08-10 11:43:21 -05:00
2021-09-17 22:26:31 -05:00
if (array_key_exists('account_id', $json['where'])
&& array_key_exists('account_id', $json['update'])
2021-08-10 11:43:21 -05:00
) {
// find both accounts, must be same type.
2021-08-10 11:43:21 -05:00
// already validated: belongs to this user.
$repository = app(AccountRepositoryInterface::class);
2022-03-29 08:00:29 -05:00
$source = $repository->find((int) $json['where']['account_id']);
$dest = $repository->find((int) $json['update']['account_id']);
2021-08-10 11:43:21 -05:00
if (null === $source) {
2022-03-29 08:00:29 -05:00
$validator->errors()->add('query', sprintf((string) trans('validation.invalid_query_data'), 'where', 'account_id'));
2021-08-10 11:43:21 -05:00
return;
}
if (null === $dest) {
2022-03-29 08:00:29 -05:00
$validator->errors()->add('query', sprintf((string) trans('validation.invalid_query_data'), 'update', 'account_id'));
2021-08-10 11:43:21 -05:00
return;
}
if ($source->accountType->type !== $dest->accountType->type) {
2022-03-29 08:00:29 -05:00
$validator->errors()->add('query', (string) trans('validation.invalid_query_account_type'));
2021-08-10 11:43:21 -05:00
return;
}
// must have same currency:
if ($repository->getAccountCurrency($source)->id !== $repository->getAccountCurrency($dest)->id) {
2022-03-29 08:00:29 -05:00
$validator->errors()->add('query', (string) trans('validation.invalid_query_currency'));
2021-08-10 11:43:21 -05:00
}
}
}
2021-08-10 12:31:55 -05:00
}