42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Pacs;
|
|
|
|
use App\Domain\Rule\StringMatchMode;
|
|
use App\Services\StringMatcher;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class InstituteMapper
|
|
{
|
|
private static array $patterns = [];
|
|
|
|
private static int $catchAll = -1;
|
|
|
|
/**
|
|
* @return array<int, ?int>
|
|
*/
|
|
public static function map(?string $input): array
|
|
{
|
|
if (empty(self::$patterns)) {
|
|
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;
|
|
}
|
|
|
|
if (! blank($input)) {
|
|
$input = strtolower($input);
|
|
|
|
foreach (self::$patterns as $pattern) {
|
|
if (StringMatcher::match($input, $pattern->name, StringMatchMode::from($pattern->match_mode))) {
|
|
return [$pattern->institute_id, $pattern->facility_id];
|
|
}
|
|
}
|
|
}
|
|
|
|
return [self::$catchAll, null];
|
|
}
|
|
}
|