Skip to content

Commit

Permalink
feat: add globMatch
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <zhouxinyu1001@gmail.com>
  • Loading branch information
Zxilly committed May 3, 2021
1 parent 16462f9 commit c92c5dc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Model/FunctionMap.php
Expand Up @@ -50,6 +50,9 @@ public static function loadFunctionMap(): self
$fm->addFunction('ipMatch', function (...$args) {
return BuiltinOperations::ipMatchFunc(...$args);
});
$fm->addFunction('globMatch', function (...$args) {
return BuiltinOperations::globMatchFunc(...$args);
});

return $fm;
}
Expand Down
37 changes: 35 additions & 2 deletions src/Util/BuiltinOperations.php
Expand Up @@ -6,6 +6,7 @@

use Casbin\Rbac\RoleManager;
use Closure;
use Exception;
use IPTools\IP;
use IPTools\Range;

Expand Down Expand Up @@ -233,7 +234,7 @@ public static function regexMatchFunc(...$args): bool
*
* @return bool
*
* @throws \Exception
* @throws Exception
*/
public static function ipMatch(string $ip1, string $ip2): bool
{
Expand All @@ -251,7 +252,7 @@ public static function ipMatch(string $ip1, string $ip2): bool
*
* @return bool
*
* @throws \Exception
* @throws Exception
*/
public static function ipMatchFunc(...$args): bool
{
Expand All @@ -261,6 +262,38 @@ public static function ipMatchFunc(...$args): bool
return self::ipMatch($ip1, $ip2);
}

/**
* Returns true if the specified `string` matches the given glob `pattern`.
*
* @param string $str
* @param string $pattern
*
* @return bool
*
* @throws Exception
*/
public static function globMatch(string $str, string $pattern): bool
{
return fnmatch($pattern, $str, FNM_PATHNAME | FNM_PERIOD);
}

/**
* The wrapper for globMatch.
*
* @param mixed ...$args
*
* @return bool
*
* @throws Exception
*/
public static function globMatchFunc(...$args): bool
{
$str = $args[0];
$pattern = $args[1];

return self::globMatch($str, $pattern);
}

/**
* The factory method of the g(_, _) function.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/Util/BuiltinOperationsTest.php
Expand Up @@ -32,6 +32,11 @@ private function keyMatch4Func($name1, $name2)
return (bool)BuiltinOperations::keyMatch4Func($name1, $name2);
}

private function globMatchFunc($name1, $name2)
{
return BuiltinOperations::globMatchFunc($name1, $name2);
}

public function testKeyMatchFunc()
{
$this->assertTrue($this->keyMatchFunc('/foo', '/foo'));
Expand Down Expand Up @@ -120,4 +125,15 @@ public function testKeyMatch4Func()

$this->assertFalse($this->keyMatch4Func('/parent/123/child/123', '/parent/{i/d}/child/{i/d}'));
}

public function testGlobMatchFunc()
{
$this->assertTrue($this->globMatchFunc('/foo', '/foo'));
$this->assertTrue($this->globMatchFunc('/foo', '/foo*'));
$this->assertFalse($this->globMatchFunc('/foo', '/foo/*'));

$this->assertFalse($this->globMatchFunc('/prefix/foo', '*/foo'));

$this->assertTrue($this->globMatchFunc('/foo/bar', '/foo/*'));
}
}

0 comments on commit c92c5dc

Please sign in to comment.