Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复验证器工具类对小数的验证 #705

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions doc/components/validation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public $inverseResult = false;

/**
* 当验证条件不符合时的信息
*
*
* 支持代入{:value}原始值
* 支持代入{:data.xxx}所有数据中的某项
* 支持以{name}这样的形式,代入注解参数值
Expand All @@ -64,7 +64,7 @@ public $callable;

/**
* 参数名数组
*
*
* 支持代入{:value}原始值
* 支持代入{:data}所有数据
* 支持代入{:data.xxx}所有数据中的某项
Expand Down Expand Up @@ -183,6 +183,12 @@ class TestValidate

整数验证

> 能通过整数验证的数据类型为 `int | string`
>
> 对于 `string` 类型可参考[数字字符串](https://www.php.net/manual/zh/language.types.numeric-strings.php)
>
> 例如,类似 `5` 、 `'5'` 均属于整数

验证必须为整数:

`@Integer`
Expand All @@ -203,6 +209,12 @@ class TestValidate

小数验证

> 能通过小数验证的数据类型为 `float | string`
>
> 对于 `string` 类型可参考[数字字符串](https://www.php.net/manual/zh/language.types.numeric-strings.php)
>
> 例如,类似 `5.1` 、 `'5.1'` 、 `5.0` 、 `'5.0'` 均属于小数

验证必须为小数:

`@Decimal`
Expand All @@ -220,33 +232,37 @@ class TestValidate
`@Decimal(min=1, max=10.24)`

> 传入`1`,结果为`false`
>
>
> 传入`1.0`,结果为`true`

### @Number

数值验证,允许是整数或者小数

> 能通过数值验证的数据类型为 `int | float | string`
>
> 对于 `string` 类型可参考[数字字符串](https://www.php.net/manual/zh/language.types.numeric-strings.php)

验证必须为数值:

`@Decimal`
`@Number`

验证必须为>=10.24的数值:

`@Decimal(min=10.24)`
`@Number(min=10.24)`

验证必须为<=10.24的数值:

`@Decimal(max=10.24)`
`@Number(max=10.24)`

验证必须为>=1 && <=10.24的数值:

`@Decimal(min=1, max=10.24)`
`@Number(min=1, max=10.24)`

> 传入`1`,结果为`true`
>
>
> 传入`1.0`,结果为`true`
>
>
### @InList

列表验证,判断值是否存在于列表中
Expand Down Expand Up @@ -308,10 +324,10 @@ imi 支持在类、属性上使用 `@AutoValidation` 注解,当构造方法执
```php
/**
* @Bean("ValidatorTest")
*
*
* @AutoValidation
*
*
*
*
* @InList(name="in", list={1, 2, 3}, message="{:value} 不在列表内")
* @Integer(name="int", min=0, max=100, message="{:value} 不符合大于等于{min}且小于等于{max}")
* @Required(name="required", message="{name}为必须参数")
Expand Down Expand Up @@ -351,7 +367,7 @@ class Test
```php
/**
* @AutoValidation
*
*
* @Required(name="id", message="用户ID为必传参数")
* @Integer(name="id", min=1, message="用户ID不符合规则")
* @Required(name="name", message="用户姓名为必传参数")
Expand All @@ -374,7 +390,7 @@ public function test222($id, $name)
```php
/**
* @Bean("ValidatorTest")
*
*
* @InList(name="in", list={1, 2, 3}, message="{:value} 不在列表内")
* @Integer(name="int", min=0, max=100, message="{:value} 不符合大于等于{min}且小于等于{max}")
* @Required(name="required", message="{name}为必须参数")
Expand Down
8 changes: 5 additions & 3 deletions src/Validate/ValidatorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function regex($value, string $rule): bool
*/
public static function decimal($value, ?float $min = null, ?float $max = null, ?int $accuracy = null): bool
{
return static::number($value, $min, $max, $accuracy) && str_contains((string) $value, '.');
return static::number($value, $min, $max, $accuracy) && (\is_float($value) || str_contains((string) $value, '.'));
}

/**
Expand All @@ -39,7 +39,7 @@ public static function decimal($value, ?float $min = null, ?float $max = null, ?
public static function int($value, ?int $min = null, ?int $max = null): bool
{
// 整数验证
if ((string) (int) $value !== (string) $value)
if ((string) (int) $value !== (string) $value || \is_float($value))
{
return false;
}
Expand Down Expand Up @@ -85,7 +85,9 @@ public static function number($value, $min = null, $max = null, ?int $accuracy =
{
$value = (string) $value;

return \strlen($value) - strrpos($value, '.') - 1 <= $accuracy;
$pointPos = strrpos($value, '.');

return (false === $pointPos ? 0 : \strlen($value) - $pointPos - 1) <= $accuracy;
}

return true;
Expand Down
15 changes: 13 additions & 2 deletions tests/unit/Component/Tests/ValidatorHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testDecimal(): void
Assert::assertFalse(ValidatorHelper::decimal('1'));
Assert::assertTrue(ValidatorHelper::decimal('1.1'));

Assert::assertFalse(ValidatorHelper::decimal(1.0)); // x.0 不是有效地浮点数,强转字符串将变为整数
Assert::assertTrue(ValidatorHelper::decimal(1.0));
Assert::assertTrue(ValidatorHelper::decimal('1.0'));

Assert::assertFalse(ValidatorHelper::decimal(1.25, 2));
Expand All @@ -46,7 +46,12 @@ public function testInt(): void
{
Assert::assertFalse(ValidatorHelper::int('abc'));
Assert::assertFalse(ValidatorHelper::int(1.1));
Assert::assertFalse(ValidatorHelper::int('1.1'));
Assert::assertFalse(ValidatorHelper::int(1.0));
Assert::assertFalse(ValidatorHelper::int('1.0'));

Assert::assertTrue(ValidatorHelper::int(1));
Assert::assertTrue(ValidatorHelper::int('1'));

Assert::assertFalse(ValidatorHelper::int(5, 6));
Assert::assertTrue(ValidatorHelper::int(5, 5));
Expand All @@ -66,9 +71,15 @@ public function testNumber(): void
Assert::assertFalse(ValidatorHelper::number(1.25, null, 1.24));
Assert::assertTrue(ValidatorHelper::number(1.25, 1, 1.25));

Assert::assertTrue(ValidatorHelper::number(1, null, null, 0));
Assert::assertTrue(ValidatorHelper::number('1', null, null, 0));
Assert::assertFalse(ValidatorHelper::number(1.1, null, null, 0));
Assert::assertTrue(ValidatorHelper::number(1.0, null, null, 0));
Assert::assertFalse(ValidatorHelper::number('1.1', null, null, 0));
Assert::assertTrue(ValidatorHelper::number(1.1, null, null, 1));
Assert::assertTrue(ValidatorHelper::number('1.1', null, null, 1));
Assert::assertTrue(ValidatorHelper::number(1.0, null, null, 0));
Assert::assertFalse(ValidatorHelper::number('1.0', null, null, 0));
Assert::assertTrue(ValidatorHelper::number('1.0', null, null, 1));
Assert::assertFalse(ValidatorHelper::number(1.25, null, null, 1));
Assert::assertTrue(ValidatorHelper::number(1.25, null, null, 2));
Assert::assertTrue(ValidatorHelper::number(1.25, null, null, 3));
Expand Down
Loading