diff --git a/app/Domain/Rule/NameMatchModes.php b/app/Domain/Rule/StringMatchMode.php similarity index 85% rename from app/Domain/Rule/NameMatchModes.php rename to app/Domain/Rule/StringMatchMode.php index 4f91c23..0775545 100644 --- a/app/Domain/Rule/NameMatchModes.php +++ b/app/Domain/Rule/StringMatchMode.php @@ -2,7 +2,7 @@ namespace App\Domain\Rule; -enum NameMatchModes: int +enum StringMatchMode: int { case Exact = 0; case Contains = 1; diff --git a/app/Services/InputMatcher.php b/app/Services/InputMatcher.php deleted file mode 100644 index d2906b2..0000000 --- a/app/Services/InputMatcher.php +++ /dev/null @@ -1,21 +0,0 @@ - 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, - }; - } -} diff --git a/app/Services/Pacs/InstituteMapper.php b/app/Services/Pacs/InstituteMapper.php index 02bca32..9940dfc 100644 --- a/app/Services/Pacs/InstituteMapper.php +++ b/app/Services/Pacs/InstituteMapper.php @@ -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 + */ + 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]; } } diff --git a/app/Services/StringMatcher.php b/app/Services/StringMatcher.php new file mode 100644 index 0000000..d65a9b0 --- /dev/null +++ b/app/Services/StringMatcher.php @@ -0,0 +1,21 @@ + 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, + }; + } +} diff --git a/database/migrations/2024_12_28_175040_create_institute_names_table.php b/database/migrations/2024_12_28_175040_create_institute_names_table.php index 35d4084..bc9cfc4 100644 --- a/database/migrations/2024_12_28_175040_create_institute_names_table.php +++ b/database/migrations/2024_12_28_175040_create_institute_names_table.php @@ -1,6 +1,6 @@ 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(); }); diff --git a/database/seeders/InstituteSeeder.php b/database/seeders/InstituteSeeder.php index 34161a4..5cb4414 100644 --- a/database/seeders/InstituteSeeder.php +++ b/database/seeders/InstituteSeeder.php @@ -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(