Passed
Push — master ( 3c0766...793f0f )
by Chris
05:28
created

Checklist::getControlValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 6
rs 10
1
<?php
2
3
namespace WebTheory\Saveyour\Deprecated;
4
5
use WebTheory\Saveyour\Contracts\FormFieldInterface;
6
use WebTheory\Saveyour\Elements\Label;
7
use WebTheory\Saveyour\Fields\AbstractCompositeField;
0 ignored issues
show
Bug introduced by
The type WebTheory\Saveyour\Fields\AbstractCompositeField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use WebTheory\Saveyour\Fields\Checkbox;
9
use WebTheory\Saveyour\Fields\Hidden;
10
11
class Checklist extends AbstractCompositeField implements FormFieldInterface
12
{
13
    /**
14
     * Associative array of item definitions with the value as the key
15
     *
16
     * @var array
17
     */
18
    protected $items = [];
19
20
    /**
21
     *
22
     */
23
    protected $value = [];
24
25
    /**
26
     * @var array
27
     */
28
    protected $labelOptions = [];
29
30
    /**
31
     * Value for hidden input that facilitates unsetting all values on the server
32
     *
33
     * @var string
34
     */
35
    protected $clearControl = '0';
36
37
    /**
38
     * Value for hidden input that facilitates unsetting single value on the server
39
     *
40
     * @var mixed
41
     */
42
    protected $toggleControl = '0';
43
44
    /**
45
     * @var null|string
46
     */
47
    protected $controlType = 'clear';
48
49
    /**
50
     *
51
     */
52
    public function setValue($value)
53
    {
54
        $this->value = $value;
55
    }
56
57
    /**
58
     * Get the value of items
59
     *
60
     * @return mixed
61
     */
62
    public function getItems()
63
    {
64
        return $this->items;
65
    }
66
67
    /**
68
     * Set the value of items
69
     *
70
     * @param mixed $items
71
     *
72
     * @return self
73
     */
74
    public function setItems($items)
75
    {
76
        $this->items = $items;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Get value for hidden input that facilitates unsetting all values on the server
83
     *
84
     * @return string
85
     */
86
    public function getClearControl(): string
87
    {
88
        return $this->clearControl;
89
    }
90
91
    /**
92
     * Set value for hidden input that facilitates unsetting all values on the server
93
     *
94
     * @param string
95
     *
96
     * @return self
97
     */
98
    public function setClearControl(string $clearControl)
99
    {
100
        $this->clearControl = $clearControl;
101
        $this->controlType = 'clear';
102
103
        return $this;
104
    }
105
106
    /**
107
     * Get value for hidden input that facilitates unsetting single value on the server
108
     *
109
     * @return mixed
110
     */
111
    public function getToggleControl()
112
    {
113
        return $this->toggleControl;
114
    }
115
116
    /**
117
     * Set value for hidden input that facilitates unsetting single value on the server
118
     *
119
     * @param mixed $toggleControl
120
     *
121
     * @return self
122
     */
123
    public function setToggleControl($toggleControl)
124
    {
125
        $this->toggleControl = $toggleControl;
126
        $this->controlType = 'toggle';
127
128
        return $this;
129
    }
130
131
    /**
132
     *
133
     */
134
    public function isToggleControlType(): bool
135
    {
136
        return 'control' === $this->controlType;
137
    }
138
139
    /**
140
     *
141
     */
142
    public function isClearControlType(): bool
143
    {
144
        return 'clear' === $this->controlType;
145
    }
146
147
    /**
148
     *
149
     */
150
    public function getControlValue(): string
151
    {
152
        return $this->isClearControlType() ? $this->clearControl : $this->toggleControl;
153
    }
154
155
    /**
156
     *
157
     */
158
    protected function createClearControl(): Hidden
159
    {
160
        return (new Hidden())
161
            ->setName($this->name . "[]")
162
            ->setValue($this->clearControl);
163
    }
164
165
    /**
166
     *
167
     */
168
    protected function createItemToggleControl(array $values): Hidden
169
    {
170
        $basename = $values['name'] ?? '';
171
172
        return (new Hidden())
173
            ->setName($this->name . "[{$basename}]")
174
            ->setValue($this->toggleControl);
175
    }
176
177
    /**
178
     *
179
     */
180
    protected function defineItemName(array $values)
181
    {
182
        $basename = $values['name'] ?? '';
183
184
        return $this->isToggleControlType()
185
            ? $this->name . "[{$basename}]"
186
            : $this->name . "[]";
187
    }
188
189
    /**
190
     *
191
     */
192
    protected function createItemCheckBox(array $values): Checkbox
193
    {
194
        return (new Checkbox())
195
            ->setId($values['id'] ?? '')
196
            ->setValue($values['value'] ?? '')
197
            ->setName($this->defineItemName($values));
198
    }
199
200
    /**
201
     *
202
     */
203
    protected function isItemChecked(string $item): bool
204
    {
205
        return in_array($item, $this->value);
206
    }
207
208
    /**
209
     *
210
     */
211
    protected function createItemLabel(array $values): Label
212
    {
213
        return $this->createLabel($values['label'] ?? '', $this->labelOptions);
214
    }
215
216
    /**
217
     *
218
     */
219
    protected function defineChecklistItem(string $item, array $values)
220
    {
221
        $html = '';
222
        $html .= $this->isToggleControlType() ? $this->createItemToggleControl($values) : '';
223
        $html .= $this->createItemCheckBox($values)->setChecked($this->isItemChecked($item));
224
        $html .= $this->createItemLabel($values)->setFor($values['id'] ?? '');
225
226
        return $html;
227
    }
228
229
    /**
230
     *
231
     */
232
    public function renderHtmlMarkup(): string
233
    {
234
        $html = '';
235
        $html .= $this->open('div', $this->attributes ?? null);
236
        $html .= $this->isClearControlType() ? $this->createClearControl() : '';
237
        $html .= $this->open('ul');
238
239
        foreach ($this->items as $item => $values) {
240
            $html .= $this->open('li');
241
            $html .= $this->defineChecklistItem($item, $values);
242
            $html .= $this->close('li');
243
        }
244
245
        $html .= $this->close('ul');
246
        $html .= $this->close('div');
247
248
        return $html;
249
    }
250
}
251