<?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\TripCountry\GetBySlugOrUuidAction;
use App\Application\Service\SluggerService;
use App\Domain\Entity\Behavior\Identifiable;
use App\Domain\Entity\Translation\TripCountryTranslation;
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_country.search_filter', 'trip_country.query_filter', 'soft_delete_filter']),
new Get(
uriTemplate: 'trip_countries/{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_country_read']],
denormalizationContext: ['groups' => ['trip_country_write']],
filters: ['translation.groups']
)]
class TripCountry extends AbstractTranslatable
{
use Identifiable;
use SoftDeleteable;
use Timestampable;
public Collection $trips;
public function __construct(
public Media $photo,
public Country $country,
public ?string $remoteId = null,
public ?DateTime $zhistUpdate = null,
) {
parent::__construct();
$this->trips = new ArrayCollection();
}
public function getTranslationType(?string $alpha2 = null): TripCountryTranslation
{
return $this->getTranslation($alpha2);
}
public function getName(?string $alpha2 = null): ?string
{
return $this->getTranslationType($alpha2)->getName();
}
public function setName(?string $name): void
{
$this->getTranslationType()->setName($name);
}
public function createTranslation(): TranslationInterface
{
return new TripCountryTranslation();
}
public function getSlug(): string
{
return SluggerService::formatSlug($this->getName());
}
public function getAllSlugs(): array
{
return array_values($this->getTranslations()->map(fn(TripCountryTranslation $t) => $t->getSlug())->toArray());
}
}