1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Template\Builder\ContentList; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Constant\Html5; |
8
|
|
|
use AbterPhp\Framework\Html\Helper\StringHelper; |
9
|
|
|
use AbterPhp\Framework\Template\Data; |
10
|
|
|
use AbterPhp\Framework\Template\IBuilder; |
11
|
|
|
use AbterPhp\Framework\Template\IData; |
12
|
|
|
use AbterPhp\Framework\Template\ParsedTemplate; |
13
|
|
|
use AbterPhp\Website\Domain\Entities\ContentList as Entity; |
14
|
|
|
use AbterPhp\Website\Domain\Entities\ContentListItem as Item; |
15
|
|
|
|
16
|
|
|
trait ItemTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
abstract public function getIdentifier(): string; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
25
|
|
|
* |
26
|
|
|
* @param Entity $list |
27
|
|
|
* @param ParsedTemplate|null $template |
28
|
|
|
* |
29
|
|
|
* @return IData |
30
|
|
|
*/ |
31
|
|
|
public function build($list, ?ParsedTemplate $template = null): IData |
32
|
|
|
{ |
33
|
|
|
$wrapperTags = $this->getWrapperTags($template); |
34
|
|
|
$wrapperClasses = $this->getWrapperClasses($template); |
35
|
|
|
$options = $this->getOptions($template); |
36
|
|
|
|
37
|
|
|
$content = $this->getContent($list, $wrapperTags, $wrapperClasses, $options); |
38
|
|
|
$tag = $wrapperTags[IBuilder::LIST_TAG]; |
39
|
|
|
$classes = $this->getListClasses($list->getClasses(), $wrapperClasses[IBuilder::LIST_CLASS]); |
40
|
|
|
$attributes = [Html5::ATTR_ID => $list->getIdentifier(), Html5::ATTR_CLASS => $classes]; |
41
|
|
|
|
42
|
|
|
$html = StringHelper::wrapInTag($content, $tag, $attributes); |
43
|
|
|
|
44
|
|
|
return new Data( |
45
|
|
|
$list->getIdentifier(), |
46
|
|
|
[], |
47
|
|
|
['body' => $html] |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ParsedTemplate|null $template |
53
|
|
|
* |
54
|
|
|
* @return <string,string> |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
abstract protected function getWrapperTags(?ParsedTemplate $template = null): array; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ParsedTemplate|null $template |
61
|
|
|
* |
62
|
|
|
* @return <string,string> |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
abstract protected function getWrapperClasses(?ParsedTemplate $template = null): array; |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param ParsedTemplate|null $template |
69
|
|
|
* |
70
|
|
|
* @return <string,string> |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
abstract protected function getOptions(?ParsedTemplate $template = null): array; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
76
|
|
|
* |
77
|
|
|
* @param Entity $list |
78
|
|
|
* @param <string,string> $tags |
|
|
|
|
79
|
|
|
* @param <string,string> $classes |
80
|
|
|
* @param <string,string> $options |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
protected function getContent(Entity $list, array $tags, array $classes, array $options): string |
85
|
|
|
{ |
86
|
|
|
if ($list->getItems() === null) { |
87
|
|
|
return ''; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$htmlParts = []; |
91
|
|
|
foreach ($list->getItems() as $item) { |
92
|
|
|
$parts = $this->buildItemParts($item, $list, $tags, $classes, $options); |
93
|
|
|
$content = $this->joinItemParts($parts); |
94
|
|
|
|
95
|
|
|
$tag = $tags[IBuilder::ITEM_TAG]; |
96
|
|
|
$class = $classes[IBuilder::ITEM_CLASS]; |
97
|
|
|
|
98
|
|
|
$htmlParts[] = StringHelper::wrapInTag($content, $tag, [Html5::ATTR_CLASS => $class]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return implode(PHP_EOL, $htmlParts); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Item $item |
106
|
|
|
* @param Entity $list |
107
|
|
|
* @param <string,string> $tags |
|
|
|
|
108
|
|
|
* @param <string,string> $classes |
109
|
|
|
* @param <string,string> $options |
110
|
|
|
* |
111
|
|
|
* @return <string,string> |
|
|
|
|
112
|
|
|
*/ |
113
|
|
|
protected function buildItemParts(Item $item, Entity $list, array $tags, array $classes, array $options): array |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
|
|
IBuilder::LABEL => $this->buildLabel($item, $list, $tags, $classes, $options), |
117
|
|
|
IBuilder::CONTENT => $this->buildContent($item, $list, $tags, $classes, $options), |
118
|
|
|
IBuilder::IMAGE => $this->buildImage($item, $list, $tags, $classes, $options), |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Item $item |
124
|
|
|
* @param Entity $list |
125
|
|
|
* @param <string,string> $tags |
|
|
|
|
126
|
|
|
* @param <string,string> $classes |
127
|
|
|
* @param <string,string> $options |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
protected function buildLabel(Item $item, Entity $list, array $tags, array $classes, array $options): string |
132
|
|
|
{ |
133
|
|
|
if (!$options[IBuilder::WITH_LABEL_OPTION]) { |
134
|
|
|
return ''; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$label = $item->getLabel(); |
138
|
|
|
|
139
|
|
|
if ($list->isWithLinks()) { |
140
|
|
|
$href = $item->getContentHref(); |
141
|
|
|
if ($list->isWithLabelLinks()) { |
142
|
|
|
$href = $item->getLabelHref() ?: $item->getContentHref(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$label = StringHelper::wrapInTag($label, Html5::TAG_A, [Html5::ATTR_HREF => $href]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$tag = $tags[IBuilder::LABEL_TAG]; |
149
|
|
|
$class = $classes[IBuilder::LABEL_CLASS]; |
150
|
|
|
$label = StringHelper::wrapInTag($label, $tag, [Html5::ATTR_CLASS => $class]); |
151
|
|
|
|
152
|
|
|
return $label; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
157
|
|
|
* |
158
|
|
|
* @param Item $item |
159
|
|
|
* @param Entity $list |
160
|
|
|
* @param <string,string> $tags |
|
|
|
|
161
|
|
|
* @param <string,string> $classes |
162
|
|
|
* @param <string,string> $options |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
protected function buildContent(Item $item, Entity $list, array $tags, array $classes, array $options): string |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$content = $item->getContent(); |
169
|
|
|
|
170
|
|
|
if ($list->isWithLinks()) { |
171
|
|
|
$content = StringHelper::wrapInTag($content, Html5::TAG_A, [Html5::ATTR_HREF => $item->getContentHref()]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$tag = $tags[IBuilder::CONTENT_TAG]; |
175
|
|
|
$class = $classes[IBuilder::CONTENT_CLASS]; |
176
|
|
|
$content = StringHelper::wrapInTag($content, $tag, [Html5::ATTR_CLASS => $class]); |
177
|
|
|
|
178
|
|
|
return $content; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param Item $item |
183
|
|
|
* @param Entity $list |
184
|
|
|
* @param <string,string> $tags |
|
|
|
|
185
|
|
|
* @param <string,string> $classes |
186
|
|
|
* @param <string,string> $options |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
protected function buildImage(Item $item, Entity $list, array $tags, array $classes, array $options): string |
191
|
|
|
{ |
192
|
|
|
if (!$options[IBuilder::WITH_IMAGE_OPTION] || !$list->isWithImages()) { |
193
|
|
|
return ''; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$attr = [Html5::ATTR_SRC => $item->getImgSrc(), Html5::ATTR_ALT => $item->getImgAlt()]; |
197
|
|
|
$image = StringHelper::createTag(Html5::TAG_IMG, $attr); |
198
|
|
|
|
199
|
|
|
if ($list->isWithLinks()) { |
200
|
|
|
$href = $item->getImgHref() ?: $item->getContentHref(); |
201
|
|
|
$image = StringHelper::wrapInTag($image, Html5::TAG_A, [Html5::ATTR_HREF => $href]); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$tag = $tags[IBuilder::IMAGE_TAG]; |
205
|
|
|
$class = $classes[IBuilder::IMAGE_CLASS]; |
206
|
|
|
$image = StringHelper::wrapInTag($image, $tag, [Html5::ATTR_CLASS => $class]); |
207
|
|
|
|
208
|
|
|
return $image; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param <string,string> $parts |
|
|
|
|
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
protected function joinItemParts(array $parts): string |
217
|
|
|
{ |
218
|
|
|
$content = ''; |
219
|
|
|
foreach ($parts as $part) { |
220
|
|
|
if (!$part) { |
221
|
|
|
continue; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$content .= $part; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $content; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $classes |
232
|
|
|
* @param string $attributeClasses |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
protected function getListClasses(string $classes, string $attributeClasses): string |
237
|
|
|
{ |
238
|
|
|
$trimmedClasses = [$this->getIdentifier() => $this->getIdentifier()]; |
239
|
|
|
|
240
|
|
|
foreach (explode(' ', $classes) as $class) { |
241
|
|
|
$trimmedClass = trim($class); |
242
|
|
|
if ($trimmedClass) { |
243
|
|
|
$trimmedClasses[$trimmedClass] = $trimmedClass; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
foreach (explode(' ', $attributeClasses) as $class) { |
247
|
|
|
$trimmedClass = trim($class); |
248
|
|
|
if ($trimmedClass) { |
249
|
|
|
$trimmedClasses[$trimmedClass] = $trimmedClass; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return implode(' ', $trimmedClasses); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|