1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Entity\Content\Component; |
4
|
|
|
|
5
|
|
|
use ApiPlatform\Core\Annotation\ApiProperty; |
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
use Doctrine\Common\Collections\Collection; |
8
|
|
|
use Doctrine\ORM\Mapping as ORM; |
9
|
|
|
use Ramsey\Uuid\Uuid; |
10
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent; |
11
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\ComponentGroup; |
12
|
|
|
use Silverback\ApiComponentBundle\Entity\DeleteCascadeInterface; |
13
|
|
|
use Silverback\ApiComponentBundle\Entity\ValidComponentTrait; |
14
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AbstractComponent |
18
|
|
|
* @package Silverback\ApiComponentBundle\Entity\Content\Component |
19
|
|
|
* @author Daniel West <[email protected]> |
20
|
|
|
* @ORM\Entity() |
21
|
|
|
* @ORM\Table(name="component") |
22
|
|
|
* @ORM\InheritanceType("SINGLE_TABLE") |
23
|
|
|
* @ORM\DiscriminatorColumn(name="type", type="string") |
24
|
|
|
* @ORM\DiscriminatorMap({ |
25
|
|
|
* "content" = "Silverback\ApiComponentBundle\Entity\Content\Component\Content\Content", |
26
|
|
|
* "form" = "Silverback\ApiComponentBundle\Entity\Content\Component\Form\Form", |
27
|
|
|
* "gallery" = "Silverback\ApiComponentBundle\Entity\Content\Component\Gallery\Gallery", |
28
|
|
|
* "gallery_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Gallery\GalleryItem", |
29
|
|
|
* "hero" = "Silverback\ApiComponentBundle\Entity\Content\Component\Hero\Hero", |
30
|
|
|
* "feature_columns" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\Columns\FeatureColumns", |
31
|
|
|
* "feature_columns_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\Columns\FeatureColumnsItem", |
32
|
|
|
* "feature_stacked" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\Stacked\FeatureStacked", |
33
|
|
|
* "feature_stacked_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\Stacked\FeatureStackedItem", |
34
|
|
|
* "feature_text_list" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\TextList\FeatureTextList", |
35
|
|
|
* "feature_text_list_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\TextList\FeatureTextListItem", |
36
|
|
|
* "nav_bar" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\NavBar\NavBar", |
37
|
|
|
* "nav_bar_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\NavBar\NavBarItem", |
38
|
|
|
* "tabs" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\Tabs\Tabs", |
39
|
|
|
* "tabs_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\Tabs\TabsItem", |
40
|
|
|
* "menu" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\Menu\Menu", |
41
|
|
|
* "menu_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\Menu\MenuItem", |
42
|
|
|
* "collection" = "Silverback\ApiComponentBundle\Entity\Content\Component\Collection\Collection", |
43
|
|
|
* "layout_side_column" = "Silverback\ApiComponentBundle\Entity\Content\Component\Layout\SideColumn", |
44
|
|
|
* "simple_image" = "Silverback\ApiComponentBundle\Entity\Content\Component\Image\SimpleImage" |
45
|
|
|
* }) |
46
|
|
|
* @ORM\EntityListeners({"Silverback\ApiComponentBundle\EntityListener\ComponentListener"}) |
47
|
|
|
*/ |
48
|
|
|
abstract class AbstractComponent implements ComponentInterface, DeleteCascadeInterface |
49
|
|
|
{ |
50
|
|
|
use ValidComponentTrait; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @ORM\Id() |
54
|
|
|
* @ORM\Column(type="string") |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $id; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @ORM\Column(nullable=true) |
61
|
|
|
* @Groups({"component", "content"}) |
62
|
|
|
* @var null|string |
63
|
|
|
*/ |
64
|
|
|
private $className; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @ORM\OneToMany(targetEntity="Silverback\ApiComponentBundle\Entity\Content\Component\ComponentLocation", mappedBy="component") |
68
|
|
|
* @var Collection|ComponentLocation[] |
69
|
|
|
*/ |
70
|
|
|
protected $locations; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @ORM\OneToMany(targetEntity="Silverback\ApiComponentBundle\Entity\Content\ComponentGroup", mappedBy="parent", cascade={"persist"}) |
74
|
|
|
* @ORM\OrderBy({"sort"="ASC"}) |
75
|
|
|
* @ApiProperty(attributes={"fetchEager": false}) |
76
|
|
|
* @Groups({"layout", "route", "content"}) |
77
|
|
|
* @var Collection|ComponentGroup[] |
78
|
|
|
*/ |
79
|
|
|
protected $componentGroups; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @ORM\Column(nullable=true) |
83
|
|
|
* @Groups({"route", "content", "component"}) |
84
|
|
|
* @var string|null |
85
|
|
|
*/ |
86
|
|
|
protected $componentName; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* AbstractComponent constructor. |
90
|
|
|
*/ |
91
|
41 |
|
public function __construct() |
92
|
|
|
{ |
93
|
41 |
|
$this->id = Uuid::uuid4()->getHex(); |
94
|
41 |
|
$this->locations = new ArrayCollection; |
95
|
41 |
|
$this->componentGroups = new ArrayCollection; |
96
|
41 |
|
$this->validComponents = new ArrayCollection; |
97
|
41 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
4 |
|
public function getId(): string |
103
|
|
|
{ |
104
|
4 |
|
return $this->id; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return null|string |
109
|
|
|
*/ |
110
|
5 |
|
public function getClassName(): ?string |
111
|
|
|
{ |
112
|
5 |
|
return $this->className; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param null|string $className |
117
|
|
|
* @return AbstractComponent |
118
|
|
|
*/ |
119
|
2 |
|
public function setClassName(?string $className): AbstractComponent |
120
|
|
|
{ |
121
|
2 |
|
$this->className = $className; |
122
|
2 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return Collection|ComponentLocation[] |
127
|
|
|
*/ |
128
|
|
|
public function getLocations() |
129
|
|
|
{ |
130
|
|
|
return $this->locations; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param ComponentLocation $content |
135
|
|
|
* @return AbstractComponent |
136
|
|
|
*/ |
137
|
|
|
public function addLocation(ComponentLocation $content): AbstractComponent |
138
|
|
|
{ |
139
|
|
|
$this->locations->add($content); |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param ComponentLocation $content |
145
|
|
|
* @return AbstractComponent |
146
|
|
|
*/ |
147
|
|
|
public function removeLocation(ComponentLocation $content): AbstractComponent |
148
|
|
|
{ |
149
|
|
|
$this->locations->removeElement($content); |
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param array $componentGroups |
155
|
|
|
* @return AbstractComponent |
156
|
|
|
*/ |
157
|
|
|
public function setComponentGroups(array $componentGroups): AbstractComponent |
158
|
|
|
{ |
159
|
|
|
$this->componentGroups = new ArrayCollection; |
160
|
|
|
foreach ($componentGroups as $componentGroup) { |
161
|
|
|
$this->addComponentGroup($componentGroup); |
162
|
|
|
} |
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param ComponentGroup $componentGroup |
168
|
|
|
* @return AbstractComponent |
169
|
|
|
*/ |
170
|
12 |
|
public function addComponentGroup(ComponentGroup $componentGroup): AbstractComponent |
171
|
|
|
{ |
172
|
12 |
|
$componentGroup->setParent($this); |
173
|
12 |
|
$this->componentGroups->add($componentGroup); |
174
|
12 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param ComponentGroup $componentGroup |
179
|
|
|
* @return AbstractComponent |
180
|
|
|
*/ |
181
|
|
|
public function removeComponentGroup(ComponentGroup $componentGroup): AbstractComponent |
182
|
|
|
{ |
183
|
|
|
$this->componentGroups->removeElement($componentGroup); |
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return Collection|ComponentGroup[] |
189
|
|
|
*/ |
190
|
13 |
|
public function getComponentGroups(): Collection |
191
|
|
|
{ |
192
|
13 |
|
return $this->componentGroups; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param null|string $componentName |
197
|
|
|
*/ |
198
|
|
|
public function setComponentName(?string $componentName): void |
199
|
|
|
{ |
200
|
|
|
$this->componentName = $componentName; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Return the component name for front-end to decipher |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
4 |
|
public function getComponentName(): string |
208
|
|
|
{ |
209
|
4 |
|
if ($this->componentName) { |
210
|
|
|
return $this->componentName; |
211
|
|
|
} |
212
|
4 |
|
$explodedClass = explode('\\', static::class); |
213
|
4 |
|
return array_pop($explodedClass); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
|
|
public function onDeleteCascade(): bool |
220
|
|
|
{ |
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @Groups({"component_write"}) |
226
|
|
|
* @param AbstractComponent $parent |
227
|
|
|
* @param int $componentGroupOffset |
228
|
|
|
* @return AbstractComponent |
229
|
|
|
* @throws \InvalidArgumentException |
230
|
|
|
*/ |
231
|
1 |
|
public function setParentComponent(AbstractComponent $parent, int $componentGroupOffset = 0): AbstractComponent |
232
|
|
|
{ |
233
|
1 |
|
if (!\in_array($parent, $this->getParentComponents(), true)) { |
234
|
1 |
|
$componentGroup = $this->getComponentComponentGroup($parent, $componentGroupOffset); |
235
|
1 |
|
if (!$componentGroup->hasComponent($this)) { |
236
|
1 |
|
$componentGroup->addComponentLocation(new ComponentLocation($componentGroup, $this)); |
237
|
|
|
} |
238
|
|
|
} |
239
|
1 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @Groups({"component_write"}) |
244
|
|
|
* @param ComponentGroup $componentGroup |
245
|
|
|
* @return AbstractComponent |
246
|
|
|
*/ |
247
|
|
|
public function setParentComponentGroup(ComponentGroup $componentGroup): AbstractComponent |
248
|
|
|
{ |
249
|
|
|
if (!$componentGroup->hasComponent($this)) { |
250
|
|
|
$componentGroup->addComponentLocation(new ComponentLocation($componentGroup, $this)); |
251
|
|
|
} |
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param AbstractContent $content |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
|
|
public function hasParentContent(AbstractContent $content): bool |
260
|
|
|
{ |
261
|
|
|
foreach ($this->locations as $location) { |
262
|
1 |
|
if ($location->getContent() === $content) { |
263
|
|
|
return true; |
264
|
|
|
} |
265
|
1 |
|
} |
266
|
1 |
|
return false; |
267
|
|
|
} |
268
|
|
|
|
269
|
1 |
|
/** |
270
|
|
|
* @param AbstractComponent $component |
271
|
|
|
* @param int $componentGroupOffset |
272
|
|
|
* @return ComponentGroup |
273
|
|
|
* @throws \InvalidArgumentException |
274
|
|
|
*/ |
275
|
1 |
|
private function getComponentComponentGroup(AbstractComponent $component, int $componentGroupOffset = 0): ComponentGroup |
276
|
|
|
{ |
277
|
1 |
|
/** @var ComponentGroup $componentGroup */ |
278
|
1 |
|
$componentGroup = $component->getComponentGroups()->get($componentGroupOffset); |
279
|
1 |
|
if (null === $componentGroup) { |
280
|
1 |
|
throw new \InvalidArgumentException(sprintf('There is no component group child of this component with the offset %d', $componentGroupOffset)); |
281
|
1 |
|
} |
282
|
|
|
return $componentGroup; |
283
|
|
|
} |
284
|
|
|
|
285
|
1 |
|
/** |
286
|
1 |
|
* @return AbstractComponent[] |
287
|
|
|
*/ |
288
|
|
|
private function getParentComponents(): array |
289
|
|
|
{ |
290
|
|
|
$parentContent = $this->getParentContent(); |
291
|
|
|
return array_unique( |
292
|
|
|
array_filter( |
293
|
|
|
array_map( |
294
|
|
|
function ($content) { |
295
|
1 |
|
if ($content instanceof ComponentGroup) { |
296
|
|
|
return $content->getParent(); |
297
|
1 |
|
} |
298
|
1 |
|
}, |
299
|
1 |
|
$parentContent |
300
|
1 |
|
) |
301
|
|
|
) |
302
|
1 |
|
); |
303
|
1 |
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @return AbstractContent[] |
307
|
|
|
*/ |
308
|
|
|
private function getParentContent(): array |
309
|
|
|
{ |
310
|
|
|
return array_unique( |
311
|
|
|
array_filter( |
312
|
|
|
array_map( |
313
|
|
|
function (ComponentLocation $loc) { |
314
|
|
|
return $loc->getContent(); |
315
|
|
|
}, |
316
|
|
|
$this->locations->toArray() |
317
|
|
|
) |
318
|
|
|
) |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|