src/Domain/Entity/TripCountry.php line 41

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Domain\Entity;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use ApiPlatform\Metadata\Delete;
  6. use ApiPlatform\Metadata\Get;
  7. use ApiPlatform\Metadata\GetCollection;
  8. use ApiPlatform\Metadata\Post;
  9. use ApiPlatform\Metadata\Put;
  10. use App\Application\Controller\Api\TripCountry\GetBySlugOrUuidAction;
  11. use App\Application\Service\SluggerService;
  12. use App\Domain\Entity\Behavior\Identifiable;
  13. use App\Domain\Entity\Translation\TripCountryTranslation;
  14. use DateTime;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Doctrine\Common\Collections\Collection;
  17. use Gedmo\SoftDeleteable\Traits\SoftDeleteable;
  18. use Gedmo\Timestampable\Traits\Timestampable;
  19. use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
  20. use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
  21. #[ApiResource(
  22.     operations: [
  23.         new Delete(security"is_granted('ROLE_ADMIN')"),
  24.         new GetCollection(filters: ['trip_country.search_filter''trip_country.query_filter''soft_delete_filter']),
  25.         new Get(
  26.             uriTemplate'trip_countries/{uuid}',
  27.             controllerGetBySlugOrUuidAction::class,
  28.             readfalse,
  29.         ),
  30.         new Post(normalizationContext: ['groups' => ['translations']], security"is_granted('ROLE_ADMIN')"),
  31.         new Put(normalizationContext: ['groups' => ['translations']], security"is_granted('ROLE_ADMIN')"),
  32.     ],
  33.     normalizationContext: ['groups' => ['trip_country_read']],
  34.     denormalizationContext: ['groups' => ['trip_country_write']],
  35.     filters: ['translation.groups']
  36. )]
  37. class TripCountry extends AbstractTranslatable
  38. {
  39.     use Identifiable;
  40.     use SoftDeleteable;
  41.     use Timestampable;
  42.     public Collection $trips;
  43.     public function __construct(
  44.         public Media $photo,
  45.         public Country $country,
  46.         public ?string $remoteId null,
  47.         public ?DateTime $zhistUpdate null,
  48.     ) {
  49.         parent::__construct();
  50.         $this->trips = new ArrayCollection();
  51.     }
  52.     public function getTranslationType(?string $alpha2 null): TripCountryTranslation
  53.     {
  54.         return $this->getTranslation($alpha2);
  55.     }
  56.     public function getName(?string $alpha2 null): ?string
  57.     {
  58.         return $this->getTranslationType($alpha2)->getName();
  59.     }
  60.     public function setName(?string $name): void
  61.     {
  62.         $this->getTranslationType()->setName($name);
  63.     }
  64.     public function createTranslation(): TranslationInterface
  65.     {
  66.         return new TripCountryTranslation();
  67.     }
  68.     public function getSlug(): string
  69.     {
  70.         return SluggerService::formatSlug($this->getName());
  71.     }
  72.     public function getAllSlugs(): array
  73.     {
  74.         return array_values($this->getTranslations()->map(fn(TripCountryTranslation $t) => $t->getSlug())->toArray());
  75.     }
  76. }