src/Entity/Domain.php line 65

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\Common\Collections\Collection,
  5.     Doctrine\Common\Collections\ArrayCollection;
  6. use Symfony\Component\Serializer\Annotation\Groups;
  7. use ApiPlatform\Metadata\ApiResource,
  8.     ApiPlatform\Metadata\ApiProperty,
  9.     ApiPlatform\Metadata\Get,
  10.     ApiPlatform\Metadata\GetCollection,
  11.     ApiPlatform\Metadata\Post,
  12.     ApiPlatform\Metadata\Put,
  13.     ApiPlatform\Metadata\Delete,
  14.     ApiPlatform\Metadata\ApiFilter,
  15.     ApiPlatform\Doctrine\Orm\Filter\SearchFilter,
  16.     ApiPlatform\Doctrine\Orm\Filter\ExistsFilter,
  17.     ApiPlatform\Doctrine\Orm\Filter\BooleanFilter,
  18.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  19. use App\Entity\Trait\IdTrait,
  20.     App\Entity\Trait\UuidTrait,
  21.     App\Entity\Trait\OrdStatTrait,
  22.     App\Entity\Trait\TimestampableTrait,
  23.     App\Entity\Trait\TranslatableTrait,
  24.     App\Entity\Trait\Languages\SiteLanguagesTrait,
  25.     App\Entity\Trait\DepthTrait,
  26.     App\Entity\Interface\TranslatableInterface,
  27.     App\Entity\Interface\OrdStatableInterface,
  28.     App\Entity\Interface\ParentableInterface,
  29.     App\Entity\Interface\LanguageableInterface,
  30.     App\Filter\IriFilter,
  31.     App\Repository\DomainRepository,
  32.     App\Enum\Language;
  33. use App\Validator\Constraints as CustomAssert;
  34. #[ApiResource(
  35.     description'Domains',
  36.     normalizationContext: ['groups' => [
  37.         'read',
  38.         'read:' self::class,
  39.         'read:' self::class . 'Translation'
  40.     ]],
  41.     denormalizationContext: ['groups' => ['write']],
  42.     security'is_granted("' self::class . '")',
  43.     order: ['ord' => 'desc'],
  44.     operations: [
  45.         new GetCollection(),
  46.         new Post(denormalizationContext: ['groups' => ['write''post']]),
  47.         new Get(),
  48.         new Put(),
  49.         new Delete(),
  50.     ],
  51.     extraProperties: ['standard_put' => false],
  52. )]
  53. #[ApiFilter(SearchFilter::class, properties: ['translations.title' => 'partial'])]
  54. #[ApiFilter(ExistsFilter::class, properties: ['parent'])]
  55. #[ApiFilter(IriFilter::class, properties: ['parent'])]
  56. #[ApiFilter(BooleanFilter::class, properties: ['stat'])]
  57. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  58. #[ORM\Entity(repositoryClassDomainRepository::class)]
  59. class Domain implements TranslatableInterfaceOrdStatableInterfaceParentableInterfaceLanguageableInterface
  60. {
  61.     use IdTrait,
  62.         UuidTrait,
  63.         OrdStatTrait,
  64.         TimestampableTrait,
  65.         TranslatableTrait,
  66.         SiteLanguagesTrait,
  67.         DepthTrait;
  68.     #[ApiProperty(description'Parent domain'readableLinkfalse)]
  69.     #[Groups(['read:' self::class, 'post'])]
  70.     #[CustomAssert\Depth(max1)]
  71.     #[ORM\ManyToOne(targetEntityDomain::class, inversedBy'children')]
  72.     #[ORM\JoinColumn(onDelete'cascade')]
  73.     private ?Domain $parent null;
  74.     #[ApiProperty(description'Children domains'writableLinkfalse)]
  75.     #[Groups(['read:' self::class])]
  76.     #[ORM\OneToMany(targetEntityDomain::class, mappedBy'parent')]
  77.     #[ORM\OrderBy(['ord' => 'DESC'])]
  78.     private Collection $children;
  79.     public function __construct(?Domain $parent, ?array $languages)
  80.     {
  81.         $this->setUuid();
  82.         $this->parent $parent;
  83.         $this->translations = new ArrayCollection();
  84.         $this->children = new ArrayCollection();
  85.         $this->setLanguages($languages ?? Language::DEFAULT_SITE);
  86.         $this->createdAt = new \DateTimeImmutable();
  87.         $this->updatedAt = new \DateTimeImmutable();
  88.         $parent?->addChild($this);
  89.     }
  90.     public function getParent(): ?self
  91.     {
  92.         return $this->parent;
  93.     }
  94.     /**
  95.      * @return Collection|Domain[]
  96.      */
  97.     public function getChildren(): Collection
  98.     {
  99.         return $this->children;
  100.     }
  101.     public function addChild(Domain $child): self
  102.     {
  103.         if ($this->children->contains($child)) {
  104.             return $this;
  105.         }
  106.         $this->children[] = $child;
  107.         return $this;
  108.     }
  109.     public function removeChild(Domain $child): self
  110.     {
  111.         $this->children->removeElement($child);
  112.         return $this;
  113.     }
  114. }