| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * ValidatesAutoBudgetRequest.php | 
					
						
							|  |  |  |  * Copyright (c) 2020 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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Validation\AutoBudget; | 
					
						
							| 
									
										
										
										
											2021-03-31 19:36:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  | use Illuminate\Validation\Validator; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Trait ValidatesAutoBudgetRequest | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | trait ValidatesAutoBudgetRequest | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.NPathComplexity) | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  |     protected function validateAutoBudgetAmount(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $data         = $validator->getData(); | 
					
						
							|  |  |  |         $type         = $data['auto_budget_type'] ?? ''; | 
					
						
							| 
									
										
										
										
											2024-01-02 14:33:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|float|int|string $amount */ | 
					
						
							| 
									
										
										
										
											2021-03-31 19:36:08 +02:00
										 |  |  |         $amount       = array_key_exists('auto_budget_amount', $data) ? $data['auto_budget_amount'] : null; | 
					
						
							|  |  |  |         $period       = array_key_exists('auto_budget_period', $data) ? $data['auto_budget_period'] : null; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         $currencyId   = array_key_exists('auto_budget_currency_id', $data) ? (int)$data['auto_budget_currency_id'] : null; | 
					
						
							| 
									
										
										
										
											2021-03-31 19:36:08 +02:00
										 |  |  |         $currencyCode = array_key_exists('auto_budget_currency_code', $data) ? $data['auto_budget_currency_code'] : null; | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  |         if (is_numeric($type)) { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             $type = (int)$type; | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-29 13:38:35 +01:00
										 |  |  |         if ('' === $type || 0 === $type) { | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-02 14:33:17 +01:00
										 |  |  |         // TODO lots of duplicates with number validator.
 | 
					
						
							| 
									
										
										
										
											2024-01-02 07:08:15 +01:00
										 |  |  |         // TODO should be present at more places, stop scientific notification
 | 
					
						
							| 
									
										
										
										
											2024-01-02 14:33:17 +01:00
										 |  |  |         if (str_contains(strtoupper((string)$amount), 'E')) { | 
					
						
							| 
									
										
										
										
											2024-01-02 07:08:15 +01:00
										 |  |  |             $amount = ''; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  |         // basic float check:
 | 
					
						
							| 
									
										
										
										
											2024-01-02 20:19:09 +01:00
										 |  |  |         if (!is_numeric($amount)) { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             $validator->errors()->add('auto_budget_amount', (string)trans('validation.amount_required_for_auto_budget')); | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-23 06:03:06 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         if (1 !== bccomp((string)$amount, '0')) { | 
					
						
							|  |  |  |             $validator->errors()->add('auto_budget_amount', (string)trans('validation.auto_budget_amount_positive')); | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if ('' === $period) { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             $validator->errors()->add('auto_budget_period', (string)trans('validation.auto_budget_period_mandatory')); | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-23 06:03:06 +02:00
										 |  |  |         if (null !== $currencyId && null !== $currencyCode && '' === $currencyCode && 0 === $currencyId) { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             $validator->errors()->add('auto_budget_amount', (string)trans('validation.require_currency_info')); | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-18 05:53:11 +02:00
										 |  |  |         // too big amount
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         if ((int)$amount > 268435456) { | 
					
						
							|  |  |  |             $validator->errors()->add('auto_budget_amount', (string)trans('validation.amount_required_for_auto_budget')); | 
					
						
							| 
									
										
										
										
											2022-09-18 05:53:11 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |