| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 19:31:55 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * IsValidBulkClause.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 18:43:21 +02:00
										 |  |  | namespace FireflyIII\Rules; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 17:41:14 +01:00
										 |  |  | use Illuminate\Contracts\Validation\ValidationRule; | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  | use Illuminate\Support\Facades\Validator; | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  | use Closure; | 
					
						
							|  |  |  | use JsonException; | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  | use function Safe\json_decode; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class IsValidBulkClause | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-10-29 17:41:14 +01:00
										 |  |  | class IsValidBulkClause implements ValidationRule | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     private string $error; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |     private array  $rules; | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function __construct(string $type) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->rules = config(sprintf('bulk.%s', $type)); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $this->error = (string) trans('firefly.belongs_user'); | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |     public function message(): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |     public function validate(string $attribute, mixed $value, Closure $fail): void | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $result = $this->basicValidation((string) $value); | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  |         if (false === $result) { | 
					
						
							| 
									
										
										
										
											2023-10-29 17:41:14 +01:00
										 |  |  |             $fail($this->error); | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Does basic rule based validation. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function basicValidation(string $value): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             $array = json_decode($value, true, 8, JSON_THROW_ON_ERROR); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (JsonException) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $this->error = (string) trans('validation.json'); | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $clauses = ['where', 'update']; | 
					
						
							|  |  |  |         foreach ($clauses as $clause) { | 
					
						
							|  |  |  |             if (!array_key_exists($clause, $array)) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 $this->error = (string) trans(sprintf('validation.missing_%s', $clause)); | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  |             /** | 
					
						
							|  |  |  |              * @var string $arrayKey | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |              * @var mixed  $arrayValue | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  |              */ | 
					
						
							|  |  |  |             foreach ($array[$clause] as $arrayKey => $arrayValue) { | 
					
						
							|  |  |  |                 if (!array_key_exists($arrayKey, $this->rules[$clause])) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                     $this->error = (string) trans(sprintf('validation.invalid_%s_key', $clause)); | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 // validate!
 | 
					
						
							|  |  |  |                 $validator = Validator::make(['value' => $arrayValue], [ | 
					
						
							|  |  |  |                     'value' => $this->rules[$clause][$arrayKey], | 
					
						
							|  |  |  |                 ]); | 
					
						
							|  |  |  |                 if ($validator->fails()) { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |                     $this->error = sprintf('%s: %s: %s', $clause, $arrayKey, implode(', ', $validator->errors()->get('value'))); | 
					
						
							| 
									
										
										
										
											2021-08-10 18:43:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-08-10 19:31:55 +02:00
										 |  |  | } |