Passed
Pull Request — master (#192)
by Sergei
07:08 queued 03:58
created

Url   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Test Coverage

Coverage 10%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 39
c 3
b 0
f 0
dl 0
loc 172
ccs 5
cts 50
cp 0.1
rs 10
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A maxlength() 0 5 1
A minlength() 0 5 1
A readonly() 0 5 1
A pattern() 0 5 1
A required() 0 5 1
A ariaLabel() 0 5 1
A autofocus() 0 5 1
A generateInput() 0 11 3
A size() 0 5 1
A disabled() 0 5 1
A ariaDescribedBy() 0 5 1
A tabIndex() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Field\Base\InputField;
9
use Yiisoft\Form\Field\Base\PlaceholderTrait;
10
use Yiisoft\Html\Html;
11
12
use function is_string;
13
14
/**
15
 * @link https://html.spec.whatwg.org/multipage/input.html#url-state-(type=url)
16
 */
17
final class Url extends InputField
18
{
19
    use PlaceholderTrait;
20
21
    /**
22
     * Maximum length of value.
23
     *
24
     * @param int $value A limit on the number of characters a user can input.
25
     *
26
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-maxlength
27
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength
28
     */
29
    public function maxlength(int $value): self
30
    {
31
        $new = clone $this;
32
        $new->inputTagAttributes['maxlength'] = $value;
33
        return $new;
34
    }
35
36
    /**
37
     * Minimum length of value.
38
     *
39
     * @param int $value A lower bound on the number of characters a user can input.
40
     *
41
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength
42
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-minlength
43
     */
44
    public function minlength(int $value): self
45
    {
46
        $new = clone $this;
47
        $new->inputTagAttributes['minlength'] = $value;
48
        return $new;
49
    }
50
51
    /**
52
     * Pattern to be matched by the form control's value.
53
     *
54
     * @param string $value A regular expression against which the control's value.
55
     *
56
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-pattern
57
     */
58
    public function pattern(string $value): self
59
    {
60
        $new = clone $this;
61
        $new->inputTagAttributes['pattern'] = $value;
62
        return $new;
63
    }
64
65
    /**
66
     * A boolean attribute that controls whether or not the user can edit the form control.
67
     *
68
     * @param bool $value Whether to allow the value to be edited by the user.
69
     *
70
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
71
     */
72
    public function readonly(bool $value = true): self
73
    {
74
        $new = clone $this;
75
        $new->inputTagAttributes['readonly'] = $value;
76
        return $new;
77
    }
78
79
    /**
80
     * A boolean attribute. When specified, the element is required.
81
     *
82
     * @param bool $value Whether the control is required for form submission.
83
     *
84
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required
85
     */
86
    public function required(bool $value = true): self
87
    {
88
        $new = clone $this;
89
        $new->inputTagAttributes['required'] = $value;
90
        return $new;
91
    }
92
93
    /**
94
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
95
     */
96
    public function disabled(bool $disabled = true): self
97
    {
98
        $new = clone $this;
99
        $new->inputTagAttributes['disabled'] = $disabled;
100
        return $new;
101
    }
102
103
    /**
104
     * Identifies the element (or elements) that describes the object.
105
     *
106
     * @link https://w3c.github.io/aria/#aria-describedby
107
     */
108
    public function ariaDescribedBy(string $value): self
109
    {
110
        $new = clone $this;
111
        $new->inputTagAttributes['aria-describedby'] = $value;
112
        return $new;
113
    }
114
115
    /**
116
     * Defines a string value that labels the current element.
117
     *
118
     * @link https://w3c.github.io/aria/#aria-label
119
     */
120
    public function ariaLabel(string $value): self
121
    {
122
        $new = clone $this;
123
        $new->inputTagAttributes['aria-label'] = $value;
124
        return $new;
125
    }
126
127
    /**
128
     * Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus
129
     * at the same time.
130
     *
131
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus
132
     */
133
    public function autofocus(bool $value = true): self
134
    {
135
        $new = clone $this;
136
        $new->inputTagAttributes['autofocus'] = $value;
137
        return $new;
138
    }
139
140
    /**
141
     * The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential
142
     * keyboard navigation (usually with the Tab key, hence the name).
143
     *
144
     * It accepts an integer as a value, with different results depending on the integer's value:
145
     *
146
     * - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard
147
     *   navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
148
     *   with JavaScript.
149
     * - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is
150
     *   defined by the document's source order.
151
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
152
     *   defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after
153
     *   `tabindex="3"`.
154
     *
155
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex
156
     */
157
    public function tabIndex(?int $value): self
158
    {
159
        $new = clone $this;
160
        $new->inputTagAttributes['tabindex'] = $value;
161
        return $new;
162
    }
163
164
    /**
165
     * The size of the control.
166
     *
167
     * @param int $value The number of characters that allow the user to see while editing the element's value.
168
     *
169
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-size
170
     */
171
    public function size(int $value): self
172
    {
173
        $new = clone $this;
174
        $new->inputTagAttributes['size'] = $value;
175
        return $new;
176
    }
177
178 1
    protected function generateInput(): string
179
    {
180 1
        $value = $this->getAttributeValue();
181
182 1
        if (!is_string($value) && $value !== null) {
183
            throw new InvalidArgumentException('URL widget must be a string or null value.');
184
        }
185
186 1
        $tagAttributes = $this->getInputTagAttributes();
187
188 1
        return Html::input('url', $this->getInputName(), $value, $tagAttributes)->render();
189
    }
190
}
191