47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\StudyRouter;
|
|
|
|
use App\Domain\Rule\MatchMode;
|
|
use App\Models\DicomRoutingRule;
|
|
use App\Services\ContentMatcher;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class DicomStudyRouter
|
|
{
|
|
private static ?Collection $rules;
|
|
|
|
private static int $catchAll = -1;
|
|
|
|
/**
|
|
* @return array<int, ?int>
|
|
*/
|
|
public static function map(?string $input): array
|
|
{
|
|
if (is_null(self::$rules)) {
|
|
self::$rules = Cache::remember('dicom.routers',
|
|
now()->addMinutes(15),
|
|
fn () => DicomRoutingRule::active()->with('conditions')->get()
|
|
);
|
|
self::$catchAll = DB::table('institutes')
|
|
->where('name', 'Catch-all')
|
|
->first('id')
|
|
->id;
|
|
}
|
|
|
|
if (! blank($input)) {
|
|
$input = strtolower($input);
|
|
|
|
foreach (self::$rules as $pattern) {
|
|
if (ContentMatcher::match($input, $pattern->name, MatchMode::from($pattern->match_mode))) {
|
|
return [$pattern->institute_id, $pattern->facility_id];
|
|
}
|
|
}
|
|
}
|
|
|
|
return [self::$catchAll, null];
|
|
}
|
|
}
|