22 lines
692 B
PHP
22 lines
692 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Domain\Rule\StringMatchMode;
|
|
|
|
use function strlen;
|
|
|
|
final class StringMatcher
|
|
{
|
|
public static function match(string $input, string $pattern, StringMatchMode $mode): bool
|
|
{
|
|
return match ($mode) {
|
|
StringMatchMode::Exact => strcasecmp($input, $pattern) === 0,
|
|
StringMatchMode::Contains => stripos($input, $pattern) !== false,
|
|
StringMatchMode::StartsWith => strncasecmp($input, $pattern, strlen($pattern)) === 0,
|
|
StringMatchMode::EndsWith => str_ends_with(strtolower($input), strtolower($pattern)),
|
|
StringMatchMode::Regex => preg_match($pattern, $input) === 1,
|
|
};
|
|
}
|
|
}
|