src/Entity/ClassificationTypeEntry.php line 65
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM,
Doctrine\DBAL\Types\Types,
Doctrine\Common\Collections\Collection,
Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups,
Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity,
Symfony\Component\Validator\Constraints as Assert;
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\ExistsFilter,
ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use App\Entity\Trait\IdTrait,
App\Entity\Trait\UuidTrait,
App\Entity\Trait\OrdTrait,
App\Entity\Trait\TimestampableTrait,
App\Entity\Trait\DepthTrait,
App\Entity\Interface\OrderableInterface,
App\Entity\Interface\ParentableInterface,
App\Repository\ClassificationTypeEntryRepository,
App\Filter\IriFilter;
use App\Validator\Constraints as CustomAssert;
#[ApiResource(
description: 'Classification type entries',
normalizationContext: ['groups' => [
'read',
'read:' . self::class,
'read:' . self::class . 'Translation'
]],
denormalizationContext: ['groups' => ['write']],
security: 'is_granted("' . ClassificationType::class . '")',
order: ['ord' => 'desc'],
operations: [
new GetCollection(),
new Post(denormalizationContext: ['groups' => ['write', 'post']]),
new Get(),
new Put(),
new Delete(),
],
extraProperties: ['standard_put' => false],
)]
#[ApiFilter(ExistsFilter::class, properties: ['parent', 'type'])]
#[ApiFilter(IriFilter::class, properties: ['parent', 'type'])]
#[ApiFilter(OrderFilter::class, properties: ['ord'])]
#[Assert\Expression(
expression: 'value.getParent() || value.getType()',
message: 'Entry must be assigned to either parent or type.'
)]
#[UniqueEntity(fields: ['parent', 'code'], message: 'This code already exists.')]
#[ORM\UniqueConstraint(fields: ['parent', 'code'])]
#[ORM\Entity(repositoryClass: ClassificationTypeEntryRepository::class)]
class ClassificationTypeEntry implements OrderableInterface, ParentableInterface
{
use IdTrait,
UuidTrait,
OrdTrait,
TimestampableTrait,
DepthTrait;
#[ApiProperty(description: 'Parent entry', readableLink: false, writableLink: false)]
#[Groups(['read:' . self::class, 'post'])]
#[CustomAssert\Depth(max: 5)]
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
#[ORM\JoinColumn(onDelete: 'cascade')]
private ?self $parent = null;
#[ApiProperty(description: 'Title')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: Types::STRING, length: 1023)]
private string $title;
#[ApiProperty(description: 'Code')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: Types::STRING, length: 255)]
private string $code;
#[ApiProperty(description: 'Type of entry', writableLink: false)]
#[Groups(['read:' . self::class, 'post', 'write:depth:0'])]
#[ORM\ManyToOne(targetEntity: ClassificationType::class, inversedBy: 'entries')]
#[ORM\JoinColumn(onDelete: 'set null')]
private ?ClassificationType $type = null;
#[ApiProperty(description: 'Children entries', writableLink: false)]
#[Groups(['read:' . self::class])]
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')]
#[ORM\OrderBy(['ord' => 'desc'])]
private Collection $children;
public function __construct(?ClassificationTypeEntry $parent, ?ClassificationType $type)
{
$this->setUuid();
$this->parent = $parent;
$this->type = $parent ? null : $type;
$this->children = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
$parent?->addChild($this);
}
public function getParent(): ?self
{
return $this->parent;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code): self
{
$this->code = $code;
return $this;
}
public function getType(): ?ClassificationType
{
return $this->type;
}
public function getRootType(ClassificationTypeEntry $entry = null): ClassificationType
{
$current = $entry ?? $this;
if ($current->getType()) {
return $current->getType();
}
return $this->getRootType($current->getParent());
}
/**
* @return Collection|Affiliation[]
*/
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(self $child): self
{
if ($this->children->contains($child)) {
return $this;
}
$this->children[] = $child;
return $this;
}
public function removeChild(self $child): self
{
$this->children->removeElement($child);
return $this;
}
}