Passed
Push — master ( 94344c...86ce1f )
by Alexander
07:20 queued 04:31
created

DateTimeInputField::prepareInputAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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