Passed
Pull Request — master (#192)
by Alexander
03:40
created

Number::ariaDescribedBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
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
/**
16
 * A control for setting the element's value to a string representing a number.
17
 *
18
 * @link https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number)
19
 */
20
final class Number extends InputField implements PlaceholderInterface, ValidationClassInterface
21
{
22
    use PlaceholderTrait;
23
    use ValidationClassTrait;
24
25
    /**
26
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-max
27
     */
28
    public function max(?string $value): self
29
    {
30
        $new = clone $this;
31
        $new->inputTagAttributes['max'] = $value;
32
        return $new;
33
    }
34
35
    /**
36
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-min
37
     */
38
    public function min(?string $value): self
39
    {
40
        $new = clone $this;
41
        $new->inputTagAttributes['min'] = $value;
42
        return $new;
43
    }
44
45
    /**
46
     * A boolean attribute that controls whether or not the user can edit the form control.
47
     *
48
     * @param bool $value Whether to allow the value to be edited by the user.
49
     *
50
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
51
     */
52
    public function readonly(bool $value = true): self
53
    {
54
        $new = clone $this;
55
        $new->inputTagAttributes['readonly'] = $value;
56
        return $new;
57
    }
58
59
    /**
60
     * A boolean attribute. When specified, the element is required.
61
     *
62
     * @param bool $value Whether the control is required for form submission.
63
     *
64
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required
65
     */
66
    public function required(bool $value = true): self
67
    {
68
        $new = clone $this;
69
        $new->inputTagAttributes['required'] = $value;
70
        return $new;
71
    }
72
73
    /**
74
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
75
     */
76
    public function disabled(bool $disabled = true): self
77
    {
78
        $new = clone $this;
79
        $new->inputTagAttributes['disabled'] = $disabled;
80
        return $new;
81
    }
82
83
    /**
84
     * Identifies the element (or elements) that describes the object.
85
     *
86
     * @link https://w3c.github.io/aria/#aria-describedby
87
     */
88
    public function ariaDescribedBy(string $value): self
89
    {
90
        $new = clone $this;
91
        $new->inputTagAttributes['aria-describedby'] = $value;
92
        return $new;
93
    }
94
95
    /**
96
     * Defines a string value that labels the current element.
97
     *
98
     * @link https://w3c.github.io/aria/#aria-label
99
     */
100
    public function ariaLabel(string $value): self
101
    {
102
        $new = clone $this;
103
        $new->inputTagAttributes['aria-label'] = $value;
104
        return $new;
105
    }
106
107
    /**
108
     * Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus
109
     * at the same time.
110
     *
111
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus
112
     */
113
    public function autofocus(bool $value = true): self
114
    {
115
        $new = clone $this;
116
        $new->inputTagAttributes['autofocus'] = $value;
117
        return $new;
118
    }
119
120
    /**
121
     * The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential
122
     * keyboard navigation (usually with the Tab key, hence the name).
123
     *
124
     * It accepts an integer as a value, with different results depending on the integer's value:
125
     *
126
     * - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard
127
     *   navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
128
     *   with JavaScript.
129
     * - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is
130
     *   defined by the document's source order.
131
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
132
     *   defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after
133
     *   `tabindex="3"`.
134
     *
135
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex
136
     */
137
    public function tabIndex(?int $value): self
138
    {
139
        $new = clone $this;
140
        $new->inputTagAttributes['tabindex'] = $value;
141
        return $new;
142
    }
143
144 1
    protected function generateInput(): string
145
    {
146 1
        $value = $this->getAttributeValue();
147
148 1
        if (!is_numeric($value) && $value !== null) {
149
            throw new InvalidArgumentException('Number widget must be a numeric or null value.');
150
        }
151
152 1
        $tagAttributes = $this->getInputTagAttributes();
153
154 1
        return Html::input('number', $this->getInputName(), $value, $tagAttributes)->render();
155
    }
156
157 1
    protected function prepareContainerTagAttributes(array &$attributes): void
158
    {
159 1
        if ($this->hasFormModelAndAttribute()) {
160 1
            $this->addValidationClassToTagAttributes(
161
                $attributes,
162 1
                $this->getFormModel(),
163 1
                $this->getAttributeName(),
164
            );
165
        }
166
    }
167
168 1
    protected function prepareInputTagAttributes(array &$attributes): void
169
    {
170 1
        $this->preparePlaceholderInInputTagAttributes($attributes);
171
    }
172
}
173