<?php
declare(strict_types=1);
namespace App\Domain\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Application\Controller\Api\TripCategory\GetBySlugOrUuidAction;
use App\Application\Service\SluggerService;
use App\Domain\Entity\Behavior\Identifiable;
use App\Domain\Entity\Translation\TripCategoryTranslation;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Gedmo\SoftDeleteable\Traits\SoftDeleteable;
use Gedmo\Timestampable\Traits\Timestampable;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
#[ApiResource(
operations : [
new Delete(security: "is_granted('ROLE_ADMIN')"),
new GetCollection(filters: ['trip_category.search_filter', 'trip_category.query_filter', 'soft_delete_filter']),
new Get(
uriTemplate: 'trip_categories/{uuid}',
controller : GetBySlugOrUuidAction::class,
read : false,
),
new Post(normalizationContext: ['groups' => ['translations']], security: "is_granted('ROLE_ADMIN')"),
new Put(normalizationContext: ['groups' => ['translations']], security: "is_granted('ROLE_ADMIN')"),
],
normalizationContext : ['groups' => ['trip_category_read']],
denormalizationContext: ['groups' => ['trip_category_write']],
filters : ['translation.groups']
)]
class TripCategory extends AbstractTranslatable
{
use Identifiable;
use SoftDeleteable;
use Timestampable;
public Collection $trips;
public ?DateTime $zhistUpdate = null;
public function __construct(
public Media $photo,
public Media $logo,
public ?string $remoteId = null,
// For Input Dto only, not doctrine mapped
public int $tripsCount = 0,
) {
parent::__construct();
$this->trips = new ArrayCollection();
}
public function getTranslationType(): TripCategoryTranslation
{
return $this->getTranslation();
}
public function getName(): ?string
{
return $this->getTranslationType()->getName();
}
public function setName(?string $name): void
{
$this->getTranslationType()->setName($name);
}
public function createTranslation(): TranslationInterface
{
return new TripCategoryTranslation();
}
public function getSlug(): string
{
return SluggerService::formatSlug($this->getName());
}
}