src/Entity/ClassificationTypeEntry.php line 65

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\DBAL\Types\Types,
  5.     Doctrine\Common\Collections\Collection,
  6.     Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Serializer\Annotation\Groups,
  8.     Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity,
  9.     Symfony\Component\Validator\Constraints as Assert;
  10. use ApiPlatform\Metadata\ApiResource,
  11.     ApiPlatform\Metadata\ApiProperty,
  12.     ApiPlatform\Metadata\Get,
  13.     ApiPlatform\Metadata\GetCollection,
  14.     ApiPlatform\Metadata\Post,
  15.     ApiPlatform\Metadata\Put,
  16.     ApiPlatform\Metadata\Delete,
  17.     ApiPlatform\Metadata\ApiFilter,
  18.     ApiPlatform\Doctrine\Orm\Filter\ExistsFilter,
  19.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  20. use App\Entity\Trait\IdTrait,
  21.     App\Entity\Trait\UuidTrait,
  22.     App\Entity\Trait\OrdTrait,
  23.     App\Entity\Trait\TimestampableTrait,
  24.     App\Entity\Trait\DepthTrait,
  25.     App\Entity\Interface\OrderableInterface,
  26.     App\Entity\Interface\ParentableInterface,
  27.     App\Repository\ClassificationTypeEntryRepository,
  28.     App\Filter\IriFilter;
  29. use App\Validator\Constraints as CustomAssert;
  30. #[ApiResource(
  31.     description'Classification type entries',
  32.     normalizationContext: ['groups' => [
  33.         'read',
  34.         'read:' self::class,
  35.         'read:' self::class . 'Translation'
  36.     ]],
  37.     denormalizationContext: ['groups' => ['write']],
  38.     security'is_granted("' ClassificationType::class . '")',
  39.     order: ['ord' => 'desc'],
  40.     operations: [
  41.         new GetCollection(),
  42.         new Post(denormalizationContext: ['groups' => ['write''post']]),
  43.         new Get(),
  44.         new Put(),
  45.         new Delete(),
  46.     ],
  47.     extraProperties: ['standard_put' => false],
  48. )]
  49. #[ApiFilter(ExistsFilter::class, properties: ['parent''type'])]
  50. #[ApiFilter(IriFilter::class, properties: ['parent''type'])]
  51. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  52. #[Assert\Expression(
  53.     expression'value.getParent() || value.getType()',
  54.     message'Entry must be assigned to either parent or type.'
  55. )]
  56. #[UniqueEntity(fields: ['parent''code'], message'This code already exists.')]
  57. #[ORM\UniqueConstraint(fields: ['parent''code'])]
  58. #[ORM\Entity(repositoryClassClassificationTypeEntryRepository::class)]
  59. class ClassificationTypeEntry implements OrderableInterfaceParentableInterface
  60. {
  61.     use IdTrait,
  62.         UuidTrait,
  63.         OrdTrait,
  64.         TimestampableTrait,
  65.         DepthTrait;
  66.     #[ApiProperty(description'Parent entry'readableLinkfalsewritableLinkfalse)]
  67.     #[Groups(['read:' self::class, 'post'])]
  68.     #[CustomAssert\Depth(max5)]
  69.     #[ORM\ManyToOne(targetEntityself::class, inversedBy'children')]
  70.     #[ORM\JoinColumn(onDelete'cascade')]
  71.     private ?self $parent null;
  72.     #[ApiProperty(description'Title')]
  73.     #[Groups(['read''write'])]
  74.     #[ORM\Column(typeTypes::STRINGlength1023)]
  75.     private string $title;
  76.     #[ApiProperty(description'Code')]
  77.     #[Groups(['read''write'])]
  78.     #[ORM\Column(typeTypes::STRINGlength255)]
  79.     private string $code;
  80.     #[ApiProperty(description'Type of entry'writableLinkfalse)]
  81.     #[Groups(['read:' self::class, 'post''write:depth:0'])]
  82.     #[ORM\ManyToOne(targetEntityClassificationType::class, inversedBy'entries')]
  83.     #[ORM\JoinColumn(onDelete'set null')]
  84.     private ?ClassificationType $type null;
  85.     #[ApiProperty(description'Children entries'writableLinkfalse)]
  86.     #[Groups(['read:' self::class])]
  87.     #[ORM\OneToMany(targetEntityself::class, mappedBy'parent')]
  88.     #[ORM\OrderBy(['ord' => 'desc'])]
  89.     private Collection $children;
  90.     public function __construct(?ClassificationTypeEntry $parent, ?ClassificationType $type)
  91.     {
  92.         $this->setUuid();
  93.         $this->parent $parent;
  94.         $this->type $parent null $type;
  95.         $this->children = new ArrayCollection();
  96.         $this->createdAt = new \DateTimeImmutable();
  97.         $this->updatedAt = new \DateTimeImmutable();
  98.         $parent?->addChild($this);
  99.     }
  100.     public function getParent(): ?self
  101.     {
  102.         return $this->parent;
  103.     }
  104.     public function getTitle(): ?string
  105.     {
  106.         return $this->title;
  107.     }
  108.     public function setTitle(?string $title): self
  109.     {
  110.         $this->title $title;
  111.         return $this;
  112.     }
  113.     public function getCode(): ?string
  114.     {
  115.         return $this->code;
  116.     }
  117.     public function setCode(?string $code): self
  118.     {
  119.         $this->code $code;
  120.         return $this;
  121.     }
  122.     public function getType(): ?ClassificationType
  123.     {
  124.         return $this->type;
  125.     }
  126.     public function getRootType(ClassificationTypeEntry $entry null): ClassificationType
  127.     {
  128.         $current $entry ?? $this;
  129.         if ($current->getType()) {
  130.             return $current->getType();
  131.         }
  132.         return $this->getRootType($current->getParent());
  133.     }
  134.     /**
  135.      * @return Collection|Affiliation[]
  136.      */
  137.     public function getChildren(): Collection
  138.     {
  139.         return $this->children;
  140.     }
  141.     public function addChild(self $child): self
  142.     {
  143.         if ($this->children->contains($child)) {
  144.             return $this;
  145.         }
  146.         $this->children[] = $child;
  147.         return $this;
  148.     }
  149.     public function removeChild(self $child): self
  150.     {
  151.         $this->children->removeElement($child);
  152.         return $this;
  153.     }
  154. }