src/Entity/Affiliation.php line 81

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\DBAL\Types\Types,
  5.     Doctrine\Common\Collections\Collection,
  6.     Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Serializer\Annotation\Groups,
  8.     Symfony\Component\Validator\Constraints as Assert;
  9. use ApiPlatform\Metadata\ApiResource,
  10.     ApiPlatform\Metadata\ApiProperty,
  11.     ApiPlatform\Metadata\Get,
  12.     ApiPlatform\Metadata\GetCollection,
  13.     ApiPlatform\Metadata\Post,
  14.     ApiPlatform\Metadata\Put,
  15.     ApiPlatform\Metadata\Delete,
  16.     ApiPlatform\Metadata\ApiFilter,
  17.     ApiPlatform\Doctrine\Orm\Filter\SearchFilter,
  18.     ApiPlatform\Doctrine\Orm\Filter\ExistsFilter,
  19.     ApiPlatform\Doctrine\Orm\Filter\BooleanFilter,
  20.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  21. use App\Entity\Trait\IdTrait,
  22.     App\Entity\Trait\UuidTrait,
  23.     App\Entity\Trait\OrdStatTrait,
  24.     App\Entity\Trait\TimestampableTrait,
  25.     App\Entity\Trait\TranslatableTrait,
  26.     App\Entity\Trait\Languages\SiteLanguagesTrait,
  27.     App\Entity\Trait\DepthTrait,
  28.     App\Entity\Trait\ImportIdTrait,
  29.     App\Entity\Interface\TranslatableInterface,
  30.     App\Entity\Interface\OrdStatableInterface,
  31.     App\Entity\Interface\ParentableInterface,
  32.     App\Entity\Interface\LanguageableInterface,
  33.     App\Repository\AffiliationRepository,
  34.     App\Enum\Country,
  35.     App\Enum\Language,
  36.     App\Filter\IriFilter;
  37. use App\Attribute\DenormalizationPriority,
  38.     App\Doctrine\DBAL\Types\LanguagesType;
  39. use App\Validator\Constraints as CustomAssert;
  40. #[ApiResource(
  41.     description'Affiliations',
  42.     normalizationContext: ['groups' => [
  43.         'read',
  44.         'read:' self::class,
  45.         'read:' self::class . 'Translation',
  46.         'single:read:' self::class ,
  47.     ]],
  48.     denormalizationContext: ['groups' => ['write']],
  49.     security'is_granted("' self::class . '")',
  50.     order: ['ord' => 'desc'],
  51.     operations: [
  52.         new GetCollection(normalizationContext: ['groups' => [
  53.             'read',
  54.             'read:' self::class,
  55.             'read:' self::class . 'Translation'
  56.         ]]),
  57.         new Post(denormalizationContext: ['groups' => ['write''post']]),
  58.         new Get(),
  59.         new Put(),
  60.         new Delete(),
  61.     ],
  62.     extraProperties: ['standard_put' => false],
  63. )]
  64. #[ApiFilter(SearchFilter::class, properties: [
  65.     'translations.title' => 'partial',
  66.     'nativeTitle' => 'partial',
  67.     'ror' => 'exact'
  68. ])]
  69. #[ApiFilter(ExistsFilter::class, properties: ['parent''country'])]
  70. #[ApiFilter(IriFilter::class, properties: ['parent''country'])]
  71. #[ApiFilter(BooleanFilter::class, properties: ['stat'])]
  72. #[ApiFilter(OrderFilter::class, properties: ['nativeTitle''ord'])]
  73. #[ORM\Entity(repositoryClassAffiliationRepository::class)]
  74. class Affiliation implements TranslatableInterfaceOrdStatableInterfaceParentableInterfaceLanguageableInterface
  75. {
  76.     use IdTrait,
  77.         UuidTrait,
  78.         OrdStatTrait,
  79.         TimestampableTrait,
  80.         TranslatableTrait,
  81.         SiteLanguagesTrait,
  82.         DepthTrait,
  83.         ImportIdTrait;
  84.     #[ApiProperty(description'Parent affiliation'readableLinkfalsewritableLinkfalse)]
  85.     #[Groups(['read:' self::class, 'write'])]
  86.     #[CustomAssert\Depth(max3)]
  87.     #[ORM\ManyToOne(targetEntityself::class, inversedBy'children')]
  88.     #[ORM\JoinColumn(onDelete'cascade')]
  89.     private ?self $parent null;
  90.     #[ApiProperty(description'Root affiliation'readableLinktrue)]
  91.     #[Groups(['read:' self::class])]
  92.     #[ORM\ManyToOne(targetEntityself::class)]
  93.     #[ORM\JoinColumn(onDelete'cascade'nullabletrue)]
  94.     private ?self $root;
  95.     #[ApiProperty(description'Address')]
  96.     #[Groups(['single:read:' self::class, 'write'])]
  97.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  98.     private ?string $address null;
  99.     #[ApiProperty(description'Website')]
  100.     #[Groups(['read:' self::class, 'write'])]
  101.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  102.     private ?string $website null;
  103.     #[ApiProperty(description'E-mail')]
  104.     #[Groups(['read:' self::class, 'write'])]
  105.     #[Assert\Email]
  106.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  107.     private ?string $email null;
  108.     #[ApiProperty(description'ROR')]
  109.     #[Groups(['read:' self::class, 'write:depth:0'])]
  110.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  111.     private ?string $ror null;
  112.     #[ApiProperty(description'GRID')]
  113.     #[Groups(['read:' self::class, 'write:depth:0'])]
  114.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  115.     private ?string $grid null;
  116.     #[ApiProperty(description'ISNI')]
  117.     #[Groups(['read:' self::class, 'write:depth:0'])]
  118.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  119.     private ?string $isni null;
  120.     #[ApiProperty(description'Country'writableLinkfalse)]
  121.     #[Groups(['read:' self::class, 'write:depth:0'])]
  122.     #[ORM\Column(typeTypes::STRINGenumTypeCountry::class, length255nullabletrue)]
  123.     private ?Country $country null;
  124.     #[ApiProperty(description'Type of affiliation'writableLinkfalse)]
  125.     #[Groups(['read:' self::class, 'write:depth:0'])]
  126.     #[ORM\ManyToOne(targetEntityAffiliationType::class, inversedBy'affiliations')]
  127.     #[ORM\JoinColumn(onDelete'set null')]
  128.     private ?AffiliationType $type null;
  129.     #[ApiProperty(description'Children affiliations'writableLinkfalse)]
  130.     #[Groups(['read:' self::class])]
  131.     #[ORM\OneToMany(targetEntityself::class, mappedBy'parent')]
  132.     #[ORM\OrderBy(['ord' => 'desc'])]
  133.     private Collection $children;
  134.     #[ApiProperty(description'Languages'writableLinkfalse)]
  135.     #[Groups(['single:read:' self::class, 'write'])]
  136.     #[Assert\Choice(callback: [Language::class, 'cases'], multipletrue)]
  137.     #[Assert\Unique]
  138.     #[Assert\Count(min2)]
  139.     #[CustomAssert\DefaultLanguagesRequired(requiredLanguage::DEFAULT_SITE)]
  140.     #[DenormalizationPriority(DenormalizationPriority::TOP)]
  141.     #[ORM\Column(typeLanguagesType::NAME)]
  142.     protected array $languages = [];
  143.     public function __construct(?Affiliation $parent, ?array $languages)
  144.     {
  145.         $this->setUuid();
  146.         $this->parent $parent;
  147.         $this->root $this->getRootFrom($this);
  148.         $this->translations = new ArrayCollection();
  149.         $this->children = new ArrayCollection();
  150.         $this->setLanguages($languages ?? Language::DEFAULT_SITE);
  151.         $this->createdAt = new \DateTimeImmutable();
  152.         $this->updatedAt = new \DateTimeImmutable();
  153.         $parent?->addChild($this);
  154.     }
  155.     public function getParent(): ?self
  156.     {
  157.         return $this->parent;
  158.     }
  159.     public function setParent(?Affiliation $parent): self
  160.     {
  161.         $this->parent $parent;
  162.         $this->root $this->getRootFrom($this);
  163.         return $this;
  164.     }
  165.     public function getRoot(): self
  166.     {
  167.         return $this->root;
  168.     }
  169.     public function getRootFrom(Affiliation $current): Affiliation
  170.     {
  171.         if (! $current->getParent()) {
  172.             return $current;
  173.         }
  174.         return $this->getRootFrom($current->getParent());
  175.     }
  176.     public function getAddress(): ?string
  177.     {
  178.         return $this->address;
  179.     }
  180.     public function setAddress(?string $address): self
  181.     {
  182.         $this->address $address;
  183.         return $this;
  184.     }
  185.     public function getWebsite(): ?string
  186.     {
  187.         return $this->website;
  188.     }
  189.     public function setWebsite(?string $website): self
  190.     {
  191.         $this->website $website;
  192.         return $this;
  193.     }
  194.     public function getEmail(): ?string
  195.     {
  196.         return $this->email;
  197.     }
  198.     public function setEmail(?string $email): self
  199.     {
  200.         $this->email $email;
  201.         return $this;
  202.     }
  203.     public function getRor(): ?string
  204.     {
  205.         return $this->ror;
  206.     }
  207.     public function setRor(?string $ror): self
  208.     {
  209.         $this->ror $ror;
  210.         return $this;
  211.     }
  212.     public function getGrid(): ?string
  213.     {
  214.         return $this->grid;
  215.     }
  216.     public function setGrid(?string $grid): self
  217.     {
  218.         $this->grid $grid;
  219.         return $this;
  220.     }
  221.     public function getIsni(): ?string
  222.     {
  223.         return $this->isni;
  224.     }
  225.     public function setIsni(?string $isni): self
  226.     {
  227.         $this->isni $isni;
  228.         return $this;
  229.     }
  230.     public function getCountry(): ?Country
  231.     {
  232.         return $this->country;
  233.     }
  234.     public function setCountry(?Country $country): self
  235.     {
  236.         $this->country $country;
  237.         return $this;
  238.     }
  239.     public function getType(): ?AffiliationType
  240.     {
  241.         return $this->type;
  242.     }
  243.     public function setType(?AffiliationType $type): self
  244.     {
  245.         $this->type $type;
  246.         return $this;
  247.     }
  248.     /**
  249.      * @return Collection|Affiliation[]
  250.      */
  251.     public function getChildren(): Collection
  252.     {
  253.         return $this->children;
  254.     }
  255.     public function addChild(self $child): self
  256.     {
  257.         if ($this->children->contains($child)) {
  258.             return $this;
  259.         }
  260.         $this->children[] = $child;
  261.         return $this;
  262.     }
  263.     public function removeChild(self $child): self
  264.     {
  265.         $this->children->removeElement($child);
  266.         return $this;
  267.     }
  268.     public function getClosestTree(int $reach 4): array
  269.     {
  270.         if ($reach 1) {
  271.             return [$this];
  272.         }
  273.         if (! $this->getParent()) {
  274.             return [$this];
  275.         }
  276.         return [$this, ...$this->getParent()->getClosestTree($reach 1)];
  277.     }
  278. }