Passed
Pull Request — master (#192)
by Alexander
05:47 queued 02:55
created

DateTimeInputField::min()   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 0
Metric Value
cc 1
eloc 3
c 0
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
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 1
    final public function max(?string $value): static
27
    {
28 1
        $new = clone $this;
29 1
        $new->inputTagAttributes['max'] = $value;
30 1
        return $new;
31
    }
32
33
    /**
34
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-min
35
     */
36 1
    final public function min(?string $value): static
37
    {
38 1
        $new = clone $this;
39 1
        $new->inputTagAttributes['min'] = $value;
40 1
        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
    final public function ariaDescribedBy(string $value): static
49
    {
50
        $new = clone $this;
51
        $new->inputTagAttributes['aria-describedby'] = $value;
52
        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
    final public function ariaLabel(string $value): static
61
    {
62
        $new = clone $this;
63
        $new->inputTagAttributes['aria-label'] = $value;
64
        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
    final public function autofocus(bool $value = true): static
74
    {
75
        $new = clone $this;
76
        $new->inputTagAttributes['autofocus'] = $value;
77
        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
    final public function tabIndex(?int $value): static
98
    {
99
        $new = clone $this;
100
        $new->inputTagAttributes['tabindex'] = $value;
101
        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
    final public function readonly(bool $value = true): static
112
    {
113
        $new = clone $this;
114
        $new->inputTagAttributes['readonly'] = $value;
115
        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
    final public function required(bool $value = true): static
126
    {
127
        $new = clone $this;
128
        $new->inputTagAttributes['required'] = $value;
129
        return $new;
130
    }
131
132
    /**
133
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
134
     */
135
    final public function disabled(bool $disabled = true): static
136
    {
137
        $new = clone $this;
138
        $new->inputTagAttributes['disabled'] = $disabled;
139
        return $new;
140
    }
141
142
    /**
143
     * @psalm-suppress MixedAssignment,MixedArgument Remove after fix https://github.com/yiisoft/validator/issues/225
144
     */
145 5
    protected function beforeRender(): void
146
    {
147 5
        parent::beforeRender();
148 5
        if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
149
            $rules = $this->getFormModel()->getRules()[$this->getAttributeName()] ?? [];
150
            foreach ($rules as $rule) {
151
                if ($rule instanceof Required) {
152
                    $this->inputTagAttributes['required'] = true;
153
                }
154
            }
155
        }
156
    }
157
158 5
    final protected function generateInput(): string
159
    {
160 5
        $value = $this->getAttributeValue();
161
162 5
        if (!is_string($value) && $value !== null) {
163
            throw new InvalidArgumentException(
164
                (new ReflectionClass($this))->getShortName() .
165
                ' widget must be a string or null value.'
166
            );
167
        }
168
169 5
        $tagAttributes = $this->getInputTagAttributes();
170
171 5
        return Html::input($this->getInputType(), $this->getInputName(), $value, $tagAttributes)->render();
172
    }
173
174
    abstract protected function getInputType(): string;
175
176 5
    protected function prepareContainerTagAttributes(array &$attributes): void
177
    {
178 5
        if ($this->hasFormModelAndAttribute()) {
179 5
            $this->addValidationClassToTagAttributes(
180
                $attributes,
181 5
                $this->getFormModel(),
182 5
                $this->getAttributeName(),
183
            );
184
        }
185
    }
186
}
187