src/Entity/JournalArticleRelationship.php line 58

  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\Util\ClassUtils,
  28.     App\Enum\RelationshipIdentifierType,
  29.     App\Repository\JournalArticleRelationshipRepository;
  30. #[ApiResource(
  31.     description'Journal article relationships',
  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(repositoryClassJournalArticleRelationshipRepository::class)]
  52. class JournalArticleRelationship 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'relationships')]
  64.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  65.     private JournalArticle $parent;
  66.     #[ApiProperty(description'Type'writableLinkfalse)]
  67.     #[Groups(['read:' self::class, 'write'])]
  68.     #[ORM\ManyToOne(targetEntityReciprocalRelationshipType::class)]
  69.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  70.     private ReciprocalRelationshipType $type;
  71.     #[ApiProperty(description'Identifier')]
  72.     #[Groups(['read''write'])]
  73.     #[Assert\NotBlank]
  74.     #[ORM\Column(typeTypes::STRINGlength255)]
  75.     private string $identifier;
  76.     #[ApiProperty(description'Identifier type')]
  77.     #[Groups(['read:' self::class, 'write'])]
  78.     #[ORM\Column(
  79.         typeTypes::STRING,
  80.         enumTypeRelationshipIdentifierType::class,
  81.         length255,
  82.         options: ['default' => RelationshipIdentifierType::DOI],
  83.         uniquetrue
  84.     )]
  85.     private RelationshipIdentifierType $identifierType RelationshipIdentifierType::DOI;
  86.     public function __construct(JournalArticle $parent)
  87.     {
  88.         $this->setUuid();
  89.         $this->parent $parent;
  90.         $this->translations = new ArrayCollection();
  91.         foreach ($this->getParentLanguages() as $lang) {
  92.             new JournalArticleRelationshipTranslation($this$lang);
  93.         }
  94.         $this->createdAt = new \DateTimeImmutable();
  95.         $this->updatedAt = new \DateTimeImmutable();
  96.         $parent->addRelationship($this);
  97.     }
  98.     public function getParent(): JournalArticle
  99.     {
  100.         return $this->parent;
  101.     }
  102.     #[Groups(['read'])]
  103.     public function getTitle(): ?string
  104.     {
  105.         return $this->identifier;
  106.     }
  107.     public function getType(): ReciprocalRelationshipType
  108.     {
  109.         return $this->type;
  110.     }
  111.     public function setType(ReciprocalRelationshipType $type): self
  112.     {
  113.         $this->type $type;
  114.         return $this;
  115.     }
  116.     public function getIdentifier(): ?string
  117.     {
  118.         return $this->identifier;
  119.     }
  120.     public function setIdentifier(string $identifier): self
  121.     {
  122.         $this->identifier $identifier;
  123.         return $this;
  124.     }
  125.     public function getIdentifierType(): RelationshipIdentifierType
  126.     {
  127.         return $this->identifierType;
  128.     }
  129.     public function setIdentifierType(RelationshipIdentifierType $identifierType): self
  130.     {
  131.         $this->identifierType $identifierType;
  132.         return $this;
  133.     }
  134.     public function clone(JournalArticle $parent): self
  135.     {
  136.         $clone = clone $this;
  137.         $clone->id null;
  138.         $clone->setUuid();
  139.         $clone->parent $parent;
  140.         $clone->parent->addRelationship($clone);
  141.         ClassUtils::cloneCollection($this$clone'translations');
  142.         $clone->synchronizeTranslations();
  143.         $clone->createdAt = new \DateTimeImmutable();
  144.         $clone->updatedAt = new \DateTimeImmutable();
  145.         return $clone;
  146.     }
  147. }