src/Entity/Domain.php line 65
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM,
Doctrine\Common\Collections\Collection,
Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Metadata\ApiResource,
ApiPlatform\Metadata\ApiProperty,
ApiPlatform\Metadata\Get,
ApiPlatform\Metadata\GetCollection,
ApiPlatform\Metadata\Post,
ApiPlatform\Metadata\Put,
ApiPlatform\Metadata\Delete,
ApiPlatform\Metadata\ApiFilter,
ApiPlatform\Doctrine\Orm\Filter\SearchFilter,
ApiPlatform\Doctrine\Orm\Filter\ExistsFilter,
ApiPlatform\Doctrine\Orm\Filter\BooleanFilter,
ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use App\Entity\Trait\IdTrait,
App\Entity\Trait\UuidTrait,
App\Entity\Trait\OrdStatTrait,
App\Entity\Trait\TimestampableTrait,
App\Entity\Trait\TranslatableTrait,
App\Entity\Trait\Languages\SiteLanguagesTrait,
App\Entity\Trait\DepthTrait,
App\Entity\Interface\TranslatableInterface,
App\Entity\Interface\OrdStatableInterface,
App\Entity\Interface\ParentableInterface,
App\Entity\Interface\LanguageableInterface,
App\Filter\IriFilter,
App\Repository\DomainRepository,
App\Enum\Language;
use App\Validator\Constraints as CustomAssert;
#[ApiResource(
description: 'Domains',
normalizationContext: ['groups' => [
'read',
'read:' . self::class,
'read:' . self::class . 'Translation'
]],
denormalizationContext: ['groups' => ['write']],
security: 'is_granted("' . self::class . '")',
order: ['ord' => 'desc'],
operations: [
new GetCollection(),
new Post(denormalizationContext: ['groups' => ['write', 'post']]),
new Get(),
new Put(),
new Delete(),
],
extraProperties: ['standard_put' => false],
)]
#[ApiFilter(SearchFilter::class, properties: ['translations.title' => 'partial'])]
#[ApiFilter(ExistsFilter::class, properties: ['parent'])]
#[ApiFilter(IriFilter::class, properties: ['parent'])]
#[ApiFilter(BooleanFilter::class, properties: ['stat'])]
#[ApiFilter(OrderFilter::class, properties: ['ord'])]
#[ORM\Entity(repositoryClass: DomainRepository::class)]
class Domain implements TranslatableInterface, OrdStatableInterface, ParentableInterface, LanguageableInterface
{
use IdTrait,
UuidTrait,
OrdStatTrait,
TimestampableTrait,
TranslatableTrait,
SiteLanguagesTrait,
DepthTrait;
#[ApiProperty(description: 'Parent domain', readableLink: false)]
#[Groups(['read:' . self::class, 'post'])]
#[CustomAssert\Depth(max: 1)]
#[ORM\ManyToOne(targetEntity: Domain::class, inversedBy: 'children')]
#[ORM\JoinColumn(onDelete: 'cascade')]
private ?Domain $parent = null;
#[ApiProperty(description: 'Children domains', writableLink: false)]
#[Groups(['read:' . self::class])]
#[ORM\OneToMany(targetEntity: Domain::class, mappedBy: 'parent')]
#[ORM\OrderBy(['ord' => 'DESC'])]
private Collection $children;
public function __construct(?Domain $parent, ?array $languages)
{
$this->setUuid();
$this->parent = $parent;
$this->translations = new ArrayCollection();
$this->children = new ArrayCollection();
$this->setLanguages($languages ?? Language::DEFAULT_SITE);
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
$parent?->addChild($this);
}
public function getParent(): ?self
{
return $this->parent;
}
/**
* @return Collection|Domain[]
*/
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(Domain $child): self
{
if ($this->children->contains($child)) {
return $this;
}
$this->children[] = $child;
return $this;
}
public function removeChild(Domain $child): self
{
$this->children->removeElement($child);
return $this;
}
}