<?php

namespace NoahVet\Reef\Infrastructure\Security\IAM\Mapper;

use NoahVet\Reef\Domain\Security\Model\Resource;

/**
 * Delegates entity-to-resource mapping to a set of specialized mappers.
 * Inject all InternalResourceMapperInterface implementations to this service.
 */
class IAMResourceMapper implements IAMResourceMapperInterface
{
    /**
     * @var iterable<InternalResourceMapperInterface>
     */
    protected iterable $mappers;

    /**
     * @param iterable<InternalResourceMapperInterface> $mappers
     */
    public function __construct(
        iterable $mappers,
    ) {
        $this->mappers = $mappers;
    }

    #[\Override]
    public function map(mixed $subject): ?Resource
    {
        foreach ($this->mappers as $mapper) {
            if ($mapper->canMap($subject)) {
                return $mapper->mapResource($subject);
            }
        }

        return null;
    }
}
