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

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