Passed
Push — master ( 55e218...1de924 )
by Sergei
04:29 queued 01:39
created

CheckboxList::generateInput()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 21
ccs 14
cts 14
cp 1
crap 3
rs 9.8666
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 17
    public function __construct()
34
    {
35 17
        $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
    /**
53
     * @param array[] $attributes
54
     */
55 2
    public function individualInputAttributes(array $attributes): self
56
    {
57 2
        $new = clone $this;
58 2
        $new->widget = $this->widget->individualInputAttributes($attributes);
59 2
        return $new;
60
    }
61
62
    /**
63
     * @param array[] $attributes
64
     */
65 2
    public function addIndividualInputAttributes(array $attributes): self
66
    {
67 2
        $new = clone $this;
68 2
        $new->widget = $this->widget->addIndividualInputAttributes($attributes);
69 2
        return $new;
70
    }
71
72
    /**
73
     * @param string[] $items
74
     * @param bool $encodeLabels Whether labels should be encoded.
75
     */
76 12
    public function items(array $items, bool $encodeLabels = true): self
77
    {
78 12
        $new = clone $this;
79 12
        $new->widget = $this->widget->items($items, $encodeLabels);
80 12
        return $new;
81
    }
82
83
    /**
84
     * Fills items from an array provided. Array values are used for both input labels and input values.
85
     *
86
     * @param bool[]|float[]|int[]|string[]|Stringable[] $values
87
     * @param bool $encodeLabels Whether labels should be encoded.
88
     */
89 3
    public function itemsFromValues(array $values, bool $encodeLabels = true): self
90
    {
91 3
        $new = clone $this;
92 3
        $new->widget = $this->widget->itemsFromValues($values, $encodeLabels);
93 3
        return $new;
94
    }
95
96
    /**
97
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the ID
98
     * attribute of a form element in the same document.
99
     *
100
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
101
     */
102 2
    public function form(?string $formId): self
103
    {
104 2
        $new = clone $this;
105 2
        $new->widget = $this->widget->form($formId);
106 2
        return $new;
107
    }
108
109
    /**
110
     * @param bool $disabled Whether checkboxes is disabled.
111
     *
112
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
113
     */
114 2
    public function disabled(bool $disabled = true): self
115
    {
116 2
        $new = clone $this;
117 2
        $new->widget = $this->widget->disabled($disabled);
118 2
        return $new;
119
    }
120
121 2
    public function uncheckValue(bool|float|int|string|Stringable|null $value): self
122
    {
123 2
        $new = clone $this;
124 2
        $new->widget = $this->widget->uncheckValue($value);
125 2
        return $new;
126
    }
127
128 2
    public function separator(string $separator): self
129
    {
130 2
        $new = clone $this;
131 2
        $new->widget = $this->widget->separator($separator);
132 2
        return $new;
133
    }
134
135
    /**
136
     * @psalm-param Closure(CheckboxItem):string|null $formatter
137
     */
138 2
    public function itemFormatter(?Closure $formatter): self
139
    {
140 2
        $new = clone $this;
141 2
        $new->widget = $this->widget->itemFormatter($formatter);
142 2
        return $new;
143
    }
144
145 16
    protected function generateInput(): string
146
    {
147 16
        $name = $this->getName();
148 16
        if (empty($name)) {
149 2
            throw new LogicException('"CheckboxList" field requires non-empty name.');
150
        }
151
152 14
        $value = $this->getValue();
153
154 14
        $value ??= [];
155 14
        if (!is_iterable($value)) {
156 1
            throw new InvalidArgumentException(
157 1
                '"CheckboxList" field requires iterable or null value.'
158 1
            );
159
        }
160
        /** @psalm-var iterable<int, Stringable|scalar> $value */
161
162 13
        return $this->widget
163 13
            ->name($name)
164 13
            ->values($value)
165 13
            ->render();
166
    }
167
168 13
    protected function renderLabel(Label $label): string
169
    {
170 13
        return $label
171 13
            ->inputData($this->getInputData())
172 13
            ->useInputId(false)
173 13
            ->render();
174
    }
175
176 13
    protected function renderHint(Hint $hint): string
177
    {
178 13
        return $hint
179 13
            ->inputData($this->getInputData())
180 13
            ->render();
181
    }
182
183 13
    protected function renderError(Error $error): string
184
    {
185 13
        return $error
186 13
            ->inputData($this->getInputData())
187 13
            ->render();
188
    }
189
190 13
    protected function prepareContainerAttributes(array &$attributes): void
191
    {
192 13
        $this->addValidationClassToAttributes($attributes, $this->getInputData());
193
    }
194
}
195