From 7aa00e738c283b260588506f5b179eaa47f9f07e Mon Sep 17 00:00:00 2001 From: Ivan Moroz Date: Fri, 29 May 2026 00:08:57 +0300 Subject: [PATCH] CONV-039: Implement registry lookup methods Co-Authored-By: Claude Opus 4.7 --- app/Support/Converters/ConverterRegistry.php | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/app/Support/Converters/ConverterRegistry.php b/app/Support/Converters/ConverterRegistry.php index 33c5c22..453da71 100644 --- a/app/Support/Converters/ConverterRegistry.php +++ b/app/Support/Converters/ConverterRegistry.php @@ -2,7 +2,9 @@ namespace App\Support\Converters; +use App\Enums\FileFormat; use App\Support\Converters\Contracts\Converter; +use App\Support\Converters\DTO\ConverterTarget; final class ConverterRegistry { @@ -10,4 +12,50 @@ final class ConverterRegistry public function __construct( private readonly array $converters = [], ) {} + + /** @return list */ + public function all(): array + { + return array_values($this->converters); + } + + /** @return list */ + public function forSource(string $source): array + { + $source = FileFormat::normalize($source); + + return array_values(array_filter( + $this->converters, + fn (Converter $converter): bool => $converter->sourceFormat() === $source, + )); + } + + public function find(string $source, string $target): ?Converter + { + $source = FileFormat::normalize($source); + $target = FileFormat::normalize($target); + + foreach ($this->converters as $converter) { + if ($converter->sourceFormat() === $source && $converter->targetFormat() === $target) { + return $converter; + } + } + + return null; + } + + /** @return list */ + public function targetsFor(string $source): array + { + return array_map( + fn (Converter $converter): ConverterTarget => new ConverterTarget( + format: $converter->targetFormat(), + label: $converter->label(), + description: $converter->description(), + converterKey: $converter->key(), + recommended: false, + ), + $this->forSource($source), + ); + } }