Passed
Push — master ( 26f223...98f436 )
by Peter
03:08
created

Item::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 26
rs 9.7998
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
     * @param Entity|null $entity
31
     * @param bool        $isWithImage
32
     * @param bool        $isWithLinks
33
     * @param bool        $isWithHtml
34
     *
35
     * @return INode[]
36
     */
37
    public function create(
38
        ?Entity $entity = null,
39
        bool $isWithLinks,
40
        bool $isWithImage,
41
        bool $isWithHtml
42
    ): array {
43
        $this->count++;
44
45
        $this->id = $entity ? "existing{$this->count}" : 'new';
46
47
        $components = [];
48
49
        if ($entity) {
50
            $components[] = $this->addId($entity);
51
        }
52
53
        $components[] = $this->addName($entity);
54
        $components[] = $this->addNameHref($entity, $isWithLinks);
55
        $components[] = $this->addBody($entity, $isWithHtml);
56
        $components[] = $this->addBodyHref($entity, $isWithLinks);
57
        $components[] = $this->addImgSrc($entity, $isWithImage);
58
        $components[] = $this->addImgAlt($entity, $isWithImage);
59
        $components[] = $this->addImgHref($entity, $isWithLinks, $isWithHtml);
60
        $components[] = $this->addIsDeleted();
61
62
        return $components;
63
    }
64
65
    /**
66
     * @param Entity $entity
67
     *
68
     * @return INode
69
     */
70
    protected function addId(Entity $entity): INode
71
    {
72
        return new Input("item_id_{$this->id}", "{$this->id}[id]", $entity->getId(), [], $this->hiddenAttribs);
73
    }
74
75
    /**
76
     * @param Entity|null $entity
77
     *
78
     * @return INode
79
     */
80
    protected function addName(?Entity $entity): INode
81
    {
82
        $name = $entity ? $entity->getName() : '';
83
84
        $input = new Input("item_name_{$this->id}", "{$this->id}[name]", $name);
85
        $label = new Label("item_name_{$this->id}", 'website:contentListItemName');
86
        $help  = new Help('website:contentListItemNameHelp');
87
88
        return new FormGroup($input, $label, $help);
89
    }
90
91
    /**
92
     * @param Entity|null $entity
93
     * @param bool        $isWithLinks
94
     *
95
     * @return INode
96
     */
97
    protected function addNameHref(?Entity $entity, bool $isWithLinks): INode
98
    {
99
        if (!$isWithLinks) {
100
            return new Input(
101
                "item_name_href_{$this->id}",
102
                "item_name_href[{$this->id}]",
103
                '',
104
                [],
105
                $this->hiddenAttribs
106
            );
107
        }
108
109
        $nameHref = $entity ? $entity->getNameHref() : '';
110
111
        $input = new Input("item_name_href_{$this->id}", "{$this->id}[name_href]", $nameHref);
112
        $label = new Label("item_name_href_{$this->id}", 'website:contentListItemNameHref');
113
        $help  = new Help('website:contentListItemNameHrefHelp');
114
115
        return new FormGroup($input, $label, $help);
116
    }
117
118
    /**
119
     * @param Entity|null $entity
120
     * @param bool        $isWithHtml
121
     *
122
     * @return INode
123
     */
124
    protected function addBody(?Entity $entity, bool $isWithHtml): INode
125
    {
126
        $body    = $entity ? $entity->getBody() : '';
127
        $attribs = [];
128
        if ($isWithHtml) {
129
            $attribs = [Html5::ATTR_CLASS => 'wysiwyg', Html5::ATTR_ROWS => '15'];
130
        }
131
132
        $input = new Textarea("item_body_{$this->id}", "{$this->id}[body]", $body, [], $attribs);
133
        $label = new Label("item_body_{$this->id}", 'website:contentListItemBody');
134
        $help  = new Help('website:contentListItemBodyHelp');
135
136
        return new FormGroup($input, $label, $help);
137
    }
138
139
    /**
140
     * @param Entity|null $entity
141
     * @param bool        $isWithImage
142
     *
143
     * @return INode
144
     */
145
    protected function addBodyHref(?Entity $entity, bool $isWithImage): INode
