minor refactoring

This commit is contained in:
Dr Masroor Ehsan 2025-01-22 00:54:16 +06:00
parent 0d2057dfd1
commit 498a0a0547
6 changed files with 40 additions and 36 deletions

View File

@ -2,7 +2,7 @@
namespace App\Domain\Rule;
enum NameMatchModes: int
enum StringMatchMode: int
{
case Exact = 0;
case Contains = 1;

View File

@ -1,21 +0,0 @@
<?php
namespace App\Services;
use App\Domain\Rule\NameMatchModes;
use function strlen;
final class InputMatcher
{
public static function match(string $input, string $pattern, NameMatchModes $mode): bool
{
return match ($mode) {
NameMatchModes::Exact => strcasecmp($input, $pattern) === 0,
NameMatchModes::Contains => stripos($input, $pattern) !== false,
NameMatchModes::StartsWith => strncasecmp($input, $pattern, strlen($pattern)) === 0,
NameMatchModes::EndsWith => str_ends_with(strtolower($input), strtolower($pattern)),
NameMatchModes::Regex => preg_match($pattern, $input) === 1,
};
}
}

View File

@ -2,8 +2,8 @@
namespace App\Services\Pacs;
use App\Domain\Rule\NameMatchModes;
use App\Services\InputMatcher;
use App\Domain\Rule\StringMatchMode;
use App\Services\StringMatcher;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
@ -13,12 +13,16 @@ final class InstituteMapper
private static int $catchAll = -1;
public static function map(?string $input): int
/**
* @return array<int, ?int>
*/
public static function map(?string $input): array
{
if (empty(self::$patterns)) {
self::$patterns = Cache::remember('institute_names', now()->addDay(), function () {
return DB::table('institute_names')->orderBy('priority')->get()->toArray();
});
self::$patterns = Cache::remember('institute_names',
now()->addMinutes(15),
fn () => DB::table('institute_names')->orderBy('priority')->get()->toArray()
);
self::$catchAll = DB::table('institutes')->first('id')->id;
}
@ -26,12 +30,12 @@ public static function map(?string $input): int
$input = strtolower($input);
foreach (self::$patterns as $pattern) {
if (InputMatcher::match($input, $pattern->name, NameMatchModes::from($pattern->match_mode))) {
return $pattern->institute_id;
if (StringMatcher::match($input, $pattern->name, StringMatchMode::from($pattern->match_mode))) {
return [$pattern->institute_id, $pattern->facility_id];
}
}
}
return self::$catchAll;
return [self::$catchAll, null];
}
}

View File

@ -0,0 +1,21 @@
<?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,
};
}
}

View File

@ -1,6 +1,6 @@
<?php
use App\Domain\Rule\NameMatchModes;
use App\Domain\Rule\StringMatchMode;
use App\Models\Facility;
use App\Models\Institute;
use Illuminate\Database\Migrations\Migration;
@ -16,7 +16,7 @@ public function up(): void
$table->foreignIdFor(Institute::class)->constrained()->cascadeOnDelete();
$table->foreignIdFor(Facility::class)->nullable()->constrained()->cascadeOnDelete();
$table->string('name')->unique();
$table->unsignedTinyInteger('match_mode')->default(NameMatchModes::Exact->value);
$table->unsignedTinyInteger('match_mode')->default(StringMatchMode::Exact->value);
$table->unsignedTinyInteger('priority')->default(0);
$table->timestamps();
});

View File

@ -2,7 +2,7 @@
namespace Database\Seeders;
use App\Domain\Rule\NameMatchModes;
use App\Domain\Rule\StringMatchMode;
use App\Models\DicomServer;
use App\Models\Facility;
use App\Models\Institute;
@ -48,12 +48,12 @@ public function run(): void
DB::table('institute_names')->insert([
'name' => 'CHEVRON CLINICAL LABORATORY , PANCHLAISH,CTG',
'institute_id' => $chev->id,
'match_mode' => NameMatchModes::Exact->value,
'match_mode' => StringMatchMode::Exact->value,
]);
DB::table('institute_names')->insert([
'name' => 'CHEVRON CLINICAL',
'institute_id' => $chev->id,
'match_mode' => NameMatchModes::StartsWith->value,
'match_mode' => StringMatchMode::StartsWith->value,
]);
DicomServer::create(