From 268d56b7dd5aa9c2e2d0f253422b00086e7b2a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=AF=BAEno?= <895183594@qq.com> Date: Sat, 29 Jun 2024 20:22:14 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=9C=89=E5=85=B3?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E5=99=A8=E6=B3=A8=E8=A7=A3@Integer=E3=80=81@?= =?UTF-8?q?Decimal=E5=92=8C@Number=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/components/validation/index.md | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/doc/components/validation/index.md b/doc/components/validation/index.md index f94b5f6344..6981fb4f6a 100644 --- a/doc/components/validation/index.md +++ b/doc/components/validation/index.md @@ -183,6 +183,14 @@ class TestValidate 整数验证 +> 能通过整数验证的数据类型为 `int | float | string` +> +> 对于 `string` 类型可参考[数字字符串](https://www.php.net/manual/zh/language.types.numeric-strings.php) +> +> 例如,类似 `5` 、 `5.0` 、 `'5'` 均属于整数 +> +> 特殊的,类似 `'5.0'` 这种**带小数点的数字字符串**不属于整数 + 验证必须为整数: `@Integer` @@ -203,6 +211,14 @@ class TestValidate 小数验证 +> 能通过小数验证的数据类型为 `float | string` +> +> 对于 `string` 类型可参考[数字字符串](https://www.php.net/manual/zh/language.types.numeric-strings.php) +> +> 例如,类似 `5.1` 、 `'5.1'` 、 `'5.0'` 均属于小数 +> +> 特殊的,类似 `5.0` 这种**小数部分为0的 `float` 值**不属于小数 + 验证必须为小数: `@Decimal` @@ -227,21 +243,25 @@ class TestValidate 数值验证,允许是整数或者小数 +> 能通过数值验证的数据类型为 `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` > From 4093434097df29293a5bea91b9c2180286bd179b Mon Sep 17 00:00:00 2001 From: Eno <895183594@qq.com> Date: Sat, 20 Jul 2024 22:56:58 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=95=B0=E5=80=BC?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=9B=B8=E5=85=B3=E7=9A=84=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/components/validation/index.md | 34 +++++++++++++----------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/doc/components/validation/index.md b/doc/components/validation/index.md index 6981fb4f6a..ba3c40a216 100644 --- a/doc/components/validation/index.md +++ b/doc/components/validation/index.md @@ -46,7 +46,7 @@ public $inverseResult = false; /** * 当验证条件不符合时的信息 - * + * * 支持代入{:value}原始值 * 支持代入{:data.xxx}所有数据中的某项 * 支持以{name}这样的形式,代入注解参数值 @@ -64,7 +64,7 @@ public $callable; /** * 参数名数组 - * + * * 支持代入{:value}原始值 * 支持代入{:data}所有数据 * 支持代入{:data.xxx}所有数据中的某项 @@ -183,13 +183,11 @@ class TestValidate 整数验证 -> 能通过整数验证的数据类型为 `int | float | string` +> 能通过整数验证的数据类型为 `int | string` > > 对于 `string` 类型可参考[数字字符串](https://www.php.net/manual/zh/language.types.numeric-strings.php) -> -> 例如,类似 `5` 、 `5.0` 、 `'5'` 均属于整数 -> -> 特殊的,类似 `'5.0'` 这种**带小数点的数字字符串**不属于整数 +> +> 例如,类似 `5` 、 `'5'` 均属于整数 验证必须为整数: @@ -214,10 +212,8 @@ class TestValidate > 能通过小数验证的数据类型为 `float | string` > > 对于 `string` 类型可参考[数字字符串](https://www.php.net/manual/zh/language.types.numeric-strings.php) -> -> 例如,类似 `5.1` 、 `'5.1'` 、 `'5.0'` 均属于小数 -> -> 特殊的,类似 `5.0` 这种**小数部分为0的 `float` 值**不属于小数 +> +> 例如,类似 `5.1` 、 `'5.1'` 、 `5.0` 、 `'5.0'` 均属于小数 验证必须为小数: @@ -236,7 +232,7 @@ class TestValidate `@Decimal(min=1, max=10.24)` > 传入`1`,结果为`false` -> +> > 传入`1.0`,结果为`true` ### @Number @@ -264,9 +260,9 @@ class TestValidate `@Number(min=1, max=10.24)` > 传入`1`,结果为`true` -> +> > 传入`1.0`,结果为`true` -> +> ### @InList 列表验证,判断值是否存在于列表中 @@ -328,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}为必须参数") @@ -371,7 +367,7 @@ class Test ```php /** * @AutoValidation - * + * * @Required(name="id", message="用户ID为必传参数") * @Integer(name="id", min=1, message="用户ID不符合规则") * @Required(name="name", message="用户姓名为必传参数") @@ -394,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}为必须参数") From 95d17785906dc0d488f167ab4b19e1b89254f219 Mon Sep 17 00:00:00 2001 From: Eno <895183594@qq.com> Date: Sat, 20 Jul 2024 23:26:00 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B0=8F=E6=95=B0?= =?UTF-8?q?=E3=80=81=E6=95=B4=E6=95=B0=E9=AA=8C=E8=AF=81=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=AF=B9=E6=B5=AE=E7=82=B9=E6=95=B0=E7=B1=BB=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=20=E4=BF=AE=E5=A4=8D=E6=95=B0=E5=80=BC?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E5=87=BD=E6=95=B0=E5=AF=B9=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=88=96=E5=B0=8F=E6=95=B0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=B8=BA0=E7=9A=84=E6=B5=AE=E7=82=B9=E6=95=B0=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=E5=B0=8F=E6=95=B0=E7=B2=BE=E5=BA=A6=E6=A3=80?= =?UTF-8?q?=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Validate/ValidatorHelper.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Validate/ValidatorHelper.php b/src/Validate/ValidatorHelper.php index 8b1cb4abab..2ba407fa1c 100644 --- a/src/Validate/ValidatorHelper.php +++ b/src/Validate/ValidatorHelper.php @@ -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, '.')); } /** @@ -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; } @@ -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; From dc87687f0527de460989bba6fd327751f2c92e8c Mon Sep 17 00:00:00 2001 From: Eno <895183594@qq.com> Date: Sat, 20 Jul 2024 23:30:41 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Validate/ValidatorHelper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Validate/ValidatorHelper.php b/src/Validate/ValidatorHelper.php index 2ba407fa1c..aca1d08464 100644 --- a/src/Validate/ValidatorHelper.php +++ b/src/Validate/ValidatorHelper.php @@ -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) && (is_float($value) || str_contains((string) $value, '.')); + return static::number($value, $min, $max, $accuracy) && (\is_float($value) || str_contains((string) $value, '.')); } /** @@ -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 || is_float($value)) + if ((string) (int) $value !== (string) $value || \is_float($value)) { return false; } From 867f97efef8151e80270b3ac0368a99d6f2291de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=AF=BAEno?= <895183594@qq.com> Date: Sun, 21 Jul 2024 02:04:56 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95=20x.0?= =?UTF-8?q?=20=E4=B8=BA=E6=9C=89=E6=95=88=E5=9C=B0=E6=B5=AE=E7=82=B9?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/Component/Tests/ValidatorHelperTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/Component/Tests/ValidatorHelperTest.php b/tests/unit/Component/Tests/ValidatorHelperTest.php index a17e6b0b21..d690a251ee 100644 --- a/tests/unit/Component/Tests/ValidatorHelperTest.php +++ b/tests/unit/Component/Tests/ValidatorHelperTest.php @@ -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)); From e43f1f73e6f662266895bdf49389dfb77f80e0e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=AF=BAEno?= <895183594@qq.com> Date: Sun, 21 Jul 2024 02:13:03 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=85=B3=E4=BA=8E?= =?UTF-8?q?=E6=95=B4=E6=95=B0=E3=80=81=E6=95=B0=E5=AD=97=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/Component/Tests/ValidatorHelperTest.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/unit/Component/Tests/ValidatorHelperTest.php b/tests/unit/Component/Tests/ValidatorHelperTest.php index d690a251ee..d4daa3e8d0 100644 --- a/tests/unit/Component/Tests/ValidatorHelperTest.php +++ b/tests/unit/Component/Tests/ValidatorHelperTest.php @@ -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)); @@ -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));