| 123456789101112131415161718192021222324252627282930313233343536373839 |
- <?php
- declare(strict_types=1);
- namespace App\Service\ServiceIterator;
- use App\ApiResources\Export\ExportRequest;
- use App\Service\Export\Encoder\EncoderInterface;
- use Exception;
- /**
- * 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 $encoders
- */
- public function __construct(
- 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');
- }
- }
|