src/Entity/JournalArticleAuthor.php line 59

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\DBAL\Types\Types,
  5.     Doctrine\Common\Collections\ArrayCollection;
  6. use Symfony\Component\Serializer\Annotation\Groups,
  7.     Symfony\Component\Validator\Constraints as Assert;
  8. use ApiPlatform\Metadata\ApiResource,
  9.     ApiPlatform\Metadata\ApiProperty,
  10.     ApiPlatform\Metadata\Get,
  11.     ApiPlatform\Metadata\GetCollection,
  12.     ApiPlatform\Metadata\Post,
  13.     ApiPlatform\Metadata\Put,
  14.     ApiPlatform\Metadata\Delete,
  15.     ApiPlatform\Metadata\ApiFilter,
  16.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  17. use App\Entity\Trait\IdTrait,
  18.     App\Entity\Trait\UuidTrait,
  19.     App\Entity\Trait\OrdTrait,
  20.     App\Entity\Trait\TimestampableTrait,
  21.     App\Entity\Trait\TranslatableTrait,
  22.     App\Entity\Trait\Languages\LanguageableChildTrait,
  23.     App\Entity\Interface\TranslatableInterface,
  24.     App\Entity\Interface\OrderableInterface,
  25.     App\Entity\Interface\LanguageableChildInterface;
  26. use App\Filter\IriFilter,
  27.     App\Enum\JournalArticleAuthorRole,
  28.     App\Repository\JournalArticleAuthorRepository;
  29. use App\Enum\AuthorProperty;
  30. #[ApiResource(
  31.     description'Journal article authors',
  32.     normalizationContext: ['groups' => [
  33.         'read',
  34.         'read:' self::class,
  35.         'read:' self::class . 'Translation'
  36.     ]],
  37.     denormalizationContext: ['groups' => ['write']],
  38.     security'is_granted("' Journal::class . '")',
  39.     order: ['ord' => 'desc'],
  40.     operations: [
  41.         new GetCollection(),
  42.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  43.         new Get(),
  44.         new Put(),
  45.         new Delete(),
  46.     ],
  47.     extraProperties: ['standard_put' => false],
  48. )]
  49. #[ApiFilter(IriFilter::class, properties: ['parent'])]
  50. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  51. #[ORM\Entity(repositoryClassJournalArticleAuthorRepository::class)]
  52. class JournalArticleAuthor implements TranslatableInterfaceOrderableInterfaceLanguageableChildInterface
  53. {
  54.     use IdTrait,
  55.         UuidTrait,
  56.         OrdTrait,
  57.         TimestampableTrait,
  58.         TranslatableTrait,
  59.         LanguageableChildTrait;
  60.     #[ApiProperty(description'Parent'readableLinkfalsewritableLinkfalse)]
  61.     #[Groups(['read''post'])]
  62.     #[Assert\NotNull]
  63.     #[ORM\ManyToOne(targetEntityJournalArticle::class, inversedBy'authors')]
  64.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  65.     private JournalArticle $parent;
  66.     #[ApiProperty(description'Role'writableLinkfalse)]
  67.     #[Groups(['read:' self::class, 'read:minimal''write'])]
  68.     #[ORM\Column(
  69.         typeTypes::STRING,
  70.         enumTypeJournalArticleAuthorRole::class,
  71.         length255,
  72.         options: ['default' => JournalArticleAuthorRole::AUTHOR]
  73.     )]
  74.     private JournalArticleAuthorRole $role JournalArticleAuthorRole::AUTHOR;
  75.     #[ApiProperty(description'Author'writableLinkfalse)]
  76.     #[Groups(['read''read:minimal''write'])]
  77.     #[Assert\Expression(
  78.         expression'!value.getIsArchived()',
  79.         message'Cannot assign archived author.'
  80.     )]
  81.     #[ORM\ManyToOne(targetEntityAuthor::class, inversedBy'articleRelations')]
  82.     #[ORM\JoinColumn(nullablefalse)]
  83.     private Author $author;
  84.     #[ApiProperty(description'Participation in the entire article')]
  85.     #[Groups(['read:' self::class, 'write'])]
  86.     #[Assert\Expression(
  87.         expression'!this || this.isParticipationValid()',
  88.         message'Entire participation exceeds 100%.'
  89.     )]
  90.     #[ORM\Column(typeTypes::FLOAT)]
  91.     private float $participation 0.0;
  92.     #[ORM\Column(typeTypes::FLOAT)]
  93.     private float $importParticipation 0.0;
  94.     public function __construct(JournalArticle $parentfloat $participation null)
  95.     {
  96.         $this->setUuid();
  97.         $this->parent $parent;
  98.         $this->translations = new ArrayCollection();
  99.         foreach ($this->getParentLanguages() as $lang) {
  100.             new JournalArticleAuthorTranslation($this$lang);
  101.         }
  102.         if (! $participation) {
  103.             $currentAuthorsCount $parent->getAuthors()->count() + 1;
  104.             $singleParticipation round(100  $currentAuthorsCount2);
  105.             if ($singleParticipation $currentAuthorsCount 100) {
  106.                 $singleParticipation $singleParticipation 0.01;
  107.             }
  108.             foreach($parent->getAuthors() as $author) {
  109.                 $author->setParticipation($singleParticipation);
  110.             }
  111.             $this->participation $singleParticipation;
  112.         } else {
  113.             $this->participation $participation;
  114.         }
  115.         $this->createdAt = new \DateTimeImmutable();
  116.         $this->updatedAt = new \DateTimeImmutable();
  117.         $parent->addAuthor($this);
  118.     }
  119.     public function getParent(): JournalArticle
  120.     {
  121.         return $this->parent;
  122.     }
  123.     public function getRole(): JournalArticleAuthorRole
  124.     {
  125.         return $this->role;
  126.     }
  127.     public function setRole(JournalArticleAuthorRole $role): self
  128.     {
  129.         $this->role $role;
  130.         return $this;
  131.     }
  132.     public function getAuthor(): Author
  133.     {
  134.         return $this->author;
  135.     }
  136.     public function setAuthor(Author $author): self
  137.     {
  138.         $this->author $author;
  139.         return $this;
  140.     }
  141.     public function getFullVisibleName(): ?string
  142.     {
  143.         $parts = [];
  144.         foreach ($this->parent->getVisibleAuthorProperties() as $property) {
  145.             $parts[] = match ($property) {
  146.                 AuthorProperty::PREFIX => $this->author->getTitle(),
  147.                 AuthorProperty::NAME => $this->author->getName(),
  148.                 AuthorProperty::SURNAME => $this->author->getSurname(),
  149.                 default => null
  150.             };
  151.         }
  152.         return implode(' '$parts);
  153.     }
  154.     public function getFullVisibleTranscriptionName(): ?string
  155.     {
  156.         $parts = [];
  157.         foreach ($this->parent->getVisibleAuthorProperties() as $property) {
  158.             $parts[] = match ($property) {
  159.                 AuthorProperty::PREFIX => $this->author->getTitle(),
  160.                 AuthorProperty::NAME => $this->author->getTranscriptionName(),
  161.                 AuthorProperty::SURNAME => $this->author->getTranscriptionSurname(),
  162.                 default => null
  163.             };
  164.         }
  165.         return implode(' '$parts);
  166.     }
  167.     public function getParticipation(): float
  168.     {
  169.         return round($this->participation2);
  170.     }
  171.     public function setParticipation(float $participation): self
  172.     {
  173.         $this->participation round($participation2);
  174.         return $this;
  175.     }
  176.     public function isParticipationValid(): bool
  177.     {
  178.         return 100 >= $this->sumParticipation();
  179.     }
  180.     private function sumParticipation(): float
  181.     {
  182.         $participation 0;
  183.         foreach($this->parent->getAuthors() as $author) {
  184.             $participation += $author->getParticipation();
  185.         }
  186.         return round($participation2);
  187.     }
  188.     public function getImportParticipation(): float
  189.     {
  190.         return $this->importParticipation;
  191.     }
  192.     public function setImportParticipation(float $importParticipation): void
  193.     {
  194.         $this->importParticipation $importParticipation;
  195.     }
  196.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
  197.     {
  198.         $this->updatedAt $updatedAt;
  199.         $this->getParent()->setUpdatedAt($updatedAt);
  200.         return $this;
  201.     }
  202. }