mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
New numerical validation.
This commit is contained in:
58
app/Rules/IsValidAmount.php
Normal file
58
app/Rules/IsValidAmount.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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();
|
||||
Log::debug(sprintf('IsValidAmount: "%s" cannot be empty.', $value));
|
||||
return;
|
||||
}
|
||||
|
||||
// must be a number:
|
||||
if(!$this->isValidNumber($value)) {
|
||||
$fail('validation.numeric')->translate();
|
||||
Log::debug(sprintf('IsValidAmount: "%s" is not a number.', $value));
|
||||
return;
|
||||
}
|
||||
|
||||
// must not be scientific notation:
|
||||
if($this->scientificNumber($value)) {
|
||||
$fail('validation.scientific_notation')->translate();
|
||||
Log::debug(sprintf('IsValidAmount: "%s" cannot be in the scientific notation.', $value));
|
||||
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]);
|
||||
Log::debug(sprintf('IsValidAmount: "%s" must be more than %s.', $value, $amount));
|
||||
return;
|
||||
}
|
||||
|
||||
// must be less than 100 million and 1709:
|
||||
if($this->moreThanLots($value)) {
|
||||
Log::debug(sprintf('IsValidPositiveAmount: "%s" must be more than %s.', $value, self::BIG_AMOUNT));
|
||||
$fail('validation.lte.numeric')->translate(['value' => self::BIG_AMOUNT]);
|
||||
}
|
||||
Log::debug(sprintf('IsValidAmount: "%s" is a valid positive amount.', $value));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user