firefly-iii/app/Rules/IsValidAmount.php

69 lines
2.3 KiB
PHP
Raw Normal View History

2024-01-02 07:33:17 -06: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 IsValidAmount implements ValidationRule
{
use ValidatesAmountsTrait;
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$value = (string)$value;
// must not be empty:
if($this->emptyString($value)) {
$fail('validation.filled')->translate();
2024-01-04 01:34:57 -06:00
$message = sprintf('IsValidAmount: "%s" cannot be empty.', $value);
2024-01-04 01:35:58 -06:00
Log::debug($message);
2024-01-04 01:34:57 -06:00
Log::channel('audit')->info($message);
2024-01-02 07:33:17 -06:00
return;
}
// must be a number:
if(!$this->isValidNumber($value)) {
$fail('validation.numeric')->translate();
2024-01-04 01:34:57 -06:00
$message = sprintf('IsValidAmount: "%s" is not a number.', $value);
2024-01-04 01:35:58 -06:00
Log::debug($message);
2024-01-04 01:34:57 -06:00
Log::channel('audit')->info($message);
2024-01-02 07:33:17 -06:00
return;
}
// must not be scientific notation:
if($this->scientificNumber($value)) {
$fail('validation.scientific_notation')->translate();
2024-01-04 01:34:57 -06:00
$message = sprintf('IsValidAmount: "%s" cannot be in the scientific notation.', $value);
2024-01-04 01:35:58 -06:00
Log::debug($message);
2024-01-04 01:34:57 -06:00
Log::channel('audit')->info($message);
2024-01-02 07:33:17 -06:00
return;
}
// must be more than minus a lots:
if($this->lessThanLots($value)) {
$amount = bcmul('-1', self::BIG_AMOUNT);
$fail('validation.gte.numeric')->translate(['value' => $amount]);
2024-01-04 01:34:57 -06:00
$message = sprintf('IsValidAmount: "%s" must be more than %s.', $value, $amount);
2024-01-04 01:35:58 -06:00
Log::debug($message);
2024-01-04 01:34:57 -06:00
Log::channel('audit')->info($message);
2024-01-02 07:33:17 -06:00
return;
}
// must be less than 100 million and 1709:
if($this->moreThanLots($value)) {
$fail('validation.lte.numeric')->translate(['value' => self::BIG_AMOUNT]);
2024-01-04 01:34:57 -06:00
$message = sprintf('IsValidAmount: "%s" must be more than %s.', $value, self::BIG_AMOUNT);
2024-01-04 01:35:58 -06:00
Log::debug($message);
2024-01-04 01:34:57 -06:00
Log::channel('audit')->info($message);
2024-01-02 07:33:17 -06:00
}
2024-01-04 01:35:58 -06:00
Log::debug(sprintf('IsValidAmount: "%s" is a valid positive amount.', $value));
2024-01-02 07:33:17 -06:00
}
}