Passed
Push — master ( 8f0f73...d2306b )
by Peter
02:30
created

Item::create()   B

Complexity

Conditions 8
Paths 72

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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