146
    {
147
        if (!$isWithImage) {
148
            return new Input("item_body_href_{$this->id}", "{$this->id}[body_href]", '', [], $this->hiddenAttribs);
149
        }
150
151
        $bodyHref = $entity ? $entity->getBodyHref() : '';
152
153
        $input = new Input("item_body_href_{$this->id}", "{$this->id}[body_href]", $bodyHref);
154
        $label = new Label("item_body_href_{$this->id}", 'website:contentListItemBodyHref');
155
        $help  = new Help('website:contentListItemBodyHrefHelp');
156
157
        return new FormGroup($input, $label, $help);
158
    }
159
160
    /**
161
     * @param Entity|null $entity
162
     * @param bool        $isWithImage
163
     *
164
     * @return INode
165
     */
166
    protected function addImgSrc(?Entity $entity, bool $isWithImage): INode
167
    {
168
        if (!$isWithImage) {
169
            return new Input("item_img_src_{$this->id}", "{$this->id}[img_src]", '', [], $this->hiddenAttribs);
170
        }
171
172
        $imgSrc = $entity ? $entity->getImgSrc() : '';
173
174
        $input = new Input("item_img_src_{$this->id}", "{$this->id}[img_src]", $imgSrc);
175
        $label = new Label("item_img_src_{$this->id}", 'website:contentListItemImgSrc');
176
        $help  = new Help('website:contentListItemImgSrcHelp');
177
178
        return new FormGroup($input, $label, $help);
179
    }
180
181
    /**
182
     * @param Entity|null $entity
183
     * @param bool        $isWithImage
184
     *
185
     * @return INode
186
     */
187
    protected function addImgAlt(?Entity $entity, bool $isWithImage): INode
188
    {
189
        if (!$isWithImage) {
190
            return new Input("item_img_alt_{$this->id}", "{$this->id}[img_alt]", '', [], $this->hiddenAttribs);
191
        }
192
193
        $imgAlt = $entity ? $entity->getImgAlt() : '';
194
195
        $input = new Input("item_img_alt_{$this->id}", "{$this->id}[img_alt]", $imgAlt);
196
        $label = new Label("item_img_alt_{$this->id}", 'website:contentListItemImgAlt');
197
        $help  = new Help('website:contentListItemImgAltHelp');
198
199
        return new FormGroup($input, $label, $help);
200
    }
201
202
    /**
203
     * @param Entity|null $entity
204
     * @param bool        $isWithLinks
205
     * @param bool        $isWithImage
206
     *
207
     * @return INode
208
     */
209
    protected function addImgHref(?Entity $entity, bool $isWithLinks, bool $isWithImage): INode
210
    {
211
        if (!$isWithLinks || !$isWithImage) {
212
            return new Input("item_img_href_{$this->id}", "{$this->id}[img_href]", '', [], $this->hiddenAttribs);
213
        }
214
215
        $imgHref = $entity ? $entity->getImgHref() : '';
216
217
        $input = new Input("item_img_href_{$this->id}", "{$this->id}[img_href]", $imgHref);
218
        $label = new Label("item_img_href_{$this->id}", 'website:contentListItemImgHref');
219
        $help  = new Help('website:contentListItemImgHrefHelp');
220
221
        return new FormGroup($input, $label, $help);
222
    }
223
224
    /**
225
     * @return INode
226
     */
227
    protected function addIsDeleted(): INode
228
    {
229
        $attributes = [Html5::ATTR_TYPE => Input::TYPE_CHECKBOX, Html5::ATTR_WRAP => ['item_is_deleted']];
230
231
        $input = new Input(
232
            "item_is_deleted_{$this->id}",
233
            "{$this->id}[is_deleted]",
234
            '1',
235
            [],
236
            $attributes
237
        );
238
        $label = new Label("item_is_deleted_{$this->id}", 'website:contentListItemIsDeleted');
239
        $help  = new Component('website:contentListItemIsDeletedHelp');
240
241
        return new CheckboxGroup($input, $label, $help, [], [Html5::ATTR_CLASS => 'is-deleted-container']);
242
    }
243
}
244