Passed
Push — master ( 281f71...cbbb2e )
by Peter
03:08
created

ItemTrait::getPartClassesByOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
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\IBuilder;
10
use AbterPhp\Website\Domain\Entities\ContentList as Entity;
11
use AbterPhp\Website\Domain\Entities\ContentListItem as Item;
12
13
trait ItemTrait
14
{
15
    /** @var bool */
16
    protected $withName = true;
17
18
    /**
19
     * @return string
20
     */
21
    abstract public function getIdentifier(): string;
22
23
    /**
24
     * @return string[]
25
     */
26
    public function getPartClassesByOrder(): array
27
    {
28
        return [
29
            IBuilder::NAME  => 'item-name',
30
            IBuilder::BODY  => 'item-body',
31
            IBuilder::IMAGE => 'item-image',
32
        ];
33
    }
34
35
    /**
36
     * @return string[]
37
     */
38
    public function wrapperTags(): array
39
    {
40
        return [
41
            IBuilder::NAME  => Html5::TAG_SPAN,
42
            IBuilder::BODY  => Html5::TAG_SPAN,
43
            IBuilder::IMAGE => Html5::TAG_SPAN,
44
        ];
45
    }
46
47
    /**
48
     * @param Item   $item
49
     * @param Entity $list
50
     *
51
     * @return array
52
     */
53
    protected function buildParts(Item $item, Entity $list): array
54
    {
55
        $name  = $item->getName();
56
        $body  = $item->getBody();
57
        $image = null;
58
59
        if ($list->isWithImage()) {
60
            $image = StringHelper::createTag(
61
                Html5::TAG_IMG,
62
                [Html5::ATTR_SRC => $item->getImgSrc(), Html5::ATTR_ALT => $item->getImgAlt()]
63
            );
64
        }
65
66
        if ($list->isWithLinks()) {
67
            $name  = StringHelper::wrapInTag($name, Html5::TAG_A, [Html5::ATTR_HREF => $item->getNameHref()]);
68
            $body  = StringHelper::wrapInTag($body, Html5::TAG_A, [Html5::ATTR_HREF => $item->getBodyHref()]);
69
            if ($image) {
70
                $image = StringHelper::wrapInTag($image, Html5::TAG_A, [Html5::ATTR_HREF => $item->getImgHref()]);
71
            }
72
        }
73
74
        if (!$this->withName) {
75
            $name = null;
76
        }
77
78
        if (!$list->isWithBody()) {
79
            $body = null;
80
        }
81
82
        return [
83
            IBuilder::NAME  => $name,
84
            IBuilder::BODY  => $body,
85
            IBuilder::IMAGE => $image,
86
        ];
87
    }
88
89
    /**
90
     * @param string[] $parts
91
     *
92
     * @return string
93
     */
94
    protected function joinItem(array $parts): string
95
    {
96
        $tags = $this->wrapperTags();
97
98
        $ordered = [];
99
        foreach ($this->getPartClassesByOrder() as $partName => $class) {
100
            if (empty($parts[$partName])) {
101
                continue;
102
            }
103
104
            if (empty($tags[$partName])) {
105
                $ordered[] = $parts[$partName];
106
                continue;
107
            }
108
109
            $ordered[] = StringHelper::wrapInTag($parts[$partName], $tags[$partName], [Html5::ATTR_CLASS => $class]);
110
        }
111
112
        return implode('', $ordered);
113
    }
114
115
    /**
116
     * @param Entity $list
117
     * @param string $tag
118
     *
119
     * @return string
120
     */
121
    protected function joinItems(Entity $list, string $tag): string
122
    {
123
        if ($list->getItems() === null) {
124
            return '';
125
        }
126
127
        $htmlParts = [];
128
        foreach ($list->getItems() as $item) {
129
            $parts  = $this->buildParts($item, $list);
130
            $joined = $this->joinItem($parts);
131
            if ($tag) {
132
                $htmlParts[] = sprintf("<$tag>%s</$tag>\n", $joined);
133
            } else {
134
                $htmlParts[] = sprintf("%s\n", $joined);
135
            }
136
        }
137
138
        return implode('', $htmlParts);
139
    }
140
141
    /**
142
     * @param Entity $list
143
     * @param string $listTag
144
     * @param string $itemTag
145
     *
146
     * @return string
147
     */
148
    protected function buildItems(Entity $list, string $listTag, string $itemTag): string
149
    {
150
        $content = $this->joinItems($list, $itemTag);
151
152
        $classes = [$this->getIdentifier(), $list->getType()->getName()];
153
        if ($list->getClasses()) {
154
            $classes = array_merge($classes, explode(' ', $list->getClasses()));
155
        }
156
157
        $html = StringHelper::wrapInTag(
158
            $content,
159
            $listTag,
160
            [Html5::ATTR_ID => $list->getIdentifier(), Html5::ATTR_CLASS => implode(' ', $classes)]
161
        );
162
163
        return $html;
164
    }
165
}
166