1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 09:58:59 +01:00

Improve text filters

This commit is contained in:
AhJ 2024-01-04 14:02:35 +03:30
parent 54b39937a4
commit cb38b371cf
8 changed files with 23 additions and 13 deletions

View File

@ -33,6 +33,7 @@ final class FilterText extends Filter
) {
Assert::notEmpty($content);
}
public function apply(Update $update): bool
{
return ($update instanceof Message && $update->message === $this->content) ||

View File

@ -29,14 +29,15 @@ use Webmozart\Assert\Assert;
final class FilterTextCaseInsensitive extends Filter
{
public function __construct(
private readonly string $content
private readonly string $content,
private readonly ?string $encoding = null,
) {
Assert::notEmpty($content);
Assert::lower($content);
}
public function apply(Update $update): bool
{
return ($update instanceof Message && strtolower($update->message) === $this->content) ||
($update instanceof Story && strtolower($update->caption) === $this->content);
return ($update instanceof Message && mb_strtolower($update->message, $this->encoding) === $this->content) ||
($update instanceof Story && mb_strtolower($update->caption, $this->encoding) === $this->content);
}
}

View File

@ -33,6 +33,7 @@ final class FilterTextContains extends Filter
) {
Assert::notEmpty($content);
}
public function apply(Update $update): bool
{
return ($update instanceof Message && str_contains($update->message, $this->content)) ||

View File

@ -29,13 +29,14 @@ use Webmozart\Assert\Assert;
final class FilterTextContainsCaseInsensitive extends Filter
{
public function __construct(
private readonly string $content
private readonly string $content,
private readonly ?string $encoding = null,
) {
Assert::notEmpty($content);
}
public function apply(Update $update): bool
{
return ($update instanceof Message && str_contains(strtolower($update->message), $this->content)) ||
($update instanceof Story && str_contains(strtolower($update->caption), $this->content));
return ($update instanceof Message && str_contains(mb_strtolower($update->message, $this->encoding), $this->content)) ||
($update instanceof Story && str_contains(mb_strtolower($update->caption, $this->encoding), $this->content));
}
}

View File

@ -33,6 +33,7 @@ final class FilterTextEnds extends Filter
) {
Assert::notEmpty($content);
}
public function apply(Update $update): bool
{
return ($update instanceof Message && str_ends_with($update->message, $this->content)) ||

View File

@ -29,13 +29,15 @@ use Webmozart\Assert\Assert;
final class FilterTextEndsCaseInsensitive extends Filter
{
public function __construct(
private readonly string $content
private readonly string $content,
private readonly ?string $encoding = null,
) {
Assert::notEmpty($content);
}
public function apply(Update $update): bool
{
return ($update instanceof Message && str_ends_with(strtolower($update->message), $this->content)) ||
($update instanceof Story && str_ends_with(strtolower($update->caption), $this->content));
return ($update instanceof Message && str_ends_with(mb_strtolower($update->message, $this->encoding), $this->content)) ||
($update instanceof Story && str_ends_with(mb_strtolower($update->caption, $this->encoding), $this->content));
}
}

View File

@ -33,6 +33,7 @@ final class FilterTextStart extends Filter
) {
Assert::notEmpty($content);
}
public function apply(Update $update): bool
{
return ($update instanceof Message && str_starts_with($update->message, $this->content)) ||

View File

@ -29,13 +29,15 @@ use Webmozart\Assert\Assert;
final class FilterTextStartsCaseInsensitive extends Filter
{
public function __construct(
private readonly string $content
private readonly string $content,
private readonly ?string $encoding = null,
) {
Assert::notEmpty($content);
}
public function apply(Update $update): bool
{
return ($update instanceof Message && str_starts_with(strtolower($update->message), $this->content)) ||
($update instanceof Story && str_starts_with(strtolower($update->caption), $this->content));
return ($update instanceof Message && str_starts_with(mb_strtolower($update->message, $this->encoding), $this->content)) ||
($update instanceof Story && str_starts_with(mb_strtolower($update->caption, $this->encoding), $this->content));
}
}