2018-06-23 23:51:22 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* IsValidAttachmentModel.php
|
2020-02-16 06:56:25 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-06-23 23:51:22 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-06-23 23:51:22 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* 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.
|
2018-06-23 23:51:22 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-06-23 23:51:22 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-06-23 23:51:22 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* 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/>.
|
2018-06-23 23:51:22 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Rules;
|
2020-03-18 15:36:26 -05:00
|
|
|
use FireflyIII\Models\Account;
|
2018-12-21 03:11:18 -06:00
|
|
|
use FireflyIII\Models\Bill;
|
2020-03-18 15:36:26 -05:00
|
|
|
use FireflyIII\Models\Budget;
|
|
|
|
use FireflyIII\Models\Category;
|
|
|
|
use FireflyIII\Models\PiggyBank;
|
|
|
|
use FireflyIII\Models\Tag;
|
2018-12-21 03:11:18 -06:00
|
|
|
use FireflyIII\Models\Transaction;
|
2018-06-23 23:51:22 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2020-03-18 15:36:26 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2018-12-21 03:11:18 -06:00
|
|
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
2020-03-18 15:36:26 -05:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
2019-08-10 06:42:33 -05:00
|
|
|
use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;
|
2018-06-23 23:51:22 -05:00
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
2020-03-18 15:36:26 -05:00
|
|
|
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
2018-06-23 23:51:22 -05:00
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
2018-12-21 09:38:10 -06:00
|
|
|
use Log;
|
2018-06-23 23:51:22 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class IsValidAttachmentModel
|
|
|
|
*/
|
|
|
|
class IsValidAttachmentModel implements Rule
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
private $model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IsValidAttachmentModel constructor.
|
|
|
|
*
|
2019-07-31 09:53:09 -05:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2018-06-23 23:51:22 -05:00
|
|
|
* @param string $model
|
|
|
|
*/
|
|
|
|
public function __construct(string $model)
|
|
|
|
{
|
2019-07-31 09:53:09 -05:00
|
|
|
$model = $this->normalizeModel($model);
|
2018-06-23 23:51:22 -05:00
|
|
|
$this->model = $model;
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
/**
|
|
|
|
* @param string $model
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function normalizeModel(string $model): string
|
|
|
|
{
|
|
|
|
$search = ['FireflyIII\Models\\'];
|
|
|
|
$replace = '';
|
|
|
|
$model = str_replace($search, $replace, $model);
|
|
|
|
|
|
|
|
return sprintf('FireflyIII\Models\%s', $model);
|
|
|
|
}
|
|
|
|
|
2018-06-23 23:51:22 -05:00
|
|
|
/**
|
|
|
|
* Get the validation error message.
|
2021-03-21 03:15:40 -05:00
|
|
|
*
|
2019-07-31 09:53:09 -05:00
|
|
|
* @codeCoverageIgnore
|
2018-06-23 23:51:22 -05:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function message(): string
|
|
|
|
{
|
2018-07-15 02:38:49 -05:00
|
|
|
return (string)trans('validation.model_id_invalid');
|
2018-06-23 23:51:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the validation rule passes.
|
|
|
|
*
|
2021-03-21 03:15:40 -05:00
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $value
|
2018-06-23 23:51:22 -05:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function passes($attribute, $value): bool
|
|
|
|
{
|
|
|
|
if (!auth()->check()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-18 15:36:26 -05:00
|
|
|
$methods = [
|
|
|
|
Account::class => 'validateAccount',
|
|
|
|
Bill::class => 'validateBill',
|
|
|
|
Budget::class => 'validateBudget',
|
|
|
|
Category::class => 'validateCategory',
|
|
|
|
PiggyBank::class => 'validatePiggyBank',
|
|
|
|
Tag::class => 'validateTag',
|
|
|
|
Transaction::class => 'validateTransaction',
|
|
|
|
TransactionJournal::class => 'validateJournal',
|
|
|
|
];
|
|
|
|
if (!isset($methods[$this->model])) {
|
|
|
|
Log::error(sprintf('Cannot validate model "%s" in %s.', $this->model, __METHOD__));
|
2018-06-23 23:51:22 -05:00
|
|
|
|
2020-03-18 15:36:26 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$method = $methods[$this->model];
|
2019-07-31 09:53:09 -05:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
return $this->$method((int)$value);
|
2020-03-18 15:36:26 -05:00
|
|
|
}
|
2018-12-21 03:11:18 -06:00
|
|
|
|
2020-03-18 15:36:26 -05:00
|
|
|
/**
|
|
|
|
* @param int $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
private function validateAccount(int $value): bool
|
2020-03-18 15:36:26 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
2020-03-18 15:36:26 -05:00
|
|
|
$repository->setUser(auth()->user());
|
2018-12-21 03:11:18 -06:00
|
|
|
|
2020-03-18 15:36:26 -05:00
|
|
|
return null !== $repository->findNull($value);
|
|
|
|
}
|
2018-12-21 03:11:18 -06:00
|
|
|
|
2020-03-18 15:36:26 -05:00
|
|
|
/**
|
|
|
|
* @param int $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
private function validateBill(int $value): bool
|
2020-03-18 15:36:26 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
/** @var BillRepositoryInterface $repository */
|
|
|
|
$repository = app(BillRepositoryInterface::class);
|
2020-03-18 15:36:26 -05:00
|
|
|
$repository->setUser(auth()->user());
|
2018-12-21 03:11:18 -06:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
return null !== $repository->find($value);
|
2020-03-18 15:36:26 -05:00
|
|
|
}
|
2019-08-10 06:42:33 -05:00
|
|
|
|
2020-03-18 15:36:26 -05:00
|
|
|
/**
|
|
|
|
* @param int $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function validateBudget(int $value): bool
|
|
|
|
{
|
|
|
|
/** @var BudgetRepositoryInterface $repository */
|
|
|
|
$repository = app(BudgetRepositoryInterface::class);
|
|
|
|
$repository->setUser(auth()->user());
|
2018-12-21 03:11:18 -06:00
|
|
|
|
2020-03-18 15:36:26 -05:00
|
|
|
return null !== $repository->findNull($value);
|
|
|
|
}
|
2018-07-25 23:10:17 -05:00
|
|
|
|
2020-03-18 15:36:26 -05:00
|
|
|
/**
|
|
|
|
* @param int $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function validateCategory(int $value): bool
|
|
|
|
{
|
|
|
|
/** @var CategoryRepositoryInterface $repository */
|
|
|
|
$repository = app(CategoryRepositoryInterface::class);
|
|
|
|
$repository->setUser(auth()->user());
|
2018-07-25 23:10:17 -05:00
|
|
|
|
2020-03-18 15:36:26 -05:00
|
|
|
return null !== $repository->findNull($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
private function validateJournal(int $value): bool
|
2020-03-18 15:36:26 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
$repository = app(JournalRepositoryInterface::class);
|
2020-03-18 15:36:26 -05:00
|
|
|
$repository->setUser(auth()->user());
|
|
|
|
|
|
|
|
return null !== $repository->findNull($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
private function validatePiggyBank(int $value): bool
|
2020-03-18 15:36:26 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
/** @var PiggyBankRepositoryInterface $repository */
|
|
|
|
$repository = app(PiggyBankRepositoryInterface::class);
|
2020-03-18 15:36:26 -05:00
|
|
|
$repository->setUser(auth()->user());
|
|
|
|
|
|
|
|
return null !== $repository->findNull($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
private function validateTag(int $value): bool
|
2020-03-18 15:36:26 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
/** @var TagRepositoryInterface $repository */
|
|
|
|
$repository = app(TagRepositoryInterface::class);
|
2020-03-18 15:36:26 -05:00
|
|
|
$repository->setUser(auth()->user());
|
2018-07-25 23:10:17 -05:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
return null !== $repository->findNull($value);
|
2020-03-18 15:36:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
private function validateTransaction(int $value): bool
|
2020-03-18 15:36:26 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
/** @var JournalAPIRepositoryInterface $repository */
|
|
|
|
$repository = app(JournalAPIRepositoryInterface::class);
|
2020-03-18 15:36:26 -05:00
|
|
|
$repository->setUser(auth()->user());
|
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
return null !== $repository->findTransaction((int)$value);
|
2019-07-31 09:53:09 -05:00
|
|
|
}
|
2018-07-22 13:32:02 -05:00
|
|
|
}
|