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