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
|
29 |
|
public function __construct() |
32
|
|
|
{ |
33
|
29 |
|
$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
|
|
|
/** |
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
|
3 |
|
public function addIndividualInputAttributes(array $attributes): self |
64
|
|
|
{ |
65
|
3 |
|
$new = clone $this; |
66
|
3 |
|
$new->widget = $this->widget->addIndividualInputAttributes($attributes); |
67
|
3 |
|
return $new; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string[] $items |
72
|
|
|
* @param bool $encodeLabels Whether labels should be encoded. |
73
|
|
|
*/ |
74
|
21 |
|
public function items(array $items, bool $encodeLabels = true): self |
75
|
|
|
{ |
76
|
21 |
|
$new = clone $this; |
77
|
21 |
|
$new->widget = $this->widget->items($items, $encodeLabels); |
78
|
21 |
|
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
|
6 |
|
public function itemsFromValues(array $values, bool $encodeLabels = true): self |
88
|
|
|
{ |
89
|
6 |
|
$new = clone $this; |
90
|
6 |
|
$new->widget = $this->widget->itemsFromValues($values, $encodeLabels); |
91
|
6 |
|
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
|
|
|
* @psalm-param Closure(RadioItem):string|null $formatter |
135
|
|
|
*/ |
136
|
2 |
|
public function itemFormatter(?Closure $formatter): self |
137
|
|
|
{ |
138
|
2 |
|
$new = clone $this; |
139
|
2 |
|
$new->widget = $this->widget->itemFormatter($formatter); |
140
|
2 |
|
return $new; |
141
|
|
|
} |
142
|
|
|
|
143
|
28 |
|
protected function generateInput(): string |
144
|
|
|
{ |
145
|
28 |
|
$name = $this->getName(); |
146
|
28 |
|
if (empty($name)) { |
147
|
2 |
|
throw new LogicException('"RadioList" field requires non-empty name.'); |
148
|
|
|
} |
149
|
|
|
|
150
|
26 |
|
$value = $this->getValue(); |
151
|
|
|
|
152
|
26 |
|
if (!is_bool($value) |
153
|
26 |
|
&& !is_string($value) |
154
|
26 |
|
&& !is_numeric($value) |
155
|
26 |
|
&& $value !== null |
156
|
26 |
|
&& (!is_object($value) || !method_exists($value, '__toString')) |
157
|
|
|
) { |
158
|
1 |
|
throw new InvalidArgumentException( |
159
|
1 |
|
'"RadioList" field requires a string, numeric, bool, Stringable or null value.' |
160
|
1 |
|
); |
161
|
|
|
} |
162
|
|
|
/** @psalm-var Stringable|scalar $value */ |
163
|
|
|
|
164
|
25 |
|
return $this->widget |
165
|
25 |
|
->name($name) |
166
|
25 |
|
->value($value) |
167
|
25 |
|
->render(); |
168
|
|
|
} |
169
|
|
|
|
170
|
2 |
|
protected function renderLabel(Label $label): string |
171
|
|
|
{ |
172
|
2 |
|
return $label |
173
|
2 |
|
->inputData($this->getInputData()) |
174
|
2 |
|
->useInputId(false) |
175
|
2 |
|
->render(); |
176
|
|
|
} |
177
|
|
|
|
178
|
25 |
|
protected function renderHint(Hint $hint): string |
179
|
|
|
{ |
180
|
25 |
|
return $hint |
181
|
25 |
|
->inputData($this->getInputData()) |
182
|
25 |
|
->render(); |
183
|
|
|
} |
184
|
|
|
|
185
|
25 |
|
protected function renderError(Error $error): string |
186
|
|
|
{ |
187
|
25 |
|
return $error |
188
|
25 |
|
->inputData($this->getInputData()) |
189
|
25 |
|
->render(); |
190
|
|
|
} |
191
|
|
|
|
192
|
2 |
|
protected function prepareContainerAttributes(array &$attributes): void |
193
|
|
|
{ |
194
|
2 |
|
$this->addValidationClassToAttributes($attributes, $this->getInputData()); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|