Item::addLabel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Form\Factory\ContentList;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Form\Container\CheckboxGroup;
9
use AbterPhp\Framework\Form\Container\FormGroup;
10
use AbterPhp\Framework\Form\Element\Input;
11
use AbterPhp\Framework\Form\Element\Textarea;
12
use AbterPhp\Framework\Form\Extra\Help;
13
use AbterPhp\Framework\Form\Label\Label;
14
use AbterPhp\Framework\Html\Component;
15
use AbterPhp\Framework\Html\INode;
16
use AbterPhp\Website\Domain\Entities\ContentListItem as Entity;
17
18
class Item
19
{
20
    /** @var int */
21
    protected $count = 1;
22
23
    /** @var string */
24
    protected $id;
25
26
    /** @var array */
27
    protected $hiddenAttribs = [Html5::ATTR_TYPE => Input::TYPE_HIDDEN];
28
29
    /**
30
     * @
31
     * @param Entity|null $entity
32
     * @param bool        $withLinks
33
     * @param bool        $withLabelLinks
34
     * @param bool        $withHtml
35
     * @param bool        $withImages
36
     * @param bool        $withClasses
37
     *
38
     * @return INode[]
39
     */
40
    public function create(
41
        ?Entity $entity,
42
        bool $withLinks,
43
        bool $withLabelLinks,
44
        bool $withHtml,
45
        bool $withImages,
46
        bool $withClasses
47
    ): array {
48
        $this->id = $entity ? "existing{$this->count}" : 'new';
49
50
        $components = [];
51
52
        if ($entity) {
53
            $components[] = $this->addId($entity);
54
        }
55
56
        $components[] = $this->addLabel($entity);
57
        if ($withLinks && $withLabelLinks) {
58
            $components[] = $this->addLabelHref($entity);
59
        }
60
61
        $components[] = $this->addContent($entity, $withHtml);
62
        if ($withLinks) {
63
            $components[] = $this->addContentHref($entity);
64
        }
65
66
        if ($withImages) {
67
            $components[] = $this->addImgSrc($entity);
68
            $components[] = $this->addImgAlt($entity);
69
            if ($withLinks) {
70
                $components[] = $this->addImgHref($entity);
71
            }
72
        }
73
74
        if ($withClasses) {
75
            $components[] = $this->addClasses($entity);
76
        }
77
78
        $components[] = $this->addIsDeleted();
79
80
        $this->count++;
81
82
        return $components;
83
    }
84
85
    /**
86
     * @param Entity $entity
87
     *
88
     * @return INode
89
     */
90
    protected function addId(Entity $entity): INode
91
    {
92
        return new Input("item_id_{$this->id}", "{$this->id}[id]", $entity->getId(), [], $this->hiddenAttribs);
93
    }
94
95
    /**
96
     * @param Entity|null $entity
97
     *
98
     * @return INode
99
     */
100
    protected function addLabel(?Entity $entity): INode
101
    {
102
        $label = $entity ? $entity->getLabel() : '';
103
104
        $input = new Input("item_label_{$this->id}", "{$this->id}[label]", $label);
105
        $label = new Label("item_label_{$this->id}", 'website:contentListItemLabel');
106
        $help  = new Help('website:contentListItemLabelHelp');
107
108
        return new FormGroup($input, $label, $help);
109
    }
110
111
    /**
112
     * @param Entity|null $entity
113
     *
114
     * @return INode
115
     */
116
    protected function addLabelHref(?Entity $entity): INode
117
    {
118
        $labelHref = $entity ? $entity->getLabelHref() : '';
119
120
        $input = new Input("item_label_href_{$this->id}", "{$this->id}[label_href]", $labelHref);
121
        $label = new Label("item_label_href_{$this->id}", 'website:contentListItemLabelHref');
122
        $help  = new Help('website:contentListItemLabelHrefHelp');
123
124
        return new FormGroup($input, $label, $help);
125
    }
126
127
    /**
128
     * @param Entity|null $entity
129
     * @param bool        $withHtml
130
     *
131
     * @return INode
132
     */
133
    protected function addContent(?Entity $entity, bool $withHtml): INode
134
    {
135
        $content = $entity ? $entity->getContent() : '';
136
        $attribs = [Html5::ATTR_ROWS => '7'];
137
        if ($withHtml) {
138
            $attribs = [Html5::ATTR_CLASS => 'wysiwyg', Html5::ATTR_ROWS => '15'];
139
        }
140
141
        $input = new Textarea("item_content_{$this->id}", "{$this->id}[content]", $content, [], $attribs);
142
        $label = new Label("item_content_{$this->id}", 'website:contentListItemContent');
143
        $help  = new Help('website:contentListItemContentHelp');
144
145
        return new FormGroup($input, $label, $help);
146
    }
147
148
    /**
149
     * @param Entity|null $entity
150
     *
151
     * @return INode
152
     */
153
    protected function addContentHref(?Entity $entity): INode
154
    {
155
        $contentHref = $entity ? $entity->getContentHref() : '';
156
157
        $input = new Input("item_content_href_{$this->id}", "{$this->id}[content_href]", $contentHref);
158
        $label = new Label("item_content_href_{$this->id}", 'website:contentListItemContentHref');
159
        $help  = new Help('website:contentListItemContentHrefHelp');
160
161
        return new FormGroup($input, $label, $help);
162
    }
163
164
    /**
165
     * @param Entity|null $entity
166
     *
167
     * @return INode
168
     */
169
    protected function addImgSrc(?Entity $entity): INode
170
    {
171
        $imgSrc = $entity ? $entity->getImgSrc() : '';
172
173
        $input = new Input("item_img_src_{$this->id}", "{$this->id}[img_src]", $imgSrc);
174
        $label = new Label("item_img_src_{$this->id}", 'website:contentListItemImgSrc');
175
        $help  = new Help('website:contentListItemImgSrcHelp');
176
177
        return new FormGroup($input, $label, $help);
178
    }
179
180
    /**
181
     * @param Entity|null $entity
182
     *
183
     * @return INode
184
     */
185
    protected function addImgAlt(?Entity $entity): INode
186
    {
187
        $imgAlt = $entity ? $entity->getImgAlt() : '';
188
189
        $input = new Input("item_img_alt_{$this->id}", "{$this->id}[img_alt]", $imgAlt);
190
        $label = new Label("item_img_alt_{$this->id}", 'website:contentListItemImgAlt');
191
        $help  = new Help('website:contentListItemImgAltHelp');
192
193
        return new FormGroup($input, $label, $help);
194
    }
195
196
    /**
197
     * @param Entity|null $entity
198
     *
199
     * @return INode
200
     */
201
    protected function addImgHref(?Entity $entity): INode
202
    {
203
        $imgHref = $entity ? $entity->getImgHref() : '';
204
205
        $input = new Input("item_img_href_{$this->id}", "{$this->id}[img_href]", $imgHref);
206
        $label = new Label("item_img_href_{$this->id}", 'website:contentListItemImgHref');
207
        $help  = new Help('website:contentListItemImgHrefHelp');
208
209
        return new FormGroup($input, $label, $help);
210
    }
211
212
    /**
213
     * @param Entity|null $entity
214
     *
215
     * @return INode
216
     */
217
    protected function addClasses(?Entity $entity): INode
218
    {
219
        $classes = $entity ? $entity->getClasses() : '';
220
221
        $input = new Input("item_classes_{$this->id}", "{$this->id}[classes]", $classes);
222
        $label = new Label("item_classes_{$this->id}", 'website:contentListItemClasses');
223
        $help  = new Help('website:contentListItemClassesHelp');
224
225
        return new FormGroup($input, $label, $help);
226
    }
227
228
    /**
229
     * @return INode
230
     */
231
    protected function addIsDeleted(): INode
232
    {
233
        $attributes = [Html5::ATTR_TYPE => Input::TYPE_CHECKBOX, Html5::ATTR_WRAP => ['item_is_deleted']];
234
235
        $input = new Input(
236
            "item_is_deleted_{$this->id}",
237
            "{$this->id}[is_deleted]",
238
            '1',
239
            [],
240
            $attributes
241
        );
242
        $label = new Label("item_is_deleted_{$this->id}", 'website:contentListItemIsDeleted');
243
        $help  = new Component('website:contentListItemIsDeletedHelp');
244
245
        return new CheckboxGroup($input, $label, $help, [], [Html5::ATTR_CLASS => 'is-deleted-container']);
246
    }
247
}
248