src/Entity/JournalArticleAuthor.php line 59
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM,
Doctrine\DBAL\Types\Types,
Doctrine\Common\Collections\ArrayCollection;
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\Trait\TranslatableTrait,
App\Entity\Trait\Languages\LanguageableChildTrait,
App\Entity\Interface\TranslatableInterface,
App\Entity\Interface\OrderableInterface,
App\Entity\Interface\LanguageableChildInterface;
use App\Filter\IriFilter,
App\Enum\JournalArticleAuthorRole,
App\Repository\JournalArticleAuthorRepository;
use App\Enum\AuthorProperty;
#[ApiResource(
description: 'Journal article authors',
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'])]
#[ORM\Entity(repositoryClass: JournalArticleAuthorRepository::class)]
class JournalArticleAuthor implements TranslatableInterface, OrderableInterface, LanguageableChildInterface
{
use IdTrait,
UuidTrait,
OrdTrait,
TimestampableTrait,
TranslatableTrait,
LanguageableChildTrait;
#[ApiProperty(description: 'Parent', readableLink: false, writableLink: false)]
#[Groups(['read', 'post'])]
#[Assert\NotNull]
#[ORM\ManyToOne(targetEntity: JournalArticle::class, inversedBy: 'authors')]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: false)]
private JournalArticle $parent;
#[ApiProperty(description: 'Role', writableLink: false)]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\Column(
type: Types::STRING,
enumType: JournalArticleAuthorRole::class,
length: 255,
options: ['default' => JournalArticleAuthorRole::AUTHOR]
)]
private JournalArticleAuthorRole $role = JournalArticleAuthorRole::AUTHOR;
#[ApiProperty(description: 'Author', writableLink: false)]
#[Groups(['read', 'write'])]
#[Assert\Expression(
expression: '!value.getIsArchived()',
message: 'Cannot assign archived author.'
)]
#[ORM\ManyToOne(targetEntity: Author::class)]
#[ORM\JoinColumn(nullable: false)]
private Author $author;
#[ApiProperty(description: 'Participation in the entire article')]
#[Groups(['read:' . self::class, 'write'])]
#[Assert\Expression(
expression: '!this || this.isParticipationValid()',
message: 'Entire participation exceeds 100%.'
)]
#[ORM\Column(type: Types::FLOAT)]
private float $participation = 0.0;
#[ORM\Column(type: Types::FLOAT)]
private float $importParticipation = 0.0;
public function __construct(JournalArticle $parent, float $participation = null)
{
$this->setUuid();
$this->parent = $parent;
$this->translations = new ArrayCollection();
foreach ($this->getParentLanguages() as $lang) {
new JournalArticleAuthorTranslation($this, $lang);
}
if (! $participation) {
$currentAuthorsCount = $parent->getAuthors()->count() + 1;
$singleParticipation = round(100 / $currentAuthorsCount, 2);
foreach($parent->getAuthors() as $author) {
$author->setParticipation($singleParticipation);
}
$this->participation = $singleParticipation;
} else {
$this->participation = $participation;
}
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
$parent->addAuthor($this);
}
public function getParent(): JournalArticle
{
return $this->parent;
}
public function getRole(): JournalArticleAuthorRole
{
return $this->role;
}
public function setRole(JournalArticleAuthorRole $role): self
{
$this->role = $role;
return $this;
}
public function getAuthor(): Author
{
return $this->author;
}
public function setAuthor(Author $author): self
{
$this->author = $author;
return $this;
}
public function getFullVisibleName(): ?string
{
$parts = [];
if (in_array(AuthorProperty::NAME, $this->parent->getVisibleAuthorProperties())) {
$parts[] = $this->author->getName();
}
if (in_array(AuthorProperty::SURNAME, $this->parent->getVisibleAuthorProperties())) {
$parts[] = $this->author->getSurname();
}
return implode(' ', $parts);
}
public function getFullVisibleTranscriptionName(): ?string
{
$parts = [];
if (in_array(AuthorProperty::NAME, $this->parent->getVisibleAuthorProperties())) {
$parts[] = $this->author->getTranscriptionName();
}
if (in_array(AuthorProperty::SURNAME, $this->parent->getVisibleAuthorProperties())) {
$parts[] = $this->author->getTranscriptionSurname();
}
return implode(' ', $parts);
}
public function getParticipation(): float
{
return round($this->participation, 2);
}
public function setParticipation(float $participation): self
{
$this->participation = round($participation, 2);
return $this;
}
public function isParticipationValid(): bool
{
return 100 >= $this->sumParticipation();
}
private function sumParticipation(): float
{
$participation = 0;
foreach($this->parent->getAuthors() as $author) {
$participation += $author->getParticipation();
}
return round($participation, 2);
}
public function getImportParticipation(): float
{
return $this->importParticipation;
}
public function setImportParticipation(float $importParticipation): void
{
$this->importParticipation = $importParticipation;
}
}