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, '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''write'])]
  77.     #[Assert\Expression(
  78.         expression'!value.getIsArchived()',
  79.         message'Cannot assign archived author.'
  80.     )]
  81.     #[ORM\ManyToOne(targetEntityAuthor::class)]
  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.             foreach($parent->getAuthors() as $author) {
  106.                 $author->setParticipation($singleParticipation);
  107.             }
  108.             $this->participation $singleParticipation;
  109.         } else {
  110.             $this->participation $participation;
  111.         }
  112.         $this->createdAt = new \DateTimeImmutable();
  113.         $this->updatedAt = new \DateTimeImmutable();
  114.         $parent->addAuthor($this);
  115.     }
  116.     public function getParent(): JournalArticle
  117.     {
  118.         return $this->parent;
  119.     }
  120.     public function getRole(): JournalArticleAuthorRole
  121.     {
  122.         return $this->role;
  123.     }
  124.     public function setRole(JournalArticleAuthorRole $role): self
  125.     {
  126.         $this->role $role;
  127.         return $this;
  128.     }
  129.     public function getAuthor(): Author
  130.     {
  131.         return $this->author;
  132.     }
  133.     public function setAuthor(Author $author): self
  134.     {
  135.         $this->author $author;
  136.         return $this;
  137.     }
  138.     public function getFullVisibleName(): ?string
  139.     {
  140.         $parts = [];
  141.         if (in_array(AuthorProperty::NAME$this->parent->getVisibleAuthorProperties())) {
  142.             $parts[] = $this->author->getName();
  143.         }
  144.         if (in_array(AuthorProperty::SURNAME$this->parent->getVisibleAuthorProperties())) {
  145.             $parts[] = $this->author->getSurname();
  146.         }
  147.         return implode(' '$parts);
  148.     }
  149.     public function getFullVisibleTranscriptionName(): ?string
  150.     {
  151.         $parts = [];
  152.         if (in_array(AuthorProperty::NAME$this->parent->getVisibleAuthorProperties())) {
  153.             $parts[] = $this->author->getTranscriptionName();
  154.         }
  155.         if (in_array(AuthorProperty::SURNAME$this->parent->getVisibleAuthorProperties())) {
  156.             $parts[] = $this->author->getTranscriptionSurname();
  157.         }
  158.         return implode(' '$parts);
  159.     }
  160.     public function getParticipation(): float
  161.     {
  162.         return round($this->participation2);
  163.     }
  164.     public function setParticipation(float $participation): self
  165.     {
  166.         $this->participation round($participation2);
  167.         return $this;
  168.     }
  169.     public function isParticipationValid(): bool
  170.     {
  171.         return 100 >= $this->sumParticipation();
  172.     }
  173.     private function sumParticipation(): float
  174.     {
  175.         $participation 0;
  176.         foreach($this->parent->getAuthors() as $author) {
  177.             $participation += $author->getParticipation();
  178.         }
  179.         return round($participation2);
  180.     }
  181.     public function getImportParticipation(): float
  182.     {
  183.         return $this->importParticipation;
  184.     }
  185.     public function setImportParticipation(float $importParticipation): void
  186.     {
  187.         $this->importParticipation $importParticipation;
  188.     }
  189. }