src/Entity/Author.php line 87
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource,
ApiPlatform\Metadata\Get,
ApiPlatform\Metadata\GetCollection,
ApiPlatform\Metadata\Post,
ApiPlatform\Metadata\Put,
ApiPlatform\Metadata\Delete;
use App\Filter\OrderByUnmapped;
use App\Repository\AuthorRepository,
App\Security\Voter\ArchivableVoter,
App\Lib\Actions;
use App\StateProvider\StatsAuthorProvider;
use App\Controller\Stats\StatsAuthorXlsxGenerator;
#[ApiResource(
description: 'Authors',
normalizationContext: ['groups' => [
'read',
'read:' . self::class,
'read:' . self::class . 'Translation',
'read:' . AbstractAuthor::class,
'read:' . AbstractAuthor::class . 'Translation'
]],
denormalizationContext: ['groups' => ['write']],
security: 'is_granted("' . self::class . '")',
order: ['ord' => 'desc'],
operations: [
new GetCollection(),
new GetCollection(
name: 'get_author_stats',
uriTemplate: '/authors/stats',
provider: StatsAuthorProvider::class,
security: 'is_granted("ROLE_MODULE_STATS")',
normalizationContext: ['groups' => ['read:stats']],
paginationMaximumItemsPerPage: 500
),
new GetCollection(
name: 'get_author_stats_xlsx',
uriTemplate: '/authors/stats/xlsx',
provider: StatsAuthorProvider::class,
controller: StatsAuthorXlsxGenerator::class,
security: 'is_granted("ROLE_MODULE_STATS")',
),
new Post(),
new Get(),
new Put(),
new Delete(
securityPostDenormalize: 'is_granted("' . Actions::DELETE . '", object)',
securityPostDenormalizeMessage: ArchivableVoter::MESSAGE
),
],
extraProperties: ['standard_put' => false],
)]
#[ApiFilter(SearchFilter::class, properties: [
'name' => 'ipartial',
'surname' => 'ipartial',
'transcriptionName' => 'ipartial',
'transcriptionSurname' => 'ipartial',
'affiliations.country' => 'ipartial',
'affiliations.nativeTitle' => 'ipartial',
'uuid' => 'exact',
])]
#[ApiFilter(OrderFilter::class, properties: [
'name',
'surname',
'transcriptionName',
'transcriptionSurname',
'affiliations.country',
'affiliations.nativeTitle',
])]
#[ApiFilter(OrderByUnmapped::class, properties: ['statsAmountOfViews', 'statsAmountOfDownloads'])]
#[ORM\Entity(repositoryClass: AuthorRepository::class)]
class Author extends AbstractAuthor
{
#[ApiProperty(description: 'Statistics: journals')]
#[Groups(['read:stats'])]
private ?string $statsJournals = null;
#[ApiProperty(description: 'Statistics: amount of views')]
#[Groups(['read:stats'])]
private int $statsAmountOfViews = 0;
#[ApiProperty(description: 'Statistics: amount of downloads')]
#[Groups(['read:stats'])]
private int $statsAmountOfDownloads = 0;
#[ApiProperty(description: 'Article relations')]
#[ORM\OneToMany(targetEntity: JournalArticleAuthor::class, mappedBy: 'author')]
private Collection $articleRelations;
public function __construct(string $name, string $surname, ?array $languages)
{
parent::__construct($name, $surname, $languages);
$this->articleRelations = new ArrayCollection();
}
public function getType(): string
{
return 'author';
}
public function getStatsJournals(): ?string
{
return $this->statsJournals;
}
public function setStatsJournals(?string $statsJournals): self
{
$this->statsJournals = $statsJournals;
return $this;
}
public function getStatsAmountOfViews(): int
{
return $this->statsAmountOfViews;
}
public function setStatsAmountOfViews(int $statsAmountOfViews): self
{
$this->statsAmountOfViews = $statsAmountOfViews;
return $this;
}
public function getStatsAmountOfDownloads(): int
{
return $this->statsAmountOfDownloads;
}
public function setStatsAmountOfDownloads(int $statsAmountOfDownloads): self
{
$this->statsAmountOfDownloads = $statsAmountOfDownloads;
return $this;
}
public function getArticleRelations(): Collection
{
return $this->articleRelations;
}
#[Groups(['read:stats'])]
public function getStatsCountry(): ?string
{
return false !== ($first = $this->affiliations->first())
? $first->getCountry()?->value
: null;
}
#[Groups(['read:stats'])]
public function getStatsAffiliations(): ?string
{
$affiliations = [];
/** @var Affiliation */
foreach($this->affiliations as $entry) {
if (isset($affiliations[$entry->getUuid()->toString()])) {
continue;
}
$affiliations[$entry->getUuid()->toString()] =
$entry->readNativeLanguageTranslation('title');
}
sort($affiliations);
return implode(', ', $affiliations);
}
}