src/Entity/JournalArticleRelationship.php line 60

  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\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  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\OrderFilter;
  18. use App\Entity\Trait\IdTrait,
  19.     App\Entity\Trait\UuidTrait,
  20.     App\Entity\Trait\OrdTrait,
  21.     App\Entity\Trait\TimestampableTrait,
  22.     App\Entity\Trait\TranslatableTrait,
  23.     App\Entity\Trait\Languages\LanguageableChildTrait,
  24.     App\Entity\Interface\TranslatableInterface,
  25.     App\Entity\Interface\OrderableInterface,
  26.     App\Entity\Interface\LanguageableChildInterface;
  27. use App\Filter\IriFilter,
  28.     App\Util\ClassUtils,
  29.     App\Enum\RelationshipIdentifierType,
  30.     App\Repository\JournalArticleRelationshipRepository;
  31. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  32. #[ApiResource(
  33.     description'Journal article relationships',
  34.     normalizationContext: ['groups' => [
  35.         'read',
  36.         'read:' self::class,
  37.         'read:' self::class . 'Translation'
  38.     ]],
  39.     denormalizationContext: ['groups' => ['write']],
  40.     security'is_granted("' Journal::class . '")',
  41.     order: ['ord' => 'desc'],
  42.     operations: [
  43.         new GetCollection(),
  44.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  45.         new Get(),
  46.         new Put(),
  47.         new Delete(),
  48.     ],
  49.     extraProperties: ['standard_put' => false],
  50. )]
  51. #[ApiFilter(IriFilter::class, properties: ['parent'])]
  52. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  53. #[ORM\Entity(repositoryClassJournalArticleRelationshipRepository::class)]
  54. class JournalArticleRelationship implements TranslatableInterfaceOrderableInterfaceLanguageableChildInterface
  55. {
  56.     use IdTrait,
  57.         UuidTrait,
  58.         OrdTrait,
  59.         TimestampableTrait,
  60.         TranslatableTrait,
  61.         LanguageableChildTrait;
  62.     #[ApiProperty(description'Parent'readableLinkfalsewritableLinkfalse)]
  63.     #[Groups(['read''post'])]
  64.     #[Assert\NotNull]
  65.     #[ORM\ManyToOne(targetEntityJournalArticle::class, inversedBy'relationships')]
  66.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  67.     private JournalArticle $parent;
  68.     #[ApiProperty(description'Type'writableLinkfalse)]
  69.     #[Groups(['read:' self::class, 'write'])]
  70.     #[Assert\NotBlank]
  71.     #[ORM\ManyToOne(targetEntityReciprocalRelationshipType::class)]
  72.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  73.     private ReciprocalRelationshipType $type;
  74.     #[ApiProperty(description'Identifier')]
  75.     #[Groups(['read''write'])]
  76.     #[ORM\Column(typeTypes::STRINGlength255nullabletrue)]
  77.     private ?string $identifier null;
  78.     #[ApiProperty(description'Identifier type'writableLinkfalse)]
  79.     #[Groups(['read:' self::class, 'write'])]
  80.     #[ORM\Column(
  81.         typeTypes::STRING,
  82.         enumTypeRelationshipIdentifierType::class,
  83.         length255,
  84.         options: ['default' => RelationshipIdentifierType::DOI],
  85.     )]
  86.     private RelationshipIdentifierType $identifierType RelationshipIdentifierType::DOI;
  87.     #[ApiProperty(description'Target article'readableLinkfalsewritableLinkfalse)]
  88.     #[Groups(['read''post'])]
  89.     #[Assert\NotNull]
  90.     #[ORM\ManyToOne(targetEntityJournalArticle::class)]
  91.     #[ORM\JoinColumn(nullabletrueonDelete'cascade')]
  92.     private ?JournalArticle $targetArticle null;
  93.     public function __construct(JournalArticle $parent)
  94.     {
  95.         $this->setUuid();
  96.         $this->parent $parent;
  97.         $this->translations = new ArrayCollection();
  98.         foreach ($this->getParentLanguages() as $lang) {
  99.             new JournalArticleRelationshipTranslation($this$lang);
  100.         }
  101.         $this->createdAt = new \DateTimeImmutable();
  102.         $this->updatedAt = new \DateTimeImmutable();
  103.         $parent->addRelationship($this);
  104.     }
  105.     public function getParent(): JournalArticle
  106.     {
  107.         return $this->parent;
  108.     }
  109.     #[Groups(['read'])]
  110.     public function getTitle(): ?string
  111.     {
  112.         return $this->identifier;
  113.     }
  114.     public function getType(): ReciprocalRelationshipType
  115.     {
  116.         return $this->type;
  117.     }
  118.     public function setType(ReciprocalRelationshipType $type): self
  119.     {
  120.         $this->type $type;
  121.         return $this;
  122.     }
  123.     public function getIdentifier(): ?string
  124.     {
  125.         return $this->identifier;
  126.     }
  127.     public function setIdentifier(?string $identifier): self
  128.     {
  129.         $this->identifier $identifier;
  130.         return $this;
  131.     }
  132.     public function getIdentifierType(): RelationshipIdentifierType
  133.     {
  134.         return $this->identifierType;
  135.     }
  136.     public function setIdentifierType(RelationshipIdentifierType $identifierType): self
  137.     {
  138.         $this->identifierType $identifierType;
  139.         return $this;
  140.     }
  141.     public function getTargetArticle(): ?JournalArticle
  142.     {
  143.         return $this->targetArticle;
  144.     }
  145.     public function setTargetArticle(?JournalArticle $targetArticle): self
  146.     {
  147.         $this->targetArticle $targetArticle;
  148.         if ($targetArticle) {
  149.             $this->identifier $targetArticle->getDoi();
  150.         }
  151.         return $this;
  152.     }
  153.     #[Assert\Callback]
  154.     public function validate(ExecutionContextInterface $contextmixed $payload): void
  155.     {
  156.         if ($this->getTargetArticle() && $this->getIdentifierType() !== RelationshipIdentifierType::DOI) {
  157.             $context->buildViolation('The identifier type must be DOI when the target article is set.')
  158.                 ->atPath('identifierType')
  159.                 ->addViolation();
  160.         }
  161.     }
  162.     public function clone(JournalArticle $parent): self
  163.     {
  164.         $clone = clone $this;
  165.         $clone->id null;
  166.         $clone->setUuid();
  167.         $clone->parent $parent;
  168.         $clone->parent->addRelationship($clone);
  169.         ClassUtils::cloneCollection($this$clone'translations');
  170.         $clone->synchronizeTranslations();
  171.         $clone->createdAt = new \DateTimeImmutable();
  172.         $clone->updatedAt = new \DateTimeImmutable();
  173.         return $clone;
  174.     }
  175. }