Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions app/Support/Converters/ConverterRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,60 @@

namespace App\Support\Converters;

use App\Enums\FileFormat;
use App\Support\Converters\Contracts\Converter;
use App\Support\Converters\DTO\ConverterTarget;

final class ConverterRegistry
{
/** @param list<Converter> $converters */
public function __construct(
private readonly array $converters = [],
) {}

/** @return list<Converter> */
public function all(): array
{
return array_values($this->converters);
}

/** @return list<Converter> */
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<ConverterTarget> */
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),
);
}
}