Passed
Pull Request — master (#192)
by Alexander
05:43 queued 02:47
created

CheckboxList::getInputName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
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 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
 * @see CheckboxListWidget
23
 */
24
final class CheckboxList extends PartsField implements ValidationClassInterface
25
{
26
    use FormAttributeTrait;
27
    use ValidationClassTrait;
28
29
    private CheckboxListWidget $widget;
30
31 15
    public function __construct()
32
    {
33 15
        $this->widget = CheckboxListWidget::create('');
34
    }
35
36 3
    public function checkboxAttributes(array $attributes): self
37
    {
38 3
        $new = clone $this;
39 3
        $new->widget = $this->widget->checkboxAttributes($attributes);
40 3
        return $new;
41
    }
42
43 2
    public function replaceCheckboxAttributes(array $attributes): self
44
    {
45 2
        $new = clone $this;
46 2
        $new->widget = $this->widget->replaceCheckboxAttributes($attributes);
47 2
        return $new;
48
    }
49
50
    /**
51
     * @param array[] $attributes
52
     */
53 3
    public function individualInputAttributes(array $attributes): self
54
    {
55 3
        $new = clone $this;
56 3
        $new->widget = $this->widget->individualInputAttributes($attributes);
57 3
        return $new;
58
    }
59
60
    /**
61
     * @param array[] $attributes
62
     */
63 2
    public function replaceIndividualInputAttributes(array $attributes): self
64
    {
65 2
        $new = clone $this;
66 2
        $new->widget = $this->widget->replaceIndividualInputAttributes($attributes);
67 2
        return $new;
68
    }
69
70
    /**
71
     * @param string[] $items
72
     * @param bool $encodeLabels Whether labels should be encoded.
73
     */
74 13
    public function items(array $items, bool $encodeLabels = true): self
75
    {
76 13
        $new = clone $this;
77 13
        $new->widget = $this->widget->items($items, $encodeLabels);
78 13
        return $new;
79
    }
80
81
    /**
82
     * Fills items from an array provided. Array values are used for both input labels and input values.
83
     *
84
     * @param bool[]|float[]|int[]|string[]|Stringable[] $values
85
     * @param bool $encodeLabels Whether labels should be encoded.
86
     */
87 2
    public function itemsFromValues(array $values, bool $encodeLabels = true): self
88
    {
89 2
        $new = clone $this;
90 2
        $new->widget = $this->widget->itemsFromValues($values, $encodeLabels);
91 2
        return $new;
92
    }
93
94
    /**
95
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the ID
96
     * attribute of a form element in the same document.
97
     *
98
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
99
     */
100 2
    public function form(?string $formId): self
101
    {
102 2
        $new = clone $this;
103 2
        $new->widget = $this->widget->form($formId);
104 2
        return $new;
105
    }
106
107
    /**
108
     * @param bool $disabled Whether checkboxes is disabled.
109
     *
110
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
111
     */
112 2
    public function disabled(bool $disabled = true): self
113
    {
114 2
        $new = clone $this;
115 2
        $new->widget = $this->widget->disabled($disabled);
116 2
        return $new;
117
    }
118
119 2
    public function uncheckValue(bool|float|int|string|Stringable|null $value): self
120
    {
121 2
        $new = clone $this;
122 2
        $new->widget = $this->widget->uncheckValue($value);
123 2
        return $new;
124
    }
125
126 2
    public function separator(string $separator): self
127
    {
128 2
        $new = clone $this;
129 2
        $new->widget = $this->widget->separator($separator);
130 2
        return $new;
131
    }
132
133
    /**
134
     * @param Closure|null $formatter
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 14
    protected function generateInput(): string
146
    {
147 14
        $value = $this->getAttributeValue();
148
149
        /** @var mixed $value */
150 14
        $value ??= [];
151 14
        if (!is_iterable($value)) {
152 1
            throw new InvalidArgumentException(
153
                '"CheckboxList" field requires iterable or null value.'
154
            );
155
        }
156
        /** @psalm-var iterable<int, Stringable|scalar> $value */
157
158 13
        return $this->widget
159 13
            ->name($this->getInputName())
160 13
            ->values($value)
161 13
            ->render();
162
    }
163
164 13
    protected function renderLabel(Label $label): string
165
    {
166
        return $label
167 13
            ->attribute($this->getFormModel(), $this->attribute)
168 13
            ->useInputIdAttribute(false)
169 13
            ->render();
170
    }
171
172 13
    protected function renderHint(Hint $hint): string
173
    {
174
        return $hint
175 13
            ->attribute($this->getFormModel(), $this->attribute)
176 13
            ->render();
177
    }
178
179 13
    protected function renderError(Error $error): string
180
    {
181
        return $error
182 13
            ->attribute($this->getFormModel(), $this->attribute)
183 13
            ->render();
184
    }
185
186 13
    protected function prepareContainerTagAttributes(array &$attributes): void
187
    {
188 13
        if ($this->hasFormModelAndAttribute()) {
189 13
            $this->addValidationClassToTagAttributes(
190
                $attributes,
191 13
                $this->getFormModel(),
192 13
                $this->getAttributeName(),
193
            );
194
        }
195
    }
196
197 13
    private function getInputName(): string
198
    {
199 13
        return HtmlForm::getInputName($this->getFormModel(), $this->attribute);
200
    }
201
}
202