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

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