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

Add has_validator and has_filter method #348

Closed
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
23 changes: 23 additions & 0 deletions gump.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1950,4 +1950,27 @@ 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);
}
}
45 changes: 45 additions & 0 deletions tests/StaticHasFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Tests;

use GUMP;

class StaticHasFilterTest extends BaseTestCase
{
public function testHasFilterWhenExists(): void
{
$filterRules = [
// 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',
// These are built-in functions
'trim',
'strtoupper',
'strtolower',
'intval',
'floatval',
];

foreach ($filterRules as $filterRule) {
$this->assertTrue(GUMP::has_filter($filterRule));
}
}

public function testHasFilterWhenNotExists(): void
{
$this->assertFalse(GUMP::has_filter('custom_filter'));
}
}
66 changes: 66 additions & 0 deletions tests/StaticHasValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tests;

use GUMP;

class StaticHasValidatorTest extends BaseTestCase
{
public function testHasValidatorWhenExists(): void
{
$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',
];

foreach ($validationRules as $rule) {
$this->assertTrue(GUMP::has_validator($rule));
}
}

public function testHasValidatorWhenNotExists(): void
{
$this->assertFalse(GUMP::has_validator('custom_rule'));
}
}
Loading