| 12345678910111213141516171819202122232425262728 |
- <?php
- namespace App\Rules;
- use Illuminate\Contracts\Validation\Rule;
- class DecimalNumber implements Rule
- {
- public function __construct(private int $integerDigits = 14, private int $decimalPlaces = 4)
- {
- }
- public function passes($attribute, $value): bool
- {
- if (!is_string($value) && !is_int($value) && !is_float($value)) {
- return false;
- }
- return preg_match(
- '/^\d{1,' . $this->integerDigits . '}(?:\.\d{1,' . $this->decimalPlaces . '})?$/D',
- (string) $value
- ) === 1;
- }
- public function message(): string
- {
- return ':attribute 格式错误,最多 ' . $this->integerDigits . ' 位整数和 ' . $this->decimalPlaces . ' 位小数';
- }
- }
|