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

DateTimeInputField   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 37.5%

Importance

Changes 0
Metric Value
eloc 40
c 0
b 0
f 0
dl 0
loc 148
ccs 18
cts 48
cp 0.375
rs 10
wmc 14

11 Methods

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