1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Silverback\ApiComponentBundle\Entity\Component; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use Doctrine\Common\Collections\Collection; |
9
|
|
|
use Doctrine\ORM\Mapping as ORM; |
10
|
|
|
use Ramsey\Uuid\Uuid; |
11
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\Feature\AbstractFeatureItem; |
12
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent; |
13
|
|
|
use Silverback\ApiComponentBundle\Entity\SortableInterface; |
14
|
|
|
use Silverback\ApiComponentBundle\Entity\SortableTrait; |
15
|
|
|
use Silverback\ApiComponentBundle\Validator\Constraints as ACBAssert; |
16
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
17
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
18
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class ComponentLocation |
22
|
|
|
* @package Silverback\ApiComponentBundle\Entity\Component |
23
|
|
|
* @ACBAssert\ComponentLocation() |
24
|
|
|
* @ORM\Entity(repositoryClass="Silverback\ApiComponentBundle\Repository\ComponentLocationRepository") |
25
|
|
|
* @ORM\InheritanceType("SINGLE_TABLE") |
26
|
|
|
*/ |
27
|
|
|
class ComponentLocation implements SortableInterface |
28
|
|
|
{ |
29
|
|
|
use SortableTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @ORM\Id |
33
|
|
|
* @ORM\Column(type="string") |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $id; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @ORM\ManyToOne(targetEntity="Silverback\ApiComponentBundle\Entity\Content\AbstractContent", inversedBy="componentLocations") |
40
|
|
|
* @ORM\JoinColumn(onDelete="CASCADE") |
41
|
|
|
* @Groups({"component"}) |
42
|
|
|
* @var AbstractContent |
43
|
|
|
*/ |
44
|
|
|
private $content; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @ORM\ManyToOne(targetEntity="Silverback\ApiComponentBundle\Entity\Component\AbstractComponent", inversedBy="locations") |
48
|
|
|
* @ORM\JoinColumn(onDelete="CASCADE") |
49
|
|
|
* @Groups({"default"}) |
50
|
|
|
* @var AbstractComponent |
51
|
|
|
*/ |
52
|
|
|
private $component; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @ORM\Column(type="string", nullable=true) |
56
|
|
|
* @var null|string |
57
|
|
|
*/ |
58
|
|
|
protected $dynamicPageClass; |
59
|
|
|
|
60
|
1 |
|
public static function loadValidatorMetadata(ClassMetadata $metadata): void |
61
|
|
|
{ |
62
|
1 |
|
$metadata->addPropertyConstraint( |
63
|
1 |
|
'content', |
64
|
1 |
|
new Assert\NotBlank() |
65
|
|
|
); |
66
|
1 |
|
$metadata->addPropertyConstraint( |
67
|
1 |
|
'component', |
68
|
1 |
|
new Assert\NotBlank() |
69
|
|
|
); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* ComponentLocation constructor. |
74
|
|
|
* @param null|AbstractContent $newContent |
75
|
|
|
* @param null|AbstractComponent $newComponent |
76
|
|
|
*/ |
77
|
4 |
|
public function __construct( |
78
|
|
|
?AbstractContent $newContent = null, |
79
|
|
|
?AbstractComponent $newComponent = null |
80
|
|
|
) { |
81
|
4 |
|
$this->id = Uuid::uuid4()->getHex(); |
82
|
4 |
|
if ($newContent) { |
83
|
|
|
$this->setContent($newContent); |
84
|
|
|
} |
85
|
4 |
|
if ($newComponent) { |
86
|
|
|
$this->setComponent($newComponent); |
87
|
|
|
} |
88
|
4 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getId(): string |
94
|
|
|
{ |
95
|
|
|
return $this->id; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return AbstractContent|null |
100
|
|
|
*/ |
101
|
|
|
public function getContent(): ?AbstractContent |
102
|
|
|
{ |
103
|
|
|
return $this->content; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param AbstractContent $content |
108
|
|
|
* @param bool|null $sortLast |
109
|
|
|
*/ |
110
|
|
|
public function setContent(?AbstractContent $content, ?bool $sortLast = true): void |
111
|
|
|
{ |
112
|
|
|
if ($content !== $this->content) { |
113
|
|
|
$this->content = $content; |
114
|
|
|
if (null === $this->sort || $sortLast !== null) { |
115
|
|
|
$this->setSort($this->calculateSort($sortLast)); |
116
|
|
|
} |
117
|
|
|
if ($content) { |
118
|
|
|
$content->addComponentLocation($this); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return AbstractComponent |
125
|
|
|
*/ |
126
|
|
|
public function getComponent(): AbstractComponent |
127
|
|
|
{ |
128
|
|
|
return $this->component; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param AbstractComponent $component |
133
|
|
|
*/ |
134
|
|
|
public function setComponent(AbstractComponent $component): void |
135
|
|
|
{ |
136
|
|
|
$this->component = $component; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return Collection|AbstractFeatureItem[] |
141
|
|
|
*/ |
142
|
|
|
public function getSortCollection(): Collection |
143
|
|
|
{ |
144
|
|
|
return $this->content ? $this->content->getComponentLocations() : new ArrayCollection; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return null|string |
149
|
|
|
*/ |
150
|
|
|
public function getDynamicPageClass(): ?string |
151
|
|
|
{ |
152
|
|
|
return $this->dynamicPageClass; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param null|string $dynamicPageClass |
157
|
|
|
*/ |
158
|
|
|
public function setDynamicPageClass(?string $dynamicPageClass): void |
159
|
|
|
{ |
160
|
|
|
$this->dynamicPageClass = $dynamicPageClass; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|