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

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