| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Rules; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Support\Validation\ValidatesAmountsTrait; | 
					
						
							|  |  |  | use Illuminate\Contracts\Validation\ValidationRule; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class IsValidZeroOrMoreAmount implements ValidationRule | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     use ValidatesAmountsTrait; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.UnusedFormalParameter) | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validate(string $attribute, mixed $value, \Closure $fail): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $value = (string) $value; | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |         // must not be empty:
 | 
					
						
							| 
									
										
										
										
											2024-02-22 01:29:01 +01:00
										 |  |  |         if ($this->emptyString($value)) { | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |             $fail('validation.filled')->translate(); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:32:42 +01:00
										 |  |  |             $message = sprintf('IsValidZeroOrMoreAmount: "%s" cannot be empty.', $value); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:35:58 +01:00
										 |  |  |             Log::debug($message); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:32:42 +01:00
										 |  |  |             Log::channel('audit')->info($message); | 
					
						
							| 
									
										
										
										
											2024-01-04 15:42:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // must be a number:
 | 
					
						
							| 
									
										
										
										
											2024-02-22 01:29:01 +01:00
										 |  |  |         if (!$this->isValidNumber($value)) { | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |             $fail('validation.numeric')->translate(); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:32:42 +01:00
										 |  |  |             $message = sprintf('IsValidZeroOrMoreAmount: "%s" is not a number.', $value); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:35:58 +01:00
										 |  |  |             Log::debug($message); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:32:42 +01:00
										 |  |  |             Log::channel('audit')->info($message); | 
					
						
							| 
									
										
										
										
											2024-01-04 15:42:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // must not be scientific notation:
 | 
					
						
							| 
									
										
										
										
											2024-02-22 01:29:01 +01:00
										 |  |  |         if ($this->scientificNumber($value)) { | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |             $fail('validation.scientific_notation')->translate(); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:32:42 +01:00
										 |  |  |             $message = sprintf('IsValidZeroOrMoreAmount: "%s" cannot be in the scientific notation.', $value); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:35:58 +01:00
										 |  |  |             Log::debug($message); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:32:42 +01:00
										 |  |  |             Log::channel('audit')->info($message); | 
					
						
							| 
									
										
										
										
											2024-01-04 15:42:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-04 07:25:24 +01:00
										 |  |  |         // must be zero or more
 | 
					
						
							| 
									
										
										
										
											2024-02-22 01:29:01 +01:00
										 |  |  |         if (!$this->zeroOrMore($value)) { | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |             $fail('validation.more_than_zero_correct')->translate(); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:32:42 +01:00
										 |  |  |             $message = sprintf('IsValidZeroOrMoreAmount: "%s" must be zero or more.', $value); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:35:58 +01:00
										 |  |  |             Log::debug($message); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:32:42 +01:00
										 |  |  |             Log::channel('audit')->info($message); | 
					
						
							| 
									
										
										
										
											2024-01-04 15:42:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-21 18:01:00 +01:00
										 |  |  |         // must be less than a large number
 | 
					
						
							| 
									
										
										
										
											2024-02-22 01:29:01 +01:00
										 |  |  |         if ($this->moreThanLots($value)) { | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |             $fail('validation.lte.numeric')->translate(['value' => self::BIG_AMOUNT]); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:32:42 +01:00
										 |  |  |             $message = sprintf('IsValidPositiveAmount: "%s" must be less than %s.', $value, self::BIG_AMOUNT); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:35:58 +01:00
										 |  |  |             Log::debug($message); | 
					
						
							| 
									
										
										
										
											2024-01-04 08:32:42 +01:00
										 |  |  |             Log::channel('audit')->info($message); | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-04 08:35:58 +01:00
										 |  |  |         Log::debug(sprintf('IsValidZeroOrMoreAmount: "%s" is a valid positive amount.', $value)); | 
					
						
							| 
									
										
										
										
											2024-01-02 22:28:58 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } |