|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Template\Builder\ContentList; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Website\Domain\Entities\ContentList as Entity; |
|
8
|
|
|
use AbterPhp\Website\Domain\Entities\ContentListItem as Item; |
|
9
|
|
|
|
|
10
|
|
|
trait ItemTrait |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param Item $item |
|
14
|
|
|
* @param Entity $list |
|
15
|
|
|
* |
|
16
|
|
|
* @return array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function buildParts(Item $item, Entity $list): array |
|
19
|
|
|
{ |
|
20
|
|
|
$name = $item->getName(); |
|
21
|
|
|
$body = $item->getBody(); |
|
22
|
|
|
$image = sprintf('<img src="%s" alt="%s">', $item->getImgSrc(), $item->getImgAlt()); |
|
23
|
|
|
|
|
24
|
|
|
if ($list->isWithLinks()) { |
|
25
|
|
|
$name = sprintf('<a href="%s">%s</a>', $item->getNameHref(), $name); |
|
26
|
|
|
$body = sprintf('<a href="%s">%s</a>', $item->getBodyHref(), $body); |
|
27
|
|
|
$image = sprintf('<a href="%s">%s</a>', $item->getImgHref(), $image); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if (!$list->isWithBody()) { |
|
31
|
|
|
$body = null; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (!$list->isWithImage()) { |
|
35
|
|
|
$image = null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return [$name, $body, $image]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $parts |
|
43
|
|
|
* @param int[] $order |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function joinItem(array $parts, array $order): string |
|
48
|
|
|
{ |
|
49
|
|
|
$ordered = []; |
|
50
|
|
|
foreach ($order as $i) { |
|
51
|
|
|
if (empty($parts[$i])) { |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$ordered[] = $parts[$i]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return implode('', $ordered); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param Entity $list |
|
63
|
|
|
* @param string $tag |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function joinItems(Entity $list, string $tag): string |
|
68
|
|
|
{ |
|
69
|
|
|
if ($list->getItems() === null) { |
|
70
|
|
|
return ''; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$htmlParts = []; |
|
74
|
|
|
foreach ($list->getItems() as $item) { |
|
75
|
|
|
$parts = $this->buildParts($item, $list); |
|
76
|
|
|
$joined = $this->joinItem($parts, [0, 1, 2]); |
|
77
|
|
|
$htmlParts[] = sprintf("<$tag>%s</$tag>\n", $joined); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return implode('', $htmlParts); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param Entity $list |
|
85
|
|
|
* @param string $listTag |
|
86
|
|
|
* @param string $itemTag |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function buildItems(Entity $list, string $listTag, string $itemTag): string |
|
91
|
|
|
{ |
|
92
|
|
|
$content = $this->joinItems($list, $itemTag); |
|
93
|
|
|
|
|
94
|
|
|
$classes = array_merge( |
|
95
|
|
|
[static::IDENTIFIER, $list->getType()->getName()], |
|
|
|
|
|
|
96
|
|
|
explode(' ', $list->getClasses()) |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
$html = sprintf( |
|
100
|
|
|
'<%s id="%s" class="%s">%s</%s>' . PHP_EOL, |
|
101
|
|
|
$listTag, |
|
102
|
|
|
$list->getIdentifier(), |
|
103
|
|
|
implode(' ', $classes), |
|
104
|
|
|
$content, |
|
105
|
|
|
$listTag |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
return $html; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|