Skip to content

Commit

Permalink
add has_filter method
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon committed Apr 18, 2024
1 parent a15ae93 commit f308ee6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gump.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1961,4 +1961,16 @@ 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'));
}
}

0 comments on commit f308ee6

Please sign in to comment.