Passed
Push — master ( ef6f5d...60f4bc )
by Alexander
02:52
created

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