src/Entity/Affiliation.php line 81
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM,
Doctrine\DBAL\Types\Types,
Doctrine\Common\Collections\Collection,
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\SearchFilter,
ApiPlatform\Doctrine\Orm\Filter\ExistsFilter,
ApiPlatform\Doctrine\Orm\Filter\BooleanFilter,
ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use App\Entity\Trait\IdTrait,
App\Entity\Trait\UuidTrait,
App\Entity\Trait\OrdStatTrait,
App\Entity\Trait\TimestampableTrait,
App\Entity\Trait\TranslatableTrait,
App\Entity\Trait\Languages\SiteLanguagesTrait,
App\Entity\Trait\DepthTrait,
App\Entity\Trait\ImportIdTrait,
App\Entity\Interface\TranslatableInterface,
App\Entity\Interface\OrdStatableInterface,
App\Entity\Interface\ParentableInterface,
App\Entity\Interface\LanguageableInterface,
App\Repository\AffiliationRepository,
App\Enum\Country,
App\Enum\Language,
App\Filter\IriFilter;
use App\Attribute\DenormalizationPriority,
App\Doctrine\DBAL\Types\LanguagesType;
use App\Validator\Constraints as CustomAssert;
#[ApiResource(
description: 'Affiliations',
normalizationContext: ['groups' => [
'read',
'read:' . self::class,
'read:' . self::class . 'Translation',
'single:read:' . self::class ,
]],
denormalizationContext: ['groups' => ['write']],
security: 'is_granted("' . self::class . '")',
order: ['ord' => 'desc'],
operations: [
new GetCollection(normalizationContext: ['groups' => [
'read',
'read:' . self::class,
'read:' . self::class . 'Translation'
]]),
new Post(denormalizationContext: ['groups' => ['write', 'post']]),
new Get(),
new Put(),
new Delete(),
],
extraProperties: ['standard_put' => false],
)]
#[ApiFilter(SearchFilter::class, properties: [
'translations.title' => 'partial',
'nativeTitle' => 'partial',
'ror' => 'exact'
])]
#[ApiFilter(ExistsFilter::class, properties: ['parent', 'country'])]
#[ApiFilter(IriFilter::class, properties: ['parent', 'country'])]
#[ApiFilter(BooleanFilter::class, properties: ['stat'])]
#[ApiFilter(OrderFilter::class, properties: ['nativeTitle', 'ord'])]
#[ORM\Entity(repositoryClass: AffiliationRepository::class)]
class Affiliation implements TranslatableInterface, OrdStatableInterface, ParentableInterface, LanguageableInterface
{
use IdTrait,
UuidTrait,
OrdStatTrait,
TimestampableTrait,
TranslatableTrait,
SiteLanguagesTrait,
DepthTrait,
ImportIdTrait;
#[ApiProperty(description: 'Parent affiliation', readableLink: false, writableLink: false)]
#[Groups(['read:' . self::class, 'write'])]
#[CustomAssert\Depth(max: 3)]
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
#[ORM\JoinColumn(onDelete: 'cascade')]
private ?self $parent = null;
#[ApiProperty(description: 'Root affiliation', readableLink: true)]
#[Groups(['read:' . self::class])]
#[ORM\ManyToOne(targetEntity: self::class)]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: true)]
private ?self $root;
#[ApiProperty(description: 'Address')]
#[Groups(['single:read:' . self::class, 'write'])]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $address = null;
#[ApiProperty(description: 'Website')]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\Column(type: Types::STRING, nullable: true)]
private ?string $website = null;
#[ApiProperty(description: 'E-mail')]
#[Groups(['read:' . self::class, 'write'])]
#[Assert\Email]
#[ORM\Column(type: Types::STRING, nullable: true)]
private ?string $email = null;
#[ApiProperty(description: 'ROR')]
#[Groups(['read:' . self::class, 'write:depth:0'])]
#[ORM\Column(type: Types::STRING, nullable: true)]
private ?string $ror = null;
#[ApiProperty(description: 'GRID')]
#[Groups(['read:' . self::class, 'write:depth:0'])]
#[ORM\Column(type: Types::STRING, nullable: true)]
private ?string $grid = null;
#[ApiProperty(description: 'ISNI')]
#[Groups(['read:' . self::class, 'write:depth:0'])]
#[ORM\Column(type: Types::STRING, nullable: true)]
private ?string $isni = null;
#[ApiProperty(description: 'Country', writableLink: false)]
#[Groups(['read:' . self::class, 'write:depth:0'])]
#[ORM\Column(type: Types::STRING, enumType: Country::class, length: 255, nullable: true)]
private ?Country $country = null;
#[ApiProperty(description: 'Type of affiliation', writableLink: false)]
#[Groups(['read:' . self::class, 'write:depth:0'])]
#[ORM\ManyToOne(targetEntity: AffiliationType::class, inversedBy: 'affiliations')]
#[ORM\JoinColumn(onDelete: 'set null')]
private ?AffiliationType $type = null;
#[ApiProperty(description: 'Children affiliations', writableLink: false)]
#[Groups(['read:' . self::class])]
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')]
#[ORM\OrderBy(['ord' => 'desc'])]
private Collection $children;
#[ApiProperty(description: 'Languages', writableLink: false)]
#[Groups(['single:read:' . self::class, 'write'])]
#[Assert\Choice(callback: [Language::class, 'cases'], multiple: true)]
#[Assert\Unique]
#[Assert\Count(min: 2)]
#[CustomAssert\DefaultLanguagesRequired(required: Language::DEFAULT_SITE)]
#[DenormalizationPriority(DenormalizationPriority::TOP)]
#[ORM\Column(type: LanguagesType::NAME)]
protected array $languages = [];
public function __construct(?Affiliation $parent, ?array $languages)
{
$this->setUuid();
$this->parent = $parent;
$this->root = $this->getRootFrom($this);
$this->translations = new ArrayCollection();
$this->children = new ArrayCollection();
$this->setLanguages($languages ?? Language::DEFAULT_SITE);
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
$parent?->addChild($this);
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?Affiliation $parent): self
{
$this->parent = $parent;
$this->root = $this->getRootFrom($this);
return $this;
}
public function getRoot(): self
{
return $this->root;
}
public function getRootFrom(Affiliation $current): Affiliation
{
if (! $current->getParent()) {
return $current;
}
return $this->getRootFrom($current->getParent());
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(?string $address): self
{
$this->address = $address;
return $this;
}
public function getWebsite(): ?string
{
return $this->website;
}
public function setWebsite(?string $website): self
{
$this->website = $website;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getRor(): ?string
{
return $this->ror;
}
public function setRor(?string $ror): self
{
$this->ror = $ror;
return $this;
}
public function getGrid(): ?string
{
return $this->grid;
}
public function setGrid(?string $grid): self
{
$this->grid = $grid;
return $this;
}
public function getIsni(): ?string
{
return $this->isni;
}
public function setIsni(?string $isni): self
{
$this->isni = $isni;
return $this;
}
public function getCountry(): ?Country
{
return $this->country;
}
public function setCountry(?Country $country): self
{
$this->country = $country;
return $this;
}
public function getType(): ?AffiliationType
{
return $this->type;
}
public function setType(?AffiliationType $type): self
{
$this->type = $type;
return $this;
}
/**
* @return Collection|Affiliation[]
*/
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(self $child): self
{
if ($this->children->contains($child)) {
return $this;
}
$this->children[] = $child;
return $this;
}
public function removeChild(self $child): self
{
$this->children->removeElement($child);
return $this;
}
public function getClosestTree(int $reach = 4): array
{
if ($reach < 1) {
return [$this];
}
if (! $this->getParent()) {
return [$this];
}
return [$this, ...$this->getParent()->getClosestTree($reach - 1)];
}
}