src/Entity/LandingJournal.php line 46
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups,
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\OrderFilter;
use App\Entity\Trait\IdTrait,
App\Entity\Trait\UuidTrait,
App\Entity\Trait\OrdStatTrait,
App\Entity\Trait\TimestampableTrait,
App\Entity\Interface\OrdStatableInterface,
App\Repository\LandingJournalRepository;
use App\Filter\IriFilter;
#[ApiResource(
description: 'Landing journals',
normalizationContext: ['groups' => ['read', 'read:' . self::class]],
denormalizationContext: ['groups' => ['write']],
security: 'is_granted("' . Landing::class . '")',
order: ['ord' => 'desc'],
operations: [
new GetCollection(),
new Post(denormalizationContext: ['groups' => ['write', 'post']]),
new Get(),
new Put(),
new Delete(),
],
extraProperties: ['standard_put' => false],
)]
#[ApiFilter(IriFilter::class, properties: ['parent'])]
#[ApiFilter(OrderFilter::class, properties: ['ord'])]
#[ORM\Entity(repositoryClass: LandingJournalRepository::class)]
class LandingJournal implements OrdStatableInterface
{
use IdTrait,
UuidTrait,
OrdStatTrait,
TimestampableTrait;
#[ApiProperty(description: 'Parent', readableLink: false, writableLink: false)]
#[Groups(['read', 'post'])]
#[Assert\NotNull]
#[ORM\ManyToOne(targetEntity: Landing::class, inversedBy: 'journals')]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: false)]
private Landing $parent;
#[ApiProperty(description: 'Journal', writableLink: false)]
#[Groups(['read', 'write'])]
#[Assert\NotNull]
#[Assert\Expression(
expression: 'this.isValid()',
message: 'Journal has been already assigned to this landing.'
)]
#[ORM\ManyToOne(targetEntity: Journal::class)]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: false)]
private Journal $journal;
public function __construct(Landing $parent)
{
$this->setUuid();
$this->parent = $parent;
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
$parent->addJournal($this);
}
public function getParent(): Landing
{
return $this->parent;
}
public function getJournal(): Journal
{
return $this->journal;
}
public function setJournal(Journal $journal): self
{
$this->journal = $journal;
return $this;
}
public function isValid(): bool
{
foreach($this->parent->getJournals() as $relation) {
if ($relation === $this) {
continue;
}
if ($relation->getJournal() === $this->getJournal()) {
return false;
}
}
return true;
}
}