This commit is contained in:
Masroor Ehsan 2025-01-22 19:09:47 +06:00
parent 41d53d1cc5
commit 8ba341e6cf
3 changed files with 18 additions and 8 deletions

View File

@ -8,6 +8,6 @@ enum MatchMode: string
case Contains = 'CONTAINS'; case Contains = 'CONTAINS';
case StartsWith = 'STARTS_WITH'; case StartsWith = 'STARTS_WITH';
case EndsWith = 'ENDS_WITH'; case EndsWith = 'ENDS_WITH';
case InArray = 'IN_ARRAY'; case InList = 'IN_LIST';
case Regex = 'REGEX'; case Regex = 'REGEX';
} }

View File

@ -6,22 +6,32 @@
final class ContentMatcher final class ContentMatcher
{ {
private static function matchList(string $input, string $pattern): bool
{
$patterns = collect(explode(',', $pattern))
->map(fn ($s) => trim($s))
->filter(fn ($s) => filled($s));
foreach ($patterns as $search) {
if (self::match($input, $search, MatchMode::Exact)) {
return true;
}
}
return false;
}
public static function match(string $input, string $pattern, MatchMode $mode): bool public static function match(string $input, string $pattern, MatchMode $mode): bool
{ {
if (blank($input) || blank($pattern)) { if (blank($input) || blank($pattern)) {
return false; return false;
} }
if ($mode !== MatchMode::Regex) {
$input = strtolower($input);
$pattern = strtolower($pattern);
}
return match ($mode) { return match ($mode) {
MatchMode::Exact => strcasecmp($input, $pattern) === 0, MatchMode::Exact => strcasecmp($input, $pattern) === 0,
MatchMode::Contains => str_contains($input, $pattern) , MatchMode::Contains => str_contains($input, $pattern) ,
MatchMode::StartsWith => str_starts_with($input, $pattern), MatchMode::StartsWith => str_starts_with($input, $pattern),
MatchMode::EndsWith => str_ends_with($input, $pattern), MatchMode::EndsWith => str_ends_with($input, $pattern),
MatchMode::InList => self::matchList($input, $pattern),
MatchMode::Regex => preg_match($pattern, $input) === 1, MatchMode::Regex => preg_match($pattern, $input) === 1,
}; };
} }

View File

@ -95,7 +95,7 @@ public function run(): void
'dicom_tag' => DicomTagIdentifiers::Modality->value, 'dicom_tag' => DicomTagIdentifiers::Modality->value,
'search_pattern' => 'MR,CT', 'search_pattern' => 'MR,CT',
'dicom_routing_rule_id' => $rul_chev_mr->id, 'dicom_routing_rule_id' => $rul_chev_mr->id,
'match_mode' => MatchMode::InArray->value, 'match_mode' => MatchMode::InList->value,
'case_sensitive' => true, 'case_sensitive' => true,
]); ]);
@ -110,7 +110,7 @@ public function run(): void
'dicom_tag' => DicomTagIdentifiers::Modality->value, 'dicom_tag' => DicomTagIdentifiers::Modality->value,
'search_pattern' => 'CR,DX', 'search_pattern' => 'CR,DX',
'dicom_routing_rule_id' => $rul_chev_xr->id, 'dicom_routing_rule_id' => $rul_chev_xr->id,
'match_mode' => MatchMode::InArray->value, 'match_mode' => MatchMode::InList->value,
'case_sensitive' => true, 'case_sensitive' => true,
]); ]);