| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- declare(strict_types=1);
- namespace App\State\Provider\Enum;
- use ApiPlatform\Metadata\GetCollection;
- use ApiPlatform\Metadata\Operation;
- use ApiPlatform\State\ProviderInterface;
- use App\ApiResources\Enum\Enum;
- use Symfony\Component\HttpFoundation\Response;
- use App\Service\Utils\Parser\YamlParser;
- use App\Service\Utils\Reflection;
- use Exception;
- use RuntimeException;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- /**
- * Class EnumProvider : custom provider pour assurer l'alimentation des enums
- * @package App\DataProvider\Access
- */
- final class EnumProvider implements ProviderInterface
- {
- public function __construct(
- private Reflection $reflection,
- private readonly ParameterBagInterface $parameterBag
- )
- { }
- /**
- * @param Operation $operation
- * @param mixed[] $uriVariables
- * @param mixed[] $context
- * @return Enum|null
- */
- public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?Enum
- {
- if($operation instanceof GetCollection) {
- throw new RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
- }
- $id = $uriVariables['name'];
- $enums = $this->parameterBag->get('opentalent.enum');
- $enumClass = $enums[$id];
- if(!$enumClass) {
- throw new NotFoundHttpException(sprintf('Enum %s does\'nt exist', $id));
- }
- try{
- $items = $this->reflection->dynamicInvokeClassWithArgsAndMethod(
- $enumClass,
- 'toArray'
- );
- }catch (Exception){
- throw new NotFoundHttpException(sprintf('Enum %s does\'nt exist', $id));
- }
- $enumResponse = new Enum();
- $enumResponse->setName($id);
- $enumResponse->setItems($items);
- return $enumResponse;
- }
- }
|