Passed
Push — adopt-factory ( 9be618 )
by Alexander
07:14
created

Radio   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 201
ccs 50
cts 54
cp 0.9259
rs 10
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A enclosedByLabel() 0 5 1
A labelOptions() 0 5 1
A autofocus() 0 5 1
A id() 0 5 1
A form() 0 5 1
A required() 0 5 1
A charset() 0 5 1
A run() 0 9 1
A config() 0 7 1
A label() 0 5 1
A disabled() 0 5 1
A uncheck() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget;
6
7
use Yiisoft\Form\FormModelInterface;
8
use Yiisoft\Widget\Widget;
9
10
final class Radio extends Widget
11
{
12
    private ?string $id = null;
13
    private FormModelInterface $data;
14
    private string $attribute;
15
    private array $options = [];
16
    private string $charset = 'UTF-8';
17
    private bool $enclosedByLabel = true;
18
    private bool $uncheck = true;
19
20
    /**
21
     * Generates a radio button tag together with a label for the given form attribute.
22
     *
23
     * @return string the generated radio button tag.
24
     */
25 15
    public function run(): string
26
    {
27 15
        return BooleanInput::widget()
28 15
            ->type('radio')
0 ignored issues
show
Bug introduced by
The method type() does not exist on Yiisoft\Widget\Widget. It seems like you code against a sub-type of Yiisoft\Widget\Widget such as Yiisoft\Form\Widget\ListBox or Yiisoft\Form\Widget\Input or Yiisoft\Form\Widget\ListInput or Yiisoft\Form\Widget\BooleanInput. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
            ->/** @scrutinizer ignore-call */ type('radio')
Loading history...
29 15
            ->id($this->id)
30 15
            ->config($this->data, $this->attribute, $this->options)
31 15
            ->enclosedByLabel($this->enclosedByLabel)
32 15
            ->uncheck($this->uncheck)
33 15
            ->run();
34
    }
35
36
    /**
37
     * Set form model, name and options for the widget.
38
     *
39
     * @param FormModelInterface $data Form model.
40
     * @param string $attribute Form model property this widget is rendered for.
41
     * @param array $options The HTML attributes for the widget container tag.
42
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
43
     *
44
     * @return self
45
     */
46 15
    public function config(FormModelInterface $data, string $attribute, array $options = []): self
47
    {
48 15
        $new = clone $this;
49 15
        $new->data = $data;
50 15
        $new->attribute = $attribute;
51 15
        $new->options = $options;
52 15
        return $new;
53
    }
54
55
    /**
56
     * Focus on the control (put cursor into it) when the page loads.
57
     * Only one form element could be in focus at the same time.
58
     *
59
     * @param bool $value
60
     *
61
     * @return self
62
     */
63 1
    public function autofocus(bool $value = true): self
64
    {
65 1
        $new = clone $this;
66 1
        $new->options['autofocus'] = $value;
67 1
        return $new;
68
    }
69
70
    /**
71
     * Set the character set used to generate the widget id. See {@see HtmlForm::getInputId()}.
72
     *
73
     * @param string $value
74
     *
75
     * @return self
76
     */
77
    public function charset(string $value): self
78
    {
79
        $new = clone $this;
80
        $new->charset = $value;
81
        return $new;
82
    }
83
84
    /**
85
     * Set whether the element is disabled or not.
86
     *
87
     * If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out
88
     * text.
89
     * If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event
90
     * will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as
91
     * this will suggest you can set it to false to enable the element again, which is not the case.
92
     *
93
     * @param bool $value
94
     *
95
     * @return self
96
     */
97 1
    public function disabled(bool $value = true): self
98
    {
99 1
        $new = clone $this;
100 1
        $new->options['disabled'] = $value;
101 1
        return $new;
102
    }
103
104
    /**
105
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the id
106
     * attribute of a {@see Form} element in the same document.
107
     *
108
     * @param string $value
109
     *
110
     * @return self
111
     */
112 1
    public function form(string $value): self
113
    {
114 1
        $new = clone $this;
115 1
        $new->options['form'] = $value;
116 1
        return $new;
117
    }
118
119
    /**
120
     * Set the Id of the widget.
121
     *
122
     * @param string|null $value
123
     *
124
     * @return self
125
     */
126 1
    public function id(?string $value): self
127
    {
128 1
        $new = clone $this;
129 1
        $new->id = $value;
130 1
        return $new;
131
    }
132
133
    /**
134
     * Label displayed next to the radio.
135
     *
136
     * It will NOT be HTML-encoded, therefore you can pass in HTML code such as an image tag. If this is is coming from
137
     * end users, you should {@see encode()} it to prevent XSS attacks.
138
     *
139
     * When this option is specified, the radio will be enclosed by a label tag.
140
     *
141
     * @param string $value
142
     *
143
     * @return self
144
     */
145 1
    public function label(string $value): self
146
    {
147 1
        $new = clone $this;
148 1
        $new->options['label'] = $value;
149 1
        return $new;
150
    }
151
152
    /**
153
     * HTML attributes for the label tag.
154
     *
155
     * Do not set this option unless you set the "label" option.
156
     *
157
     * @param array $value
158
     *
159
     * @return self
160
     */
161 1
    public function labelOptions(array $value = []): self
162
    {
163 1
        $new = clone $this;
164 1
        $new->options['labelOptions'] = $value;
165 1
        return $new;
166
    }
167
168
    /**
169
     * If the widget should be enclosed by label.
170
     *
171
     * @param bool $value
172
     *
173
     * @return self
174
     */
175 6
    public function enclosedByLabel(bool $value = true): self
176
    {
177 6
        $new = clone $this;
178 6
        $new->enclosedByLabel = $value;
179 6
        return $new;
180
    }
181
182
    /**
183
     * If it is required to fill in a value in order to submit the form.
184
     *
185
     * @param bool $value
186
     *
187
     * @return self
188
     */
189 1
    public function required(bool $value = true): self
190
    {
191 1
        $new = clone $this;
192 1
        $new->options['required'] = $value;
193 1
        return $new;
194
    }
195
196
    /**
197
     * The value associated with the uncheck state of the radio.
198
     *
199
     * When this attribute is present, a hidden input will be generated so that if the radio is not checked and is
200
     * submitted, the value of this attribute will still be submitted to the server via the hidden input.
201
     *
202
     * @param bool $value
203
     *
204
     * @return self
205
     */
206 2
    public function uncheck(bool $value = true): self
207
    {
208 2
        $new = clone $this;
209 2
        $new->uncheck = $value;
210 2
        return $new;
211
    }
212
}
213