Passed
Push — master ( 4c4f3f...4700f4 )
by Sergei
02:48
created

CheckboxList::renderHint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field;
6
7
use Closure;
8
use InvalidArgumentException;
9
use Stringable;
10
use Yiisoft\Form\Field\Base\PartsField;
11
use Yiisoft\Form\Field\Base\FormAttributeTrait;
12
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
13
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
14
use Yiisoft\Form\Field\Part\Error;
15
use Yiisoft\Form\Field\Part\Hint;
16
use Yiisoft\Form\Field\Part\Label;
17
use Yiisoft\Form\Helper\HtmlForm;
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 FormAttributeTrait;
29
    use ValidationClassTrait;
30
31
    private CheckboxListWidget $widget;
32
33 15
    public function __construct()
34
    {
35 15
        $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->replaceCheckboxAttributes($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->checkboxAttributes($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->replaceIndividualInputAttributes($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->individualInputAttributes($attributes);
69 2
        return $new;
70
    }
71
72
    /**
73
     * @param string[] $items
74
     * @param bool $encodeLabels Whether labels should be encoded.
75
     */
76 13
    public function items(array $items, bool $encodeLabels = true): self
77
    {
78 13
        $new = clone $this;
79 13
        $new->widget = $this->widget->items($items, $encodeLabels);
80 13
        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 2
    public function itemsFromValues(array $values, bool $encodeLabels = true): self
90
    {
91 2
        $new = clone $this;
92 2
        $new->widget = $this->widget->itemsFromValues($values, $encodeLabels);
93 2
        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
     * @param Closure|null $formatter
137
     *
138
     * @psalm-param Closure(CheckboxItem):string|null $formatter
139
     */
140 2
    public function itemFormatter(?Closure $formatter): self
141
    {
142 2
        $new = clone $this;
143 2
        $new->widget = $this->widget->itemFormatter($formatter);
144 2
        return $new;
145
    }
146
147 14
    protected function generateInput(): string
148
    {
149 14
        $value = $this->getFormAttributeValue();
150
151
        /** @var mixed $value */
152 14
        $value ??= [];
153 14
        if (!is_iterable($value)) {
154 1
            throw new InvalidArgumentException(
155
                '"CheckboxList" field requires iterable or null value.'
156
            );
157
        }
158
        /** @psalm-var iterable<int, Stringable|scalar> $value */
159
160 13
        return $this->widget
161 13
            ->name($this->getInputName())
162 13
            ->values($value)
163 13
            ->render();
164
    }
165
166 13
    protected function renderLabel(Label $label): string
167
    {
168
        return $label
169 13
            ->formAttribute($this->getFormModel(), $this->formAttribute)
170 13
            ->useInputId(false)
171 13
            ->render();
172
    }
173
174 13
    protected function renderHint(Hint $hint): string
175
    {
176
        return $hint
177 13
            ->formAttribute($this->getFormModel(), $this->formAttribute)
178 13
            ->render();
179
    }
180
181 13
    protected function renderError(Error $error): string
182
    {
183
        return $error
184 13
            ->formAttribute($this->getFormModel(), $this->formAttribute)
185 13
            ->render();
186
    }
187
188 13
    protected function prepareContainerAttributes(array &$attributes): void
189
    {
190 13
        if ($this->hasFormModelAndAttribute()) {
191 13
            $this->addValidationClassToAttributes(
192
                $attributes,
193 13
                $this->getFormModel(),
194 13
                $this->getFormAttributeName(),
195
            );
196
        }
197
    }
198
199 13
    private function getInputName(): string
200
    {
201 13
        return HtmlForm::getInputName($this->getFormModel(), $this->formAttribute);
202
    }
203
}
204