Passed
Pull Request — master (#160)
by Wilmer
03:13
created

GlobalAttributes::attributes()   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 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
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\Attribute;
6
7
use Stringable;
8
use Yiisoft\Html\Html;
9
use Yiisoft\Widget\Widget;
10
11
abstract class GlobalAttributes extends Widget
12
{
13
    protected array $attributes = [];
14
15
    /**
16
     * Focus on the control (put cursor into it) when the page loads.
17
     * Only one form element could be in focus at the same time.
18
     *
19
     * @return static
20
     *
21
     * @link https://www.w3.org/TR/html52/sec-forms.html#autofocusing-a-form-control-the-autofocus-attribute
22
     */
23 38
    public function autofocus(): self
24
    {
25 38
        $new = clone $this;
26 38
        $new->attributes['autofocus'] = true;
27 38
        return $new;
28
    }
29
30
    /**
31
     * The HTML attributes. The following special options are recognized.
32
     *
33
     * @param array $values Attribute values indexed by attribute names.
34
     *
35
     * @return static
36
     *
37
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
38
     */
39 415
    public function attributes(array $values): self
40
    {
41 415
        $new = clone $this;
42 415
        $new->attributes = array_merge($new->attributes, $values);
43 415
        return $new;
44
    }
45
46
    /**
47
     * Set CSS class of the field widget.
48
     *
49
     * @param string $class
50
     *
51
     * @return static
52
     */
53 10
    public function class(string $class): self
54
    {
55 10
        $new = clone $this;
56 10
        Html::addCssClass($new->attributes, $class);
57 10
        return $new;
58
    }
59
60
    /**
61
     * Set whether the element is disabled or not.
62
     *
63
     * If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out
64
     * text.
65
     * If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event
66
     * will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as
67
     * this will suggest you can set it to `false` to enable the element again, which is not the case.
68
     *
69
     * @return static
70
     *
71
     * @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-disabledformelements-disabled
72
     */
73 38
    public function disabled(): self
74
    {
75 38
        $new = clone $this;
76 38
        $new->attributes['disabled'] = true;
77 38
        return $new;
78
    }
79
80
    /**
81
     * Set the ID of the widget.
82
     *
83
     * @param string|null $id
84
     *
85
     * @return static
86
     *
87
     * @link https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute
88
     */
89 73
    public function id(?string $id): self
90
    {
91 73
        $new = clone $this;
92 73
        $new->attributes['id'] = $id;
93 73
        return $new;
94
    }
95
96
    /**
97
     * The name part of the name/value pair associated with this element for the purposes of form submission.
98
     *
99
     * @param string|null The name of the widget.
0 ignored issues
show
Bug introduced by
The type Yiisoft\Form\Widget\Attribute\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...
100
     *
101
     * @return static
102
     *
103
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-name
104
     */
105 80
    public function name(?string $value): self
106
    {
107 80
        $new = clone $this;
108 80
        $new->attributes['name'] = $value;
109 80
        return $new;
110
    }
111
112
    /**
113
     * The tabindex global attribute indicates that its element can be focused, and where it participates in sequential
114
     * keyboard navigation (usually with the Tab key, hence the name).
115
     *
116
     * It accepts an integer as a value, with different results depending on the integer's value:
117
     *
118
     * - A negative value (usually tabindex="-1") means that the element is not reachable via sequential keyboard
119
     * navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
120
     * with JavaScript.
121
     * - tabindex="0" means that the element should be focusable in sequential keyboard navigation, but its order is
122
     * defined by the document's source order.
123
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
124
     * defined by the value of the number. That is, tabindex="4" is focused before tabindex="5", but after tabindex="3".
125
     *
126
     * @param int $value
127
     *
128
     * @return static
129
     *
130
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex
131
     */
132 36
    public function tabIndex(int $value): self
133
    {
134 36
        $new = clone $this;
135 36
        $new->attributes['tabindex'] = $value;
136 36
        return $new;
137
    }
138
139
    /**
140
     * The title global attribute contains text representing advisory information related to the element it belongs to.
141
     *
142
     * @param string $value
143
     *
144
     * @return static
145
     *
146
     * @link https://html.spec.whatwg.org/multipage/dom.html#attr-title
147
     */
148 3
    public function title(string $value): self
149
    {
150 3
        $new = clone $this;
151 3
        $new->attributes['title'] = $value;
152 3
        return $new;
153
    }
154
155
    /**
156
     * The value of the radio button.
157
     *
158
     * @param scalar|Stringable|null $value
159
     *
160
     * @return static
161
     *
162
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-value
163
     */
164 101
    public function value($value): self
165
    {
166 101
        $new = clone $this;
167 101
        $new->attributes['value'] = $value;
168 101
        return $new;
169
    }
170
}
171