diff --git a/README.md b/README.md index d2b98537..52bfacfb 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 866cc98b..6f3eefe7 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 * @@ -1950,27 +1976,4 @@ protected function validate_valid_array_size_equal($field, array $input, array $ { return !(!is_array($input[$field]) || count($input[$field]) != $params[0]); } - - /** - * Checks if a validator method exists for a given rule. - * - * @param string $rule - * @return bool - */ - public static function has_validator(string $rule): bool - { - return method_exists(__CLASS__, self::validator_to_method($rule)) || isset(self::$validation_methods[$rule]); - } - - /** - * Checks if a filter method exists for a given filter. - * @param string $filter - * @return bool - */ - public static function has_filter(string $filter): bool - { - return method_exists(__CLASS__, self::filter_to_method($filter)) - || isset(self::$filter_methods[$filter]) - || function_exists($filter); - } } diff --git a/tests/StaticHasFilterTest.php b/tests/StaticHasFilterTest.php index 6c458392..97285dea 100644 --- a/tests/StaticHasFilterTest.php +++ b/tests/StaticHasFilterTest.php @@ -6,40 +6,38 @@ class StaticHasFilterTest extends BaseTestCase { - public function testHasFilterWhenExists(): void + public function dataOfExistingRules() { - $filterRules = [ + return [ // There are native filters - 'noise_words', - 'rmpunctuation', - 'urlencode', - 'htmlencode', - 'sanitize_email', - 'sanitize_numbers', - 'sanitize_floats', - 'sanitize_string', - 'boolean', - 'basic_tags', - 'whole_number', - 'ms_word_characters', - 'lower_case', - 'upper_case', - 'slug', + ['noise_words'], + ['upper_case'], + ['slug'], // These are built-in functions - 'trim', - 'strtoupper', - 'strtolower', - 'intval', - 'floatval', + ['trim'], + ['strtoupper'], ]; + } + + /** + * @dataProvider dataOfExistingRules + */ + public function testHasFilterWhenExists(string $rule) + { + $this->assertTrue(GUMP::has_filter($rule)); + } + + public function testHasFilterWithCustomRule(): void + { + GUMP::add_filter('test', function($value, array $params = []) { + return strtoupper($value); + }); - foreach ($filterRules as $filterRule) { - $this->assertTrue(GUMP::has_filter($filterRule)); - } + $this->assertTrue(GUMP::has_filter('test')); } public function testHasFilterWhenNotExists(): void { - $this->assertFalse(GUMP::has_filter('custom_filter')); + $this->assertFalse(GUMP::has_filter('test')); } -} \ No newline at end of file +} diff --git a/tests/StaticHasValidatorTest.php b/tests/StaticHasValidatorTest.php index 023cdcd4..60ba2804 100644 --- a/tests/StaticHasValidatorTest.php +++ b/tests/StaticHasValidatorTest.php @@ -6,61 +6,35 @@ class StaticHasValidatorTest extends BaseTestCase { - public function testHasValidatorWhenExists(): void + public function dataOfExistingRules() { - $validationRules = [ - 'required', - 'contains', - 'contains_list', - 'doesnt_contain_list', - 'boolean', - 'valid_email', - 'max_len', - 'min_len', - 'exact_len', - 'between_len', - 'alpha', - 'alpha_numeric', - 'alpha_dash', - 'alpha_numeric_dash', - 'alpha_numeric_space', - 'alpha_space', - 'numeric', - 'integer', - 'float', - 'valid_url', - 'url_exists', - 'valid_ip', - 'valid_ipv4', - 'valid_ipv6', - 'valid_cc', - 'valid_name', - 'street_address', - 'iban', - 'date', - 'min_age', - 'max_numeric', - 'min_numeric', - 'starts', - 'required_file', - 'extension', - 'equalsfield', - 'guidv4', - 'phone_number', - 'regex', - 'valid_json_string', - 'valid_array_size_greater', - 'valid_array_size_lesser', - 'valid_array_size_equal', + return [ + ['required'], + ['contains'], + ['regex'], + ['valid_array_size_equal'], ]; + } + + /** + * @dataProvider dataOfExistingRules + */ + public function testHasValidatorWhenExists(string $rule) + { + $this->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}.'); - foreach ($validationRules as $rule) { - $this->assertTrue(GUMP::has_validator($rule)); - } + $this->assertTrue(GUMP::has_validator('equals_string')); } - public function testHasValidatorWhenNotExists(): void + public function testHasValidatorWhenDoesntExist() { - $this->assertFalse(GUMP::has_validator('custom_rule')); + $this->assertFalse(GUMP::has_validator('equals_string')); } -} \ No newline at end of file +}