<?php
namespace App\State;
use ApiPlatform\Metadata\Operation;
use App\Dto\CategoryOutput;
use App\Entity\Category;
use ApiPlatform\State\ProviderInterface;
final class CategoryRepresentationProvider implements ProviderInterface
{
public function __construct(private ProviderInterface $itemProvider)
{
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$category = $this->itemProvider->provide($operation, $uriVariables, $context);
$categoryOutput = new CategoryOutput();
$categoryOutput->setId($category->getId());
$categoryOutput->setName($category->getName());
$categoryOutput->setSlug($category->getSlug());
$categoryOutput->setType($category->getType());
if ($category->getParent()) {
$categoryOutput->setParentType($category->getParent()->getType());
$categoryOutput->setParentName($category->getParent()->getName());
} else {
$categoryOutput->setParentType(null);
$categoryOutput->setParentName(null);
}
return $categoryOutput;
}
}