src/Entity/JournalArticleClassification.php line 53
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM,
Doctrine\DBAL\Types\Types;
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\OrdTrait,
App\Entity\Trait\TimestampableTrait,
App\Entity\Interface\OrderableInterface;
use App\Filter\IriFilter,
App\Attribute\Sanitizeable,
App\Repository\JournalArticleClassificationRepository;
#[ApiResource(
description: 'Journal article classifications',
normalizationContext: ['groups' => [
'read',
'read:' . self::class,
'read:' . self::class . 'Translation'
]],
denormalizationContext: ['groups' => ['write']],
security: 'is_granted("' . Journal::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'])]
#[Assert\GroupSequence(['JournalArticleClassification', 'additional'])]
#[ORM\Entity(repositoryClass: JournalArticleClassificationRepository::class)]
class JournalArticleClassification implements OrderableInterface
{
use IdTrait,
UuidTrait,
OrdTrait,
TimestampableTrait;
#[ApiProperty(description: 'Parent', readableLink: false, writableLink: false)]
#[Groups(['read', 'post'])]
#[ORM\ManyToOne(targetEntity: JournalArticle::class, inversedBy: 'classifications')]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: false)]
private JournalArticle $parent;
#[ApiProperty(description: 'Type', writableLink: false)]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\ManyToOne(targetEntity: ClassificationType::class)]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: false)]
private ClassificationType $type;
#[ApiProperty(description: 'Entry', writableLink: false)]
#[Groups(['read:' . self::class, 'write'])]
#[Assert\Expression(
expression: 'value.getRootType() === this.getType()',
message: 'Entry must be of selected type.',
groups: ['additional']
)]
#[ORM\ManyToOne(targetEntity: ClassificationTypeEntry::class)]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: false)]
private ClassificationTypeEntry $entry;
#[ApiProperty(description: 'Text')]
#[Groups(['read', 'write'])]
#[Sanitizeable]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $text = null;
public function __construct(JournalArticle $parent)
{
$this->setUuid();
$this->parent = $parent;
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
$parent->addClassification($this);
}
public function getParent(): JournalArticle
{
return $this->parent;
}
public function getType(): ClassificationType
{
return $this->type;
}
public function setType(ClassificationType $type): self
{
$this->type = $type;
return $this;
}
public function getEntry(): ClassificationTypeEntry
{
return $this->entry;
}
public function setEntry(ClassificationTypeEntry $entry): self
{
$this->entry = $entry;
return $this;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(?string $text): self
{
$this->text = $text;
return $this;
}
public function isValid(): bool
{
return $this->entry->getRootType() === $this->type;
}
public function clone(JournalArticle $parent): self
{
$clone = clone $this;
$clone->id = null;
$clone->setUuid();
$clone->parent = $parent;
$clone->parent->addClassification($clone);
$clone->createdAt = new \DateTimeImmutable();
$clone->updatedAt = new \DateTimeImmutable();
return $clone;
}
}