Skip to content

Commit

Permalink
Command improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
irazasyed committed Apr 15, 2023
1 parent 5d389c8 commit 45a15ca
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,11 @@ private function makeRegexPattern(): array

$patterns = collect($matches)
->mapWithKeys(function ($match) {
$name = $match['name'] ?? null;

if ($name === null) {
return [];
}

$pattern = $match['pattern'] ?? '[^ ]++';

return [$name => "(?<$name>$pattern)?"];
return [
$match['name'] => "(?<{$match['name']}>{$pattern})?",
];
})
->filter();

Expand All @@ -219,35 +215,37 @@ private function relevantMessageSubString(): string
2
);

return $splice->count() === 2 ? $this->cutTextBetween($splice) : $this->cutTextFrom($splice);
return match ($splice->count()) {
2 => $this->cutTextBetween($splice),
default => $this->cutTextFrom($splice),
};
}

private function allCommandOffsets(): Collection
{
$message = $this->getUpdate()
->getMessage();
$message = $this->getUpdate()->getMessage();

return $message->hasCommand() ?
$message
return $message->hasCommand()
? $message
->get('entities', collect())
->filter(static fn (MessageEntity $entity): bool => $entity->type === 'bot_command')
->pluck('offset') :
collect();
->pluck('offset')
: collect();
}

private function cutTextBetween(Collection $splice): string
{
return substr(
$this->getUpdate()->getMessage()->text,
return mb_substr(
$this->getUpdate()->getMessage()->text ?? '',
$splice->first(),
$splice->last() - $splice->first()
);
}

private function cutTextFrom(Collection $splice): string
{
return substr(
$this->getUpdate()->getMessage()->text,
return mb_substr(
$this->getUpdate()->getMessage()->text ?? '',
$splice->first()
);
}
Expand Down

0 comments on commit 45a15ca

Please sign in to comment.