Files
firefly-iii/app/Rules/IsValidPositiveAmount.php

89 lines
3.1 KiB
PHP
Raw Normal View History

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