src/Entity/LandingJournal.php line 46

  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. use ApiPlatform\Metadata\ApiResource,
  7.     ApiPlatform\Metadata\ApiProperty,
  8.     ApiPlatform\Metadata\Get,
  9.     ApiPlatform\Metadata\GetCollection,
  10.     ApiPlatform\Metadata\Post,
  11.     ApiPlatform\Metadata\Put,
  12.     ApiPlatform\Metadata\Delete,
  13.     ApiPlatform\Metadata\ApiFilter,
  14.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  15. use App\Entity\Trait\IdTrait,
  16.     App\Entity\Trait\UuidTrait,
  17.     App\Entity\Trait\OrdStatTrait,
  18.     App\Entity\Trait\TimestampableTrait,
  19.     App\Entity\Interface\OrdStatableInterface,
  20.     App\Repository\LandingJournalRepository;
  21. use App\Filter\IriFilter;
  22. #[ApiResource(
  23.     description'Landing journals',
  24.     normalizationContext: ['groups' => ['read''read:' self::class]],
  25.     denormalizationContext: ['groups' => ['write']],
  26.     security'is_granted("' Landing::class . '")',
  27.     order: ['ord' => 'desc'],
  28.     operations: [
  29.         new GetCollection(),
  30.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  31.         new Get(),
  32.         new Put(),
  33.         new Delete(),
  34.     ],
  35.     extraProperties: ['standard_put' => false],
  36. )]
  37. #[ApiFilter(IriFilter::class, properties: ['parent'])]
  38. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  39. #[ORM\Entity(repositoryClassLandingJournalRepository::class)]
  40. class LandingJournal implements OrdStatableInterface
  41. {
  42.     use IdTrait,
  43.         UuidTrait,
  44.         OrdStatTrait,
  45.         TimestampableTrait;
  46.     #[ApiProperty(description'Parent'readableLinkfalsewritableLinkfalse)]
  47.     #[Groups(['read''post'])]
  48.     #[Assert\NotNull]
  49.     #[ORM\ManyToOne(targetEntityLanding::class, inversedBy'journals')]
  50.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  51.     private Landing $parent;
  52.     #[ApiProperty(description'Journal'writableLinkfalse)]
  53.     #[Groups(['read''write'])]
  54.     #[Assert\NotNull]
  55.     #[Assert\Expression(
  56.         expression'this.isValid()',
  57.         message'Journal has been already assigned to this landing.'
  58.     )]
  59.     #[ORM\ManyToOne(targetEntityJournal::class)]
  60.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  61.     private Journal $journal;
  62.     public function __construct(Landing $parent)
  63.     {
  64.         $this->setUuid();
  65.         $this->parent $parent;
  66.         $this->createdAt = new \DateTimeImmutable();
  67.         $this->updatedAt = new \DateTimeImmutable();
  68.         $parent->addJournal($this);
  69.     }
  70.     public function getParent(): Landing
  71.     {
  72.         return $this->parent;
  73.     }
  74.     public function getJournal(): Journal
  75.     {
  76.         return $this->journal;
  77.     }
  78.     public function setJournal(Journal $journal): self
  79.     {
  80.         $this->journal $journal;
  81.         return $this;
  82.     }
  83.     public function isValid(): bool
  84.     {
  85.         foreach($this->parent->getJournals() as $relation) {
  86.             if ($relation === $this) {
  87.                 continue;
  88.             }
  89.             if ($relation->getJournal() === $this->getJournal()) {
  90.                 return false;
  91.             }
  92.         }
  93.         return true;
  94.     }
  95. }