29 lines
805 B
PHP
29 lines
805 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Domain\Rule\MatchMode;
|
|
|
|
final class ContentMatcher
|
|
{
|
|
public static function match(string $input, string $pattern, MatchMode $mode): bool
|
|
{
|
|
if (blank($input) || blank($pattern)) {
|
|
return false;
|
|
}
|
|
|
|
if ($mode !== MatchMode::Regex) {
|
|
$input = strtolower($input);
|
|
$pattern = strtolower($pattern);
|
|
}
|
|
|
|
return match ($mode) {
|
|
MatchMode::Exact => strcasecmp($input, $pattern) === 0,
|
|
MatchMode::Contains => str_contains($input, $pattern) ,
|
|
MatchMode::StartsWith => str_starts_with($input, $pattern),
|
|
MatchMode::EndsWith => str_ends_with($input, $pattern),
|
|
MatchMode::Regex => preg_match($pattern, $input) === 1,
|
|
};
|
|
}
|
|
}
|