diff --git a/system/Validation/FormatRules.php b/system/Validation/FormatRules.php index 3aa1c6840ceb..ad41150a8c13 100644 --- a/system/Validation/FormatRules.php +++ b/system/Validation/FormatRules.php @@ -128,10 +128,12 @@ public function hex(?string $str = null): bool /** * Integer + * + * @param int|null $str */ - public function integer(?string $str = null): bool + public function integer($str = null): bool { - return (bool) preg_match('/\A[\-+]?\d+\z/', $str ?? ''); + return is_int($str); } /** @@ -155,8 +157,7 @@ public function is_natural_no_zero(?string $str = null): bool */ public function numeric(?string $str = null): bool { - // @see https://regex101.com/r/bb9wtr/2 - return (bool) preg_match('/\A[\-+]?\d*\.?\d+\z/', $str ?? ''); + return is_numeric($str); } /** diff --git a/system/Validation/StrictRules/FormatRules.php b/system/Validation/StrictRules/FormatRules.php index 97df6c04aecb..18a140342b35 100644 --- a/system/Validation/StrictRules/FormatRules.php +++ b/system/Validation/StrictRules/FormatRules.php @@ -184,18 +184,10 @@ public function hex($str = null): bool /** * Integer * - * @param mixed $str + * @param int|null $str */ public function integer($str = null): bool { - if (is_int($str)) { - $str = (string) $str; - } - - if (! is_string($str)) { - return false; - } - return $this->nonStrictFormatRules->integer($str); } @@ -238,7 +230,7 @@ public function is_natural_no_zero($str = null): bool /** * Numeric * - * @param mixed $str + * @param float|int|string|null $str */ public function numeric($str = null): bool { diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index f3f8bf7442f8..7b626032778b 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -655,7 +655,7 @@ public function testPageCacheWithCacheQueryString($cacheQueryStringValue, int $e $routes->add($testingUrl, static function () { CodeIgniter::cache(0); // Dont cache the page in the run() function because CodeIgniter class will create default $cacheConfig and overwrite settings from the dataProvider $response = Services::response(); - $string = 'This is a test page, to check cache configuration'; + $string = 'This is a test page, to check cache configuration'; return $response->setBody($string); }); diff --git a/tests/system/Validation/FormatRulesTest.php b/tests/system/Validation/FormatRulesTest.php index bf9bf92eb6dc..434c4c14ee68 100644 --- a/tests/system/Validation/FormatRulesTest.php +++ b/tests/system/Validation/FormatRulesTest.php @@ -809,13 +809,43 @@ public function testIntegerWithInvalidTypeData($value, bool $expected): void $data = [ 'foo' => $value, ]; - $this->assertsame($expected, $this->validation->run($data)); + $this->assertSame($expected, $this->validation->run($data)); + } + + public function integerInvalidTypeDataProvider(): Generator + { + // TypeError : CodeIgniter\Validation\FormatRules::integer(): Argument #1 ($str) must be of type ?string, array given + // yield 'array with int' => [ + // [555], + // false, + // ]; + + // TypeError : CodeIgniter\Validation\FormatRules::integer(): Argument #1 ($str) must be of type ?string, array given + // yield 'empty array' => [ + // [], + // false, + // ]; + + yield 'bool true' => [ + true, + false, + ]; + + yield 'bool false' => [ + false, + false, + ]; + + yield 'null' => [ + null, + false, + ]; } /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/5374 * - * @dataProvider integerInvalidTypeDataProvider + * @dataProvider numericInvalidTypeDataProvider * * @param mixed $value */ @@ -828,18 +858,18 @@ public function testNumericWithInvalidTypeData($value, bool $expected): void $data = [ 'foo' => $value, ]; - $this->assertsame($expected, $this->validation->run($data)); + $this->assertSame($expected, $this->validation->run($data)); } - public function integerInvalidTypeDataProvider(): Generator + public function numericInvalidTypeDataProvider(): Generator { - // TypeError : CodeIgniter\Validation\FormatRules::integer(): Argument #1 ($str) must be of type ?string, array given + // TypeError : CodeIgniter\Validation\FormatRules::numeric(): Argument #1 ($str) must be of type ?string, array given // yield 'array with int' => [ // [555], // false, // ]; - // TypeError : CodeIgniter\Validation\FormatRules::integer(): Argument #1 ($str) must be of type ?string, array given + // TypeError : CodeIgniter\Validation\FormatRules::numeric(): Argument #1 ($str) must be of type ?string, array given // yield 'empty array' => [ // [], // false, @@ -863,8 +893,10 @@ public function integerInvalidTypeDataProvider(): Generator /** * @dataProvider integerProvider + * + * @param int|string|null $str */ - public function testInteger(?string $str, bool $expected): void + public function testInteger($str, bool $expected): void { $data = [ 'foo' => $str, @@ -881,16 +913,24 @@ public function integerProvider(): Generator { yield from [ [ - '0', + 1, true, ], + [ + 1.5, + false, + ], + [ + '0', + false, + ], [ '42', - true, + false, ], [ '-1', - true, + false, ], [ "+42\n", diff --git a/tests/system/Validation/StrictRules/FormatRulesTest.php b/tests/system/Validation/StrictRules/FormatRulesTest.php index c1a060f51664..25893cb8c425 100644 --- a/tests/system/Validation/StrictRules/FormatRulesTest.php +++ b/tests/system/Validation/StrictRules/FormatRulesTest.php @@ -813,10 +813,38 @@ public function testIntegerWithInvalidTypeData($value, bool $expected): void $this->assertsame($expected, $this->validation->run($data)); } + public function integerInvalidTypeDataProvider(): Generator + { + yield 'array with int' => [ + [555], + false, // TypeError in Traditional Rule + ]; + + yield 'empty array' => [ + [], + false, // TypeError in Traditional Rule + ]; + + yield 'bool true' => [ + true, + false, // true in Traditional Rule + ]; + + yield 'bool false' => [ + false, + false, + ]; + + yield 'null' => [ + null, + false, + ]; + } + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/5374 * - * @dataProvider integerInvalidTypeDataProvider + * @dataProvider numericInvalidTypeDataProvider * * @param mixed $value */ @@ -832,21 +860,11 @@ public function testNumericWithInvalidTypeData($value, bool $expected): void $this->assertsame($expected, $this->validation->run($data)); } - public function integerInvalidTypeDataProvider(): Generator + public function numericInvalidTypeDataProvider(): Generator { - yield 'array with int' => [ - [555], - false, // TypeError in Traditional Rule - ]; - - yield 'empty array' => [ - [], - false, // TypeError in Traditional Rule - ]; - yield 'bool true' => [ true, - false, // true in Traditional Rule + false, ]; yield 'bool false' => [ @@ -862,8 +880,10 @@ public function integerInvalidTypeDataProvider(): Generator /** * @dataProvider integerProvider + * + * @param int|string|null $str */ - public function testInteger(?string $str, bool $expected): void + public function testInteger($str, bool $expected): void { $data = [ 'foo' => $str, @@ -880,16 +900,24 @@ public function integerProvider(): Generator { yield from [ [ - '0', + 1, true, ], + [ + 1.5, + false, + ], + [ + '0', + false, + ], [ '42', - true, + false, ], [ '-1', - true, + false, ], [ "+42\n", diff --git a/user_guide_src/source/libraries/validation.rst b/user_guide_src/source/libraries/validation.rst index 6b50b7331e7f..83871bbe58c5 100644 --- a/user_guide_src/source/libraries/validation.rst +++ b/user_guide_src/source/libraries/validation.rst @@ -211,7 +211,6 @@ It works for most basic cases like validating POST data. However, for example, if you use JSON input data, it may be a type of bool/null/array. When you validate the boolean ``true``, it is converted to string ``'1'`` with the Traditional rule classes. -If you validate it with the ``integer`` rule, ``'1'`` passes the validation. The **Strict Rules** don't use implicit type conversion.