Skip to content

Commit

Permalink
Merge pull request #548 from nutgram/fix-pattern-with-optional-group
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasss93 committed Aug 22, 2023
2 parents ab6f728 + 3b13960 commit 528341d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Handlers/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function matching(string $value): bool
$pattern = str_replace('/', '\/', $this->pattern);

// replace named parameters with regex
$regex = '/^'.preg_replace(self::PARAM_NAME_REGEX, '(?<$1>.*)', $pattern).'?$/mUu';
$regex = '/^'.preg_replace(self::PARAM_NAME_REGEX, '(?<$1>.*?)', $pattern).'?$/mu';

// match + return only named parameters
$regexMatched = (bool)preg_match($regex, $value, $matches, PREG_UNMATCHED_AS_NULL);
Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/PatternsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,16 @@
'cyrillic-upper-lower' => ['ПРИМЕР', 'пример', false],
'cyrillic-upper-upper' => ['ПРИМЕР', 'ПРИМЕР', true],
]);

it('calls handler with optional regex group', function (string $hear, ?string $expected) {
$bot = Nutgram::fake();

$bot->onCommand('start ?(.*)?', function (Nutgram $bot, ?string $param) use ($expected) {
expect($param)->toBe($expected);
});

$bot->hearText($hear)->reply();
})->with([
'without-param' => ['/start', null],
'with-param' => ['/start foo', 'foo'],
]);

0 comments on commit 528341d

Please sign in to comment.