src/Entity/JournalIssueSection.php line 48

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Serializer\Annotation\Groups,
  5.     Symfony\Component\Validator\Constraints as Assert,
  6.     Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  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\OrdStatTrait,
  19.     App\Entity\Trait\TimestampableTrait,
  20.     App\Entity\Interface\OrdStatableInterface,
  21.     App\Repository\JournalIssueSectionRepository;
  22. use App\Filter\IriFilter;
  23. #[ApiResource(
  24.     description'Journal issue sections',
  25.     normalizationContext: ['groups' => ['read''read:' self::class]],
  26.     denormalizationContext: ['groups' => ['write']],
  27.     security'is_granted("' Journal::class . '")',
  28.     order: ['ord' => 'desc'],
  29.     operations: [
  30.         new GetCollection(),
  31.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  32.         new Get(),
  33.         new Put(),
  34.         new Delete(),
  35.     ],
  36.     extraProperties: ['standard_put' => false],
  37. )]
  38. #[ApiFilter(IriFilter::class, properties: ['parent'])]
  39. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  40. #[UniqueEntity(fields: ['parent''section'])]
  41. #[ORM\Entity(repositoryClassJournalIssueSectionRepository::class)]
  42. class JournalIssueSection implements OrdStatableInterface
  43. {
  44.     use IdTrait,
  45.         UuidTrait,
  46.         OrdStatTrait,
  47.         TimestampableTrait;
  48.     #[ApiProperty(description'Parent'readableLinkfalsewritableLinkfalse)]
  49.     #[Groups(['read''post'])]
  50.     #[Assert\NotNull]
  51.     #[ORM\ManyToOne(targetEntityJournalIssue::class, inversedBy'sections')]
  52.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  53.     private JournalIssue $parent;
  54.     #[ApiProperty(description'Section'writableLinkfalse)]
  55.     #[Groups(['read''write'])]
  56.     #[Assert\Expression(
  57.         expression'!value || value.getParent() === this.getParent().getParent()',
  58.         message'Cannot assign section from the outside of current journal.'
  59.     )]
  60.     #[ORM\ManyToOne(targetEntityJournalSection::class)]
  61.     #[ORM\JoinColumn(onDelete'cascade'nullabletrue)]
  62.     private ?JournalSection $section null;
  63.     public function __construct(JournalIssue $parent)
  64.     {
  65.         $this->setUuid();
  66.         $this->parent $parent;
  67.         $this->createdAt = new \DateTimeImmutable();
  68.         $this->updatedAt = new \DateTimeImmutable();
  69.         $parent->addSection($this);
  70.     }
  71.     public function getParent(): JournalIssue
  72.     {
  73.         return $this->parent;
  74.     }
  75.     public function getSection(): ?JournalSection
  76.     {
  77.         return $this->section;
  78.     }
  79.     public function setSection(JournalSection $section): self
  80.     {
  81.         $this->section $section;
  82.         return $this;
  83.     }
  84.     public function clone(JournalIssue $parent): self
  85.     {
  86.         $clone = clone $this;
  87.         $clone->id null;
  88.         $clone->setUuid();
  89.         $clone->parent $parent;
  90.         $clone->parent->addSection($clone);
  91.         $clone->createdAt = new \DateTimeImmutable();
  92.         $clone->updatedAt = new \DateTimeImmutable();
  93.         return $clone;
  94.     }
  95. }