Skip to content

Commit 8ed16a0

Browse files
committed
DocumentTrait
1 parent fe38e2a commit 8ed16a0

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Manuxi\SuluSharedToolsBundle\Entity\Traits;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use JMS\Serializer\Annotation as Serializer;
7+
use Sulu\Bundle\MediaBundle\Entity\MediaInterface;
8+
9+
trait DocumentTrait
10+
{
11+
12+
#[ORM\ManyToOne(targetEntity: MediaInterface::class)]
13+
#[ORM\JoinColumn(onDelete: 'SET NULL')]
14+
#[Serializer\Exclude]
15+
private ?MediaInterface $document = null;
16+
17+
public function getDocument(): ?MediaInterface
18+
{
19+
return $this->document;
20+
}
21+
22+
#[Serializer\VirtualProperty]
23+
#[Serializer\SerializedName("document")]
24+
public function getDocumentData(): ?array
25+
{
26+
if ($document = $this->getDocument()) {
27+
return [
28+
'id' => $document->getId(),
29+
];
30+
}
31+
32+
return null;
33+
34+
}
35+
36+
public function setDocument(?MediaInterface $document): self
37+
{
38+
$this->document = $document;
39+
return $this;
40+
}
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Manuxi\SuluSharedToolsBundle\Entity\Traits;
6+
7+
use JMS\Serializer\Annotation as Serializer;
8+
use Sulu\Bundle\MediaBundle\Entity\MediaInterface;
9+
10+
trait DocumentTranslatableTrait
11+
{
12+
abstract public function getLocale();
13+
abstract protected function getTranslation(string $locale);
14+
15+
#[Serializer\Exclude]
16+
public function getDocument(): ?MediaInterface
17+
{
18+
$translation = $this->getTranslation($this->getLocale());
19+
if (!$translation) {
20+
return null;
21+
}
22+
23+
return $translation->getDocument();
24+
}
25+
26+
#[Serializer\VirtualProperty]
27+
#[Serializer\SerializedName("document")]
28+
public function getDocumentData(): ?array
29+
{
30+
$translation = $this->getTranslation($this->getLocale());
31+
if (!$translation) {
32+
return null;
33+
}
34+
35+
return $translation->getDocumentData();
36+
}
37+
38+
public function setDocument(?MediaInterface $document): self
39+
{
40+
$translation = $this->getTranslation($this->getLocale());
41+
if (!$translation) {
42+
$translation = $this->createTranslation($this->getLocale());
43+
}
44+
45+
$translation->setDocument($document);
46+
return $this;
47+
}
48+
}

0 commit comments

Comments
 (0)