<?php
declare(strict_types=1);

namespace App\DataProvider\Utils;

use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\ApiResources\Utils\Siret;
use App\Service\Utils\Siret as SiretUtils;

/**
 * Class SiretDataProvider : custom provider pour assurer l'alimentation des utils Siret
 * @package App\DataProvider\Utils
 */
final class SiretDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
    public function __construct(
        private SiretUtils $siretUtils
    )
    { }

    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
    {
        return Siret::class === $resourceClass;
    }

    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?Siret
    {
        $siretResponse = new Siret();
        $siretResponse->setNumber($id);
        $siretResponse->setIsCorrect($this->siretUtils->isSiretCorrect($id));
        return $siretResponse;
    }
}
