From 7f53e33f49b7bf58e959d357ec7ea27deff1f5e3 Mon Sep 17 00:00:00 2001 From: SonyPradana Date: Mon, 30 Oct 2023 18:08:16 +0700 Subject: [PATCH] feat: multy pattern support multy pattern matching --- .../Integrate/ValueObjects/CommandMap.php | 27 ++++++++++++++++--- tests/Integrate/Console/KarnelTest.php | 27 +++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/System/Integrate/ValueObjects/CommandMap.php b/src/System/Integrate/ValueObjects/CommandMap.php index 7ea041b0..7bfae960 100644 --- a/src/System/Integrate/ValueObjects/CommandMap.php +++ b/src/System/Integrate/ValueObjects/CommandMap.php @@ -28,7 +28,7 @@ public function __construct(array $command) } /** - * Command rule. + * Command rule wrap to array. * * @return string[] */ @@ -44,6 +44,18 @@ public function mode(): string return $this->command['mode'] ?? 'full'; } + /** + * Pattern rule wrap to array. + * + * @return string[] + */ + public function patterns() + { + $pattern = $this->command['pattern']; + + return is_array($pattern) ? $pattern : [$pattern]; + } + public function class(): string { if (is_array($this->fn()) && array_key_exists(0, $this->fn())) { @@ -79,9 +91,18 @@ public function defaultOption() public function match() { if (array_key_exists('pattern', $this->command)) { - $pattern = $this->command['pattern']; + $pattern = $this->command['pattern']; + $patterns = is_array($pattern) ? $pattern : [$pattern]; - return fn ($given): bool => $given == $pattern; + return function ($given) use ($patterns): bool { + foreach ($patterns as $cmd) { + if ($given == $cmd) { + return true; + } + } + + return false; + }; } if (array_key_exists('match', $this->command)) { diff --git a/tests/Integrate/Console/KarnelTest.php b/tests/Integrate/Console/KarnelTest.php index b299a381..f2ebfe76 100644 --- a/tests/Integrate/Console/KarnelTest.php +++ b/tests/Integrate/Console/KarnelTest.php @@ -131,6 +131,29 @@ public function itCanCallCommandUsingPatternCommand() $this->assertTrue($hasContent); } + /** @test */ + public function itCanCallCommandUsingPatternGroupCommand() + { + $karnel = new NormalCommand($this->app); + ob_start(); + $exit = $karnel->handle(['cli', 'pattern1']); + $out = ob_get_clean(); + + $this->assertEquals(0, $exit); + $hasContent = Str::contains($out, 'command has founded'); + $this->assertTrue($hasContent); + + // 2 + $karnel = new NormalCommand($this->app); + ob_start(); + $exit = $karnel->handle(['cli', 'pattern2']); + $out = ob_get_clean(); + + $this->assertEquals(0, $exit); + $hasContent = Str::contains($out, 'command has founded'); + $this->assertTrue($hasContent); + } + /** @test */ public function itCanCallCommandWithDefaultOption() { @@ -185,6 +208,10 @@ protected function commands() 'pattern' => 'use:pattern', 'fn' => [FoundedCommand::class, 'main'], ]), + new CommandMap([ + 'pattern' => ['pattern1', 'pattern2'], + 'fn' => [FoundedCommand::class, 'main'], + ]), new CommandMap([ 'pattern' => 'use:default_option', 'fn' => [FoundedCommand::class, 'default'],