|
|
@@ -0,0 +1,68 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+declare(strict_types=1);
|
|
|
+
|
|
|
+namespace App\State\Provider\Core;
|
|
|
+
|
|
|
+use ApiPlatform\Metadata\GetCollection;
|
|
|
+use ApiPlatform\Metadata\Operation;
|
|
|
+use ApiPlatform\State\ProviderInterface;
|
|
|
+use App\ApiResources\Core\EventCategory;
|
|
|
+use App\Entity\Core\Categories;
|
|
|
+use Doctrine\ORM\EntityManagerInterface;
|
|
|
+use Symfony\Component\HttpFoundation\Response;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Class EventCategoryProvider : custom provider pour assurer l'alimentation de la ressource EventCategory.
|
|
|
+ */
|
|
|
+final class EventCategoryProvider implements ProviderInterface
|
|
|
+{
|
|
|
+ public function __construct(
|
|
|
+ private readonly EntityManagerInterface $entityManager,
|
|
|
+ ) {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param mixed[] $uriVariables
|
|
|
+ * @param mixed[] $context
|
|
|
+ *
|
|
|
+ * @return EventCategory[]|EventCategory
|
|
|
+ */
|
|
|
+ public function provide(Operation $operation, array $uriVariables = [], array $context = []): array|EventCategory
|
|
|
+ {
|
|
|
+ if (!$operation instanceof GetCollection) {
|
|
|
+ throw new \RuntimeException('Only GetCollection operation is supported', Response::HTTP_METHOD_NOT_ALLOWED);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->getCollection();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return EventCategory[]
|
|
|
+ */
|
|
|
+ private function getCollection(): array
|
|
|
+ {
|
|
|
+ $categoriesRepository = $this->entityManager->getRepository(Categories::class);
|
|
|
+ $categories = $categoriesRepository->findAll();
|
|
|
+
|
|
|
+ $result = [];
|
|
|
+ foreach ($categories as $category) {
|
|
|
+ $eventCategory = new EventCategory();
|
|
|
+ $eventCategory->setId($category->getId());
|
|
|
+
|
|
|
+ // Get the related entities
|
|
|
+ $familly = $category->getFamilly();
|
|
|
+ $subfamilly = $category->getSubfamilly();
|
|
|
+ $gender = $category->getGender();
|
|
|
+
|
|
|
+ // Generate labels for the related entities based on their IDs
|
|
|
+ $eventCategory->setFamillyLabel($familly->getName());
|
|
|
+ $eventCategory->setSubfamillyLabel($subfamilly->getName());
|
|
|
+ $eventCategory->setGenderLabel($gender->getName());
|
|
|
+
|
|
|
+ $result[] = $eventCategory;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+}
|