25 lines
737 B
PHP
25 lines
737 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Domain\Rule\StringMatchMode;
|
|
|
|
final class StringMatcher
|
|
{
|
|
public static function match(string $input, string $pattern, StringMatchMode $mode): bool
|
|
{
|
|
if (empty($input) || empty($pattern)) {
|
|
return false;
|
|
}
|
|
|
|
return match ($mode) {
|
|
StringMatchMode::Exact => strcasecmp($input, $pattern) === 0,
|
|
StringMatchMode::Contains => str_contains($input, $pattern) ,
|
|
StringMatchMode::StartsWith => str_starts_with($input, $pattern),
|
|
StringMatchMode::EndsWith => str_ends_with($input, $pattern),
|
|
StringMatchMode::Regex => preg_match($pattern, $input) === 1,
|
|
default => false,
|
|
};
|
|
}
|
|
}
|