From c92c5dc2158bb9e34ce2dca91f56042e97f6e8a9 Mon Sep 17 00:00:00 2001 From: Zxilly Date: Tue, 4 May 2021 01:07:09 +0800 Subject: [PATCH] feat: add globMatch Signed-off-by: Zxilly --- src/Model/FunctionMap.php | 3 ++ src/Util/BuiltinOperations.php | 37 +++++++++++++++++++++-- tests/Unit/Util/BuiltinOperationsTest.php | 16 ++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/Model/FunctionMap.php b/src/Model/FunctionMap.php index e96ca46..5d1c219 100644 --- a/src/Model/FunctionMap.php +++ b/src/Model/FunctionMap.php @@ -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; } diff --git a/src/Util/BuiltinOperations.php b/src/Util/BuiltinOperations.php index 036e1ee..22d9370 100644 --- a/src/Util/BuiltinOperations.php +++ b/src/Util/BuiltinOperations.php @@ -6,6 +6,7 @@ use Casbin\Rbac\RoleManager; use Closure; +use Exception; use IPTools\IP; use IPTools\Range; @@ -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 { @@ -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 { @@ -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. * diff --git a/tests/Unit/Util/BuiltinOperationsTest.php b/tests/Unit/Util/BuiltinOperationsTest.php index 7ffedb5..091b0d7 100644 --- a/tests/Unit/Util/BuiltinOperationsTest.php +++ b/tests/Unit/Util/BuiltinOperationsTest.php @@ -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')); @@ -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/*')); + } }