Passed
Pull Request — master (#192)
by Alexander
04:44 queued 02:17
created

RadioList   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Test Coverage

Coverage 45.33%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 66
c 3
b 0
f 0
dl 0
loc 179
ccs 34
cts 75
cp 0.4533
rs 10
wmc 25

18 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceRadioAttributes() 0 5 1
A renderHint() 0 5 1
A form() 0 5 1
A individualInputAttributes() 0 5 1
B generateInput() 0 20 7
A items() 0 5 1
A renderError() 0 5 1
A separator() 0 5 1
A renderLabel() 0 6 1
A __construct() 0 3 1
A getInputName() 0 3 1
A itemsFromValues() 0 5 1
A replaceIndividualInputAttributes() 0 5 1
A uncheckValue() 0 5 1
A disabled() 0 5 1
A itemFormatter() 0 5 1
A radioAttributes() 0 5 1
A prepareContainerTagAttributes() 0 7 2
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 2
    public function __construct()
32
    {
33 2
        $this->widget = RadioListWidget::create('');
34
    }
35
36
    public function radioAttributes(array $attributes): self
37
    {
38
        $new = clone $this;
39
        $new->widget = $this->widget->radioAttributes($attributes);
40
        return $new;
41
    }
42
43
    public function replaceRadioAttributes(array $attributes): self
44
    {
45
        $new = clone $this;
46
        $new->widget = $this->widget->replaceRadioAttributes($attributes);
47
        return $new;
48
    }
49
50
    /**
51
     * @param array[] $attributes
52
     */
53
    public function individualInputAttributes(array $attributes): self
54
    {
55
        $new = clone $this;
56
        $new->widget = $this->widget->individualInputAttributes($attributes);
57
        return $new;
58
    }
59
60
    /**
61
     * @param array[] $attributes
62
     */
63
    public function replaceIndividualInputAttributes(array $attributes): self
64
    {
65
        $new = clone $this;
66
        $new->widget = $this->widget->replaceIndividualInputAttributes($attributes);
67
        return $new;
68
    }
69
70
    /**
71
     * @param string[] $items
72
     * @param bool $encodeLabels Whether labels should be encoded.
73
     */
74 2
    public function items(array $items, bool $encodeLabels = true): self
75
    {
76 2
        $new = clone $this;
77 2
        $new->widget = $this->widget->items($items, $encodeLabels);
78 2
        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
    public function itemsFromValues(array $values, bool $encodeLabels = true): self
88
    {
89
        $new = clone $this;
90
        $new->widget = $this->widget->itemsFromValues($values, $encodeLabels);
91
        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
    public function form(?string $formId): self
101
    {
102
        $new = clone $this;
103
        $new->widget = $this->widget->form($formId);
104
        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
    public function disabled(bool $disabled = true): self
113
    {
114
        $new = clone $this;
115
        $new->widget = $this->widget->disabled($disabled);
116
        return $new;
117
    }
118
119
    public function uncheckValue(bool|float|int|string|Stringable|null $value): self
120
    {
121
        $new = clone $this;
122
        $new->widget = $this->widget->uncheckValue($value);
123
        return $new;
124
    }
125
126
    public function separator(string $separator): self
127
    {
128
        $new = clone $this;
129
        $new->widget = $this->widget->separator($separator);
130
        return $new;
131
    }
132
133
    /**
134
     * @param Closure|null $formatter
135
     *
136
     * @psalm-param Closure(RadioItem):string|null $formatter
137
     */
138
    public function itemFormatter(?Closure $formatter): self
139
    {
140
        $new = clone $this;
141
        $new->widget = $this->widget->itemFormatter($formatter);
142
        return $new;
143
    }
144
145 2
    protected function generateInput(): string
146
    {
147 2
        $value = $this->getAttributeValue();
148
149 2
        if (!is_bool($value)
150 2
            && !is_string($value)
151 2
            && !is_numeric($value)
152 2
            && $value !== null
153 2
            && (!is_object($value) || !method_exists($value, '__toString'))
154
        ) {
155
            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 2
        return $this->widget
162 2
            ->name($this->getInputName())
163 2
            ->value($value)
164 2
            ->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 2
    protected function renderHint(Hint $hint): string
176
    {
177
        return $hint
178 2
            ->attribute($this->getFormModel(), $this->attribute)
179 2
            ->render();
180
    }
181
182 2
    protected function renderError(Error $error): string
183
    {
184
        return $error
185 2
            ->attribute($this->getFormModel(), $this->attribute)
186 2
            ->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 2
    private function getInputName(): string
201
    {
202 2
        return HtmlForm::getInputName($this->getFormModel(), $this->attribute);
203
    }
204
}
205