Passed
Pull Request — master (#192)
by Sergei
08:26 queued 05:37
created

Text::generateInput()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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