src/Entity/JournalArticleClassification.php line 53

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\DBAL\Types\Types;
  5. use Symfony\Component\Serializer\Annotation\Groups,
  6.     Symfony\Component\Validator\Constraints as Assert;
  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\OrderFilter;
  16. use App\Entity\Trait\IdTrait,
  17.     App\Entity\Trait\UuidTrait,
  18.     App\Entity\Trait\OrdTrait,
  19.     App\Entity\Trait\TimestampableTrait,
  20.     App\Entity\Interface\OrderableInterface;
  21. use App\Filter\IriFilter,
  22.     App\Attribute\Sanitizeable,
  23.     App\Repository\JournalArticleClassificationRepository;
  24. #[ApiResource(
  25.     description'Journal article classifications',
  26.     normalizationContext: ['groups' => [
  27.         'read',
  28.         'read:' self::class,
  29.         'read:' self::class . 'Translation'
  30.     ]],
  31.     denormalizationContext: ['groups' => ['write']],
  32.     security'is_granted("' Journal::class . '")',
  33.     order: ['ord' => 'desc'],
  34.     operations: [
  35.         new GetCollection(),
  36.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  37.         new Get(),
  38.         new Put(),
  39.         new Delete(),
  40.     ],
  41.     extraProperties: ['standard_put' => false],
  42. )]
  43. #[ApiFilter(IriFilter::class, properties: ['parent'])]
  44. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  45. #[Assert\GroupSequence(['JournalArticleClassification''additional'])]
  46. #[ORM\Entity(repositoryClassJournalArticleClassificationRepository::class)]
  47. class JournalArticleClassification implements OrderableInterface
  48. {
  49.     use IdTrait,
  50.         UuidTrait,
  51.         OrdTrait,
  52.         TimestampableTrait;
  53.     #[ApiProperty(description'Parent'readableLinkfalsewritableLinkfalse)]
  54.     #[Groups(['read''post'])]
  55.     #[ORM\ManyToOne(targetEntityJournalArticle::class, inversedBy'classifications')]
  56.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  57.     private JournalArticle $parent;
  58.     #[ApiProperty(description'Type'writableLinkfalse)]
  59.     #[Groups(['read:' self::class, 'write'])]
  60.     #[ORM\ManyToOne(targetEntityClassificationType::class)]
  61.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  62.     private ClassificationType $type;
  63.     #[ApiProperty(description'Entry'writableLinkfalse)]
  64.     #[Groups(['read:' self::class, 'write'])]
  65.     #[Assert\Expression(
  66.         expression'value.getRootType() === this.getType()',
  67.         message'Entry must be of selected type.',
  68.         groups: ['additional']
  69.     )]
  70.     #[ORM\ManyToOne(targetEntityClassificationTypeEntry::class)]
  71.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  72.     private ClassificationTypeEntry $entry;
  73.     #[ApiProperty(description'Text')]
  74.     #[Groups(['read''write'])]
  75.     #[Sanitizeable]
  76.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  77.     private ?string $text null;
  78.     public function __construct(JournalArticle $parent)
  79.     {
  80.         $this->setUuid();
  81.         $this->parent $parent;
  82.         $this->createdAt = new \DateTimeImmutable();
  83.         $this->updatedAt = new \DateTimeImmutable();
  84.         $parent->addClassification($this);
  85.     }
  86.     public function getParent(): JournalArticle
  87.     {
  88.         return $this->parent;
  89.     }
  90.     public function getType(): ClassificationType
  91.     {
  92.         return $this->type;
  93.     }
  94.     public function setType(ClassificationType $type): self
  95.     {
  96.         $this->type $type;
  97.         return $this;
  98.     }
  99.     public function getEntry(): ClassificationTypeEntry
  100.     {
  101.         return $this->entry;
  102.     }
  103.     public function setEntry(ClassificationTypeEntry $entry): self
  104.     {
  105.         $this->entry $entry;
  106.         return $this;
  107.     }
  108.     public function getText(): ?string
  109.     {
  110.         return $this->text;
  111.     }
  112.     public function setText(?string $text): self
  113.     {
  114.         $this->text $text;
  115.         return $this;
  116.     }
  117.     public function isValid(): bool
  118.     {
  119.         return $this->entry->getRootType() === $this->type;
  120.     }
  121.     public function clone(JournalArticle $parent): self
  122.     {
  123.         $clone = clone $this;
  124.         $clone->id null;
  125.         $clone->setUuid();
  126.         $clone->parent $parent;
  127.         $clone->parent->addClassification($clone);
  128.         $clone->createdAt = new \DateTimeImmutable();
  129.         $clone->updatedAt = new \DateTimeImmutable();
  130.         return $clone;
  131.     }
  132. }