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