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

RadioList::itemsFromValues()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
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\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 FormAttributeTrait;
27
    use ValidationClassTrait;
28
29
    private RadioListWidget $widget;
30
31 27
    public function __construct()
32
    {
33 27
        $this->widget = RadioListWidget::create('');
34
    }
35
36 8
    public function radioAttributes(array $attributes): self
37
    {
38 8
        $new = clone $this;
39 8
        $new->widget = $this->widget->radioAttributes($attributes);
40 8
        return $new;
41
    }
42
43 2
    public function replaceRadioAttributes(array $attributes): self
44
    {
45 2
        $new = clone $this;
46 2
        $new->widget = $this->widget->replaceRadioAttributes($attributes);
47 2
        return $new;
48
    }
49
50
    /**
51
     * @param array[] $attributes
52
     */
53 5
    public function individualInputAttributes(array $attributes): self
54
    {
55 5
        $new = clone $this;
56 5
        $new->widget = $this->widget->individualInputAttributes($attributes);
57 5
        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 22
    public function items(array $items, bool $encodeLabels = true): self
75
    {
76 22
        $new = clone $this;
77 22
        $new->widget = $this->widget->items($items, $encodeLabels);
78 22
        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 5
    public function itemsFromValues(array $values, bool $encodeLabels = true): self
88
    {
89 5
        $new = clone $this;
90 5
        $new->widget = $this->widget->itemsFromValues($values, $encodeLabels);
91 5
        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 3
    public function form(?string $formId): self
101
    {
102 3
        $new = clone $this;
103 3
        $new->widget = $this->widget->form($formId);
104 3
        return $new;
105
    }
106
107
    /**
108
     * @param bool $disabled Whether radio buttons is disabled.
109
     *
110
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
111
     */
112 3
    public function disabled(bool $disabled = true): self
113
    {
114 3
        $new = clone $this;
115 3
        $new->widget = $this->widget->disabled($disabled);
116 3
        return $new;
117
    }
118
119 8
    public function uncheckValue(bool|float|int|string|Stringable|null $value): self
120
    {
121 8
        $new = clone $this;
122 8
        $new->widget = $this->widget->uncheckValue($value);
123 8
        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(RadioItem):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 26
    protected function generateInput(): string
146
    {
147 26
        $value = $this->getAttributeValue();
148
149 26
        if (!is_bool($value)
150 26
            && !is_string($value)
151 26
            && !is_numeric($value)
152 26
            && $value !== null
153 26
            && (!is_object($value) || !method_exists($value, '__toString'))
154
        ) {
155 1
            throw new InvalidArgumentException(
156
                '"RadioList" field requires a string, numeric, bool, Stringable or null value.'
157
            );
158
        }
159
        /** @psalm-var Stringable|scalar $value */
160
161 25
        return $this->widget
162 25
            ->name($this->getInputName())
163 25
            ->value($value)
164 25
            ->render();
165
    }
166
167 2
    protected function renderLabel(Label $label): string
168
    {
169
        return $label
170 2
            ->attribute($this->getFormModel(), $this->attribute)
171 2
            ->useInputIdAttribute(false)
172 2
            ->render();
173
    }
174
175 25
    protected function renderHint(Hint $hint): string
176
    {
177
        return $hint
178 25
            ->attribute($this->getFormModel(), $this->attribute)
179 25
            ->render();
180
    }
181
182 25
    protected function renderError(Error $error): string
183
    {
184
        return $error
185 25
            ->attribute($this->getFormModel(), $this->attribute)
186 25
            ->render();
187
    }
188
189 2
    protected function prepareContainerTagAttributes(array &$attributes): void
190
    {
191 2
        if ($this->hasFormModelAndAttribute()) {
192 2
            $this->addValidationClassToTagAttributes(
193
                $attributes,
194 2
                $this->getFormModel(),
195 2
                $this->getAttributeName(),
196
            );
197
        }
198
    }
199
200 25
    private function getInputName(): string
201
    {
202 25
        return HtmlForm::getInputName($this->getFormModel(), $this->attribute);
203
    }
204
}
205