src/State/CategoryRepresentationProvider.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\State;
  3. use ApiPlatform\Metadata\Operation;
  4. use App\Dto\CategoryOutput;
  5. use App\Entity\Category;
  6. use ApiPlatform\State\ProviderInterface;
  7. final class CategoryRepresentationProvider implements ProviderInterface
  8. {
  9.     public function __construct(private ProviderInterface $itemProvider)
  10.     {
  11.     }
  12.     public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
  13.     {
  14.         $category $this->itemProvider->provide($operation$uriVariables$context);
  15.         $categoryOutput = new CategoryOutput();
  16.         $categoryOutput->setId($category->getId());
  17.         $categoryOutput->setName($category->getName());
  18.         $categoryOutput->setSlug($category->getSlug());
  19.         $categoryOutput->setType($category->getType());
  20.         if ($category->getParent()) {
  21.             $categoryOutput->setParentType($category->getParent()->getType());
  22.             $categoryOutput->setParentName($category->getParent()->getName());
  23.         } else {
  24.             $categoryOutput->setParentType(null);
  25.             $categoryOutput->setParentName(null);
  26.         }
  27.         return $categoryOutput;
  28.     }
  29. }