Passed
Push — master ( 60c4e3...4cf43a )
by Sergei
02:58
created

CheckboxList::checkboxLabelAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field;
6
7
use Closure;
8
use InvalidArgumentException;
9
use LogicException;
10
use Stringable;
11
use Yiisoft\Form\Field\Base\InputData\InputDataWithCustomNameAndValueTrait;
12
use Yiisoft\Form\Field\Base\PartsField;
13
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
14
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
15
use Yiisoft\Form\Field\Part\Error;
16
use Yiisoft\Form\Field\Part\Hint;
17
use Yiisoft\Form\Field\Part\Label;
18
use Yiisoft\Html\Widget\CheckboxList\CheckboxItem;
19
use Yiisoft\Html\Widget\CheckboxList\CheckboxList as CheckboxListWidget;
20
21
/**
22
 * Represents a list of checkboxes with multiple selection.
23
 *
24
 * @see CheckboxListWidget
25
 */
26
final class CheckboxList extends PartsField implements ValidationClassInterface
27
{
28
    use InputDataWithCustomNameAndValueTrait;
29
    use ValidationClassTrait;
30
31
    private CheckboxListWidget $widget;
32
33 23
    public function __construct()
34
    {
35 23
        $this->widget = CheckboxListWidget::create('');
36
    }
37
38 2
    public function checkboxAttributes(array $attributes): self
39
    {
40 2
        $new = clone $this;
41 2
        $new->widget = $this->widget->checkboxAttributes($attributes);
42 2
        return $new;
43
    }
44
45 2
    public function addCheckboxAttributes(array $attributes): self
46
    {
47 2
        $new = clone $this;
48 2
        $new->widget = $this->widget->addCheckboxAttributes($attributes);
49 2
        return $new;
50
    }
51
52 2
    public function checkboxLabelAttributes(array $attributes): self
53
    {
54 2
        $new = clone $this;
55 2
        $new->widget = $this->widget->checkboxLabelAttributes($attributes);
56 2
        return $new;
57
    }
58
59 2
    public function addCheckboxLabelAttributes(array $attributes): self
60
    {
61 2
        $new = clone $this;
62 2
        $new->widget = $this->widget->addCheckboxLabelAttributes($attributes);
63 2
        return $new;
64
    }
65
66
    /**
67
     * @param array[] $attributes
68
     */
69 2
    public function individualInputAttributes(array $attributes): self
70
    {
71 2
        $new = clone $this;
72 2
        $new->widget = $this->widget->individualInputAttributes($attributes);
73 2
        return $new;
74
    }
75
76
    /**
77
     * @param array[] $attributes
78
     */
79 2
    public function addIndividualInputAttributes(array $attributes): self
80
    {
81 2
        $new = clone $this;
82 2
        $new->widget = $this->widget->addIndividualInputAttributes($attributes);
83 2
        return $new;
84
    }
85
86
    /**
87
     * @param string[] $items
88
     * @param bool $encodeLabels Whether labels should be encoded.
89
     */
90 14
    public function items(array $items, bool $encodeLabels = true): self
91
    {
92 14
        $new = clone $this;
93 14
        $new->widget = $this->widget->items($items, $encodeLabels);
94 14
        return $new;
95
    }
96
97
    /**
98
     * Fills items from an array provided. Array values are used for both input labels and input values.
99
     *
100
     * @param bool[]|float[]|int[]|string[]|Stringable[] $values
101
     * @param bool $encodeLabels Whether labels should be encoded.
102
     */
103 7
    public function itemsFromValues(array $values, bool $encodeLabels = true): self
104
    {
105 7
        $new = clone $this;
106 7
        $new->widget = $this->widget->itemsFromValues($values, $encodeLabels);
107 7
        return $new;
108
    }
109
110
    /**
111
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the ID
112
     * attribute of a form element in the same document.
113
     *
114
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
115
     */
116 2
    public function form(?string $formId): self
117
    {
118 2
        $new = clone $this;
119 2
        $new->widget = $this->widget->form($formId);
120 2
        return $new;
121
    }
122
123
    /**
124
     * @param bool $disabled Whether checkboxes is disabled.
125
     *
126
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
127
     */
128 2
    public function disabled(bool $disabled = true): self
129
    {
130 2
        $new = clone $this;
131 2
        $new->widget = $this->widget->disabled($disabled);
132 2
        return $new;
133
    }
134
135 2
    public function uncheckValue(bool|float|int|string|Stringable|null $value): self
136
    {
137 2
        $new = clone $this;
138 2
        $new->widget = $this->widget->uncheckValue($value);
139 2
        return $new;
140
    }
141
142 2
    public function separator(string $separator): self
143
    {
144 2
        $new = clone $this;
145 2
        $new->widget = $this->widget->separator($separator);
146 2
        return $new;
147
    }
148
149
    /**
150
     * @psalm-param Closure(CheckboxItem):string|null $formatter
151
     */
152 2
    public function itemFormatter(?Closure $formatter): self
153
    {
154 2
        $new = clone $this;
155 2
        $new->widget = $this->widget->itemFormatter($formatter);
156 2
        return $new;
157
    }
158
159 22
    protected function generateInput(): string
160
    {
161 22
        $name = $this->getName();
162 22
        if (empty($name)) {
163 2
            throw new LogicException('"CheckboxList" field requires non-empty name.');
164
        }
165
166 20
        $value = $this->getValue();
167
168 20
        $value ??= [];
169 20
        if (!is_iterable($value)) {
170 1
            throw new InvalidArgumentException(
171 1
                '"CheckboxList" field requires iterable or null value.'
172 1
            );
173
        }
174
        /** @psalm-var iterable<int, Stringable|scalar> $value */
175
176 19
        $checkboxAttributes = [];
177 19
        $this->addInputValidationClassToAttributes(
178 19
            $checkboxAttributes,
179 19
            $this->getInputData(),
180 19
            $this->hasCustomError() ? true : null,
181 19
        );
182
183 19
        return $this->widget
184 19
            ->name($name)
185 19
            ->values($value)
186 19
            ->addCheckboxAttributes($checkboxAttributes)
187 19
            ->render();
188
    }
189
190 19
    protected function renderLabel(Label $label): string
191
    {
192 19
        return $label
193 19
            ->inputData($this->getInputData())
194 19
            ->useInputId(false)
195 19
            ->render();
196
    }
197
198 19
    protected function renderHint(Hint $hint): string
199
    {
200 19
        return $hint
201 19
            ->inputData($this->getInputData())
202 19
            ->render();
203
    }
204
205 19
    protected function renderError(Error $error): string
206
    {
207 19
        return $error
208 19
            ->inputData($this->getInputData())
209 19
            ->render();
210
    }
211
212 19
    protected function prepareContainerAttributes(array &$attributes): void
213
    {
214 19
        $this->addValidationClassToAttributes(
215 19
            $attributes,
216 19
            $this->getInputData(),
217 19
            $this->hasCustomError() ? true : null,
218 19
        );
219
    }
220
}
221