<?php
declare(strict_types=1);

namespace App\Service\ServiceIterator;

use App\Service\Export\Encoder\EncoderInterface;
use RuntimeException;

/**
 * Permet d'itérer sur les services d'encodage
 */
class EncoderIterator
{
    /**
     * Pour l'injection des services, voir config/services.yaml, section 'TAG Services'
     * @param iterable<EncoderInterface> $encoders
     */
    public function __construct(
        readonly private iterable $encoders,
    ) {}

    /**
     * Itère sur les services d'encodage disponibles et
     * retourne le premier qui supporte ce type de requête.
     *
     * @param string $format
     * @return EncoderInterface
     */
    public function getEncoderFor(string $format): EncoderInterface
    {
        /** @var EncoderInterface $encoder */
        foreach ($this->encoders as $encoder){
            if($encoder->support($format)) {
                return $encoder;
            }
        }
        throw new RuntimeException('no encoder found for this export request');
    }
}
