<?php


namespace AutoAdmin\Bundle\Media\Mapper;


use AutoAdmin\Bundle\Media\Model\MediaInterface;
use Traversable;

class MapperManager
{
    /**
     * @var MapperInterface[]
     */
    private array $mappers;

    public function __construct(Traversable $mappers)
    {
        $this->mappers = array_map([$this, 'addMapper'], iterator_to_array($mappers));
    }

    public function addMapper(MapperInterface $mapper): MapperInterface
    {
        return $mapper;
    }

    public function getMapper(MediaInterface $media)
    {
        foreach ($this->mappers as $mapper) {
            if ($mapper->supports($media)) {
                return $mapper;
            }
        }

        return null;
    }
}