Passed
Push — master ( 65be89...3604a1 )
by Alexander
02:15
created

Fieldset::disabled()   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 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
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\Widget;
6
7
use Yiisoft\Html\Html;
8
use Yiisoft\Html\Tag\CustomTag;
9
use Yiisoft\Widget\Widget;
10
11
/**
12
 * The <fieldset> HTML element is used to group several controls as well as labels (<label>) within a web form.
13
 *
14
 * @link https://html.spec.whatwg.org/multipage/form-elements.html#the-fieldset-element
15
 */
16
final class Fieldset extends Widget
17
{
18
    private array $attributes = [];
19
    private ?string $legend = null;
20
    private array $legendAttributes = [];
21
22
    /**
23
     * @return string the generated form start tag.
24
     *
25
     * {@see end())}
26
     */
27 8
    public function begin(): string
28
    {
29 8
        parent::begin();
30
31 8
        $html = Html::openTag('fieldset', $this->attributes);
32
33 8
        if ($this->legend !== null) {
34 1
            $html .= PHP_EOL .
35 1
                CustomTag::name('legend')
36 1
                    ->attributes($this->legendAttributes)
37 1
                    ->content($this->legend)
38 1
                    ->render();
39
        }
40
41 8
        return $html;
42
    }
43
44
    /**
45
     * The HTML attributes. The following special options are recognized.
46
     *
47
     * @param array $value
48
     *
49
     * @return static
50
     *
51
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
52
     */
53 2
    public function attributes(array $value): self
54
    {
55 2
        $new = clone $this;
56 2
        $new->attributes = $value;
57 2
        return $new;
58
    }
59
60
    /**
61
     * Focus on the control (put cursor into it) when the page loads.
62
     * Only one form element could be in focus at the same time.
63
     *
64
     * @return static
65
     *
66
     * @link https://www.w3.org/TR/html52/sec-forms.html#autofocusing-a-form-control-the-autofocus-attribute
67
     */
68 2
    public function autofocus(): self
69
    {
70 2
        $new = clone $this;
71 2
        $new->attributes['autofocus'] = true;
72 2
        return $new;
73
    }
74
75
    /**
76
     * Set CSS class of the field widget.
77
     *
78
     * @param string $class
79
     *
80
     * @return static
81
     */
82 2
    public function class(string $class): self
83
    {
84 2
        $new = clone $this;
85 2
        Html::addCssClass($new->attributes, $class);
86 2
        return $new;
87
    }
88
89
    /**
90
     * Set whether the element is disabled or not.
91
     *
92
     * If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out
93
     * text.
94
     * If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event
95
     * will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as
96
     * this will suggest you can set it to `false` to enable the element again, which is not the case.
97
     *
98
     * @return static
99
     *
100
     * @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-disabledformelements-disabled
101
     */
102 2
    public function disabled(): self
103
    {
104 2
        $new = clone $this;
105 2
        $new->attributes['disabled'] = true;
106 2
        return $new;
107
    }
108
109
    /**
110
     * Set the ID of the widget.
111
     *
112
     * @param string|null $id
113
     *
114
     * @return static
115
     *
116
     * @link https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute
117
     */
118 2
    public function id(?string $id): self
119
    {
120 2
        $new = clone $this;
121 2
        $new->attributes['id'] = $id;
122 2
        return $new;
123
    }
124
125
    /**
126
     * The <legend> HTML element represents a caption for the content of its parent <fieldset>.
127
     *
128
     * @param string|null $value whether the legend is enabled or disabled.
129
     *
130
     * @return static
131
     *
132
     * @link https://html.spec.whatwg.org/multipage/form-elements.html#the-legend-element
133
     */
134 2
    public function legend(?string $value): self
135
    {
136 2
        $new = clone $this;
137 2
        $new->legend = $value;
138 2
        return $new;
139
    }
140
141
    /**
142
     * The HTML attributes. The following special options are recognized.
143
     *
144
     * @param array $values Attribute values indexed by attribute names.
145
     *
146
     * @return static
147
     *
148
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
149
     */
150 1
    public function legendAttributes(array $values): self
151
    {
152 1
        $new = clone $this;
153 1
        $new->legendAttributes = $values;
154 1
        return $new;
155
    }
156
157
    /**
158
     * The name part of the name/value pair associated with this element for the purposes of form submission.
159
     *
160
     * @param string|null The name of the widget.
0 ignored issues
show
Bug introduced by
The type Yiisoft\Form\Widget\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
161
     *
162
     * @return static
163
     *
164
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-name
165
     */
166 3
    public function name(?string $value): self
167
    {
168 3
        $new = clone $this;
169 3
        $new->attributes['name'] = $value;
170 3
        return $new;
171
    }
172
173
    /**
174
     * The title global attribute contains text representing advisory information related to the element it belongs to.
175
     *
176
     * @param string $value
177
     *
178
     * @return static
179
     *
180
     * @link https://html.spec.whatwg.org/multipage/dom.html#attr-title
181
     */
182 2
    public function title(string $value): self
183
    {
184 2
        $new = clone $this;
185 2
        $new->attributes['title'] = $value;
186 2
        return $new;
187
    }
188
189
    /**
190
     * Generates a form end tag.
191
     *
192
     * @return string the generated tag.
193
     *
194
     * {@see beginForm()}
195
     */
196 1
    protected function run(): string
197
    {
198 1
        return Html::closeTag('fieldset') . PHP_EOL;
199
    }
200
}
201