From e39c0e34207cb47b7b04ca0a705f939b474f9956 Mon Sep 17 00:00:00 2001 From: Filis Futsarov Date: Mon, 29 Apr 2024 16:39:10 +0200 Subject: [PATCH] Add has_validator and has_filter (fork of #348) (#351) * add has_validator method * add has_filter method * Add more test cases and update README.md --------- Co-authored-by: Dragon --- README.md | 6 +++++ gump.class.php | 26 +++++++++++++++++++ tests/StaticHasFilterTest.php | 43 ++++++++++++++++++++++++++++++++ tests/StaticHasValidatorTest.php | 40 +++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 tests/StaticHasFilterTest.php create mode 100644 tests/StaticHasValidatorTest.php diff --git a/README.md b/README.md index d2b9853..52bfacf 100644 --- a/README.md +++ b/README.md @@ -255,6 +255,9 @@ GUMP::add_validator("equals_string", function($field, array $input, array $param return $value === $params; }, 'Field {field} does not equal to {param}.'); +// You might want to check whether a validator exists first +GUMP::has_validator($rule); + /** * @param string $value Value * @param array $param Filter parameters (optional) @@ -264,6 +267,9 @@ GUMP::add_validator("equals_string", function($field, array $input, array $param GUMP::add_filter("upper", function($value, array $params = []) { return strtoupper($value); }); + +// You might want to check whether a filter first already +GUMP::has_filter($rule); ``` Alternately, you can simply create your own class that extends GUMP. You only have to have in mind: diff --git a/gump.class.php b/gump.class.php index 8d9762b..6f3eefe 100644 --- a/gump.class.php +++ b/gump.class.php @@ -254,6 +254,32 @@ public static function add_filter(string $rule, callable $callback) self::$filter_methods[$rule] = $callback; } + /** + * Checks if a validator exists. + * + * @param string $rule + * + * @return bool + */ + public static function has_validator(string $rule) + { + return method_exists(__CLASS__, self::validator_to_method($rule)) || isset(self::$validation_methods[$rule]); + } + + /** + * Checks if a filter exists. + * + * @param string $filter + * + * @return bool + */ + public static function has_filter(string $filter) + { + return method_exists(__CLASS__, self::filter_to_method($filter)) + || isset(self::$filter_methods[$filter]) + || function_exists($filter); + } + /** * Helper method to extract an element from an array safely * diff --git a/tests/StaticHasFilterTest.php b/tests/StaticHasFilterTest.php new file mode 100644 index 0000000..97285de --- /dev/null +++ b/tests/StaticHasFilterTest.php @@ -0,0 +1,43 @@ +assertTrue(GUMP::has_filter($rule)); + } + + public function testHasFilterWithCustomRule(): void + { + GUMP::add_filter('test', function($value, array $params = []) { + return strtoupper($value); + }); + + $this->assertTrue(GUMP::has_filter('test')); + } + + public function testHasFilterWhenNotExists(): void + { + $this->assertFalse(GUMP::has_filter('test')); + } +} diff --git a/tests/StaticHasValidatorTest.php b/tests/StaticHasValidatorTest.php new file mode 100644 index 0000000..60ba280 --- /dev/null +++ b/tests/StaticHasValidatorTest.php @@ -0,0 +1,40 @@ +assertTrue(GUMP::has_validator($rule)); + } + + public function testHasValidatorWithCustomRule() + { + GUMP::add_validator("equals_string", function($field, array $input, array $params, $value) { + return $value === $params; + }, 'Field {field} does not equal to {param}.'); + + $this->assertTrue(GUMP::has_validator('equals_string')); + } + + public function testHasValidatorWhenDoesntExist() + { + $this->assertFalse(GUMP::has_validator('equals_string')); + } +}