Passed
Pull Request — master (#275)
by Sergei
02:49
created

DateTimeInputField::tabIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
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\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesInterface;
10
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesTrait;
11
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
12
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
13
use Yiisoft\Html\Html;
14
use Yiisoft\Validator\Rule\Required;
15
use Yiisoft\Validator\WhenInterface;
16
17
use function is_string;
18
19
abstract class DateTimeInputField extends InputField implements EnrichmentFromRulesInterface, ValidationClassInterface
20
{
21
    use EnrichmentFromRulesTrait;
22
    use ValidationClassTrait;
23
24
    /**
25
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-max
26
     */
27 3
    final public function max(?string $value): static
28
    {
29 3
        $new = clone $this;
30 3
        $new->inputAttributes['max'] = $value;
31 3
        return $new;
32
    }
33
34
    /**
35
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-min
36
     */
37 3
    final public function min(?string $value): static
38
    {
39 3
        $new = clone $this;
40 3
        $new->inputAttributes['min'] = $value;
41 3
        return $new;
42
    }
43
44
    /**
45
     * Identifies the element (or elements) that describes the object.
46
     *
47
     * @link https://w3c.github.io/aria/#aria-describedby
48
     */
49 2
    final public function ariaDescribedBy(?string $value): static
50
    {
51 2
        $new = clone $this;
52 2
        $new->inputAttributes['aria-describedby'] = $value;
53 2
        return $new;
54
    }
55
56
    /**
57
     * Defines a string value that labels the current element.
58
     *
59
     * @link https://w3c.github.io/aria/#aria-label
60
     */
61 2
    final public function ariaLabel(?string $value): static
62
    {
63 2
        $new = clone $this;
64 2
        $new->inputAttributes['aria-label'] = $value;
65 2
        return $new;
66
    }
67
68
    /**
69
     * Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus
70
     * at the same time.
71
     *
72
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus
73
     */
74 2
    final public function autofocus(bool $value = true): static
75
    {
76 2
        $new = clone $this;
77 2
        $new->inputAttributes['autofocus'] = $value;
78 2
        return $new;
79
    }
80
81
    /**
82
     * The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential
83
     * keyboard navigation (usually with the Tab key, hence the name).
84
     *
85
     * It accepts an integer as a value, with different results depending on the integer's value:
86
     *
87
     * - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard
88
     *   navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
89
     *   with JavaScript.
90
     * - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is
91
     *   defined by the document's source order.
92
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
93
     *   defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after
94
     *   `tabindex="3"`.
95
     *
96
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex
97
     */
98 2
    final public function tabIndex(?int $value): static
99
    {
100 2
        $new = clone $this;
101 2
        $new->inputAttributes['tabindex'] = $value;
102 2
        return $new;
103
    }
104
105
    /**
106
     * A boolean attribute that controls whether or not the user can edit the form control.
107
     *
108
     * @param bool $value Whether to allow the value to be edited by the user.
109
     *
110
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
111
     */
112 2
    final public function readonly(bool $value = true): static
113
    {
114 2
        $new = clone $this;
115 2
        $new->inputAttributes['readonly'] = $value;
116 2
        return $new;
117
    }
118
119
    /**
120
     * A boolean attribute. When specified, the element is required.
121
     *
122
     * @param bool $value Whether the control is required for form submission.
123
     *
124
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required
125
     */
126 2
    final public function required(bool $value = true): static
127
    {
128 2
        $new = clone $this;
129 2
        $new->inputAttributes['required'] = $value;
130 2
        return $new;
131
    }
132
133
    /**
134
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
135
     */
136 2
    final public function disabled(bool $disabled = true): static
137
    {
138 2
        $new = clone $this;
139 2
        $new->inputAttributes['disabled'] = $disabled;
140 2
        return $new;
141
    }
142
143
    /**
144
     * @psalm-suppress MixedAssignment,MixedArgument
145
     */
146 19
    protected function beforeRender(): void
147
    {
148 19
        parent::beforeRender();
149 19
        if ($this->enrichmentFromRules) {
150 2
            foreach ($this->getInputData()->getValidationRules() as $rule) {
151 2
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
152 1
                    continue;
153
                }
154
155 1
                if ($rule instanceof Required) {
156 1
                    $this->inputAttributes['required'] = true;
157
                }
158
            }
159
        }
160
    }
161
162 19
    final protected function generateInput(): string
163
    {
164 19
        $value = $this->getInputData()->getValue();
165
166 19
        if (!is_string($value) && $value !== null) {
167 1
            throw new InvalidArgumentException(
168 1
                (new ReflectionClass($this))->getShortName() .
169 1
                ' field requires a string or null value.'
170 1
            );
171
        }
172
173 18
        $inputAttributes = $this->getInputAttributes();
174
175 18
        return Html::input($this->getInputType(), $this->getInputData()->getName(), $value, $inputAttributes)->render();
176
    }
177
178
    abstract protected function getInputType(): string;
179
180 18
    protected function prepareContainerAttributes(array &$attributes): void
181
    {
182 18
        $this->addValidationClassToAttributes($attributes, $this->getInputData());
183
    }
184
185 18
    protected function prepareInputAttributes(array &$attributes): void
186
    {
187 18
        $this->addInputValidationClassToAttributes($attributes, $this->getInputData());
188
    }
189
}
190