Passed
Push — master ( 79bb78...087da0 )
by Alexander
02:53
created

DateTimeInputField::beforeRender()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 8
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 7
rs 8.8333
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\BeforeValidationInterface;
15
use Yiisoft\Validator\Rule\Required;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\Rule\Required was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 Remove after fix https://github.com/yiisoft/validator/issues/225
145
     */
146 17
    protected function beforeRender(): void
147
    {
148 17
        parent::beforeRender();
149 17
        if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
150 2
            $rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
151 2
            foreach ($rules as $rule) {
152 2
                if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
153 1
                    continue;
154
                }
155
156 1
                if ($rule instanceof Required) {
157 1
                    $this->inputAttributes['required'] = true;
158
                }
159
            }
160
        }
161
    }
162
163 17
    final protected function generateInput(): string
164
    {
165 17
        $value = $this->getFormAttributeValue();
166
167 17
        if (!is_string($value) && $value !== null) {
168 1
            throw new InvalidArgumentException(
169 1
                (new ReflectionClass($this))->getShortName() .
170
                ' field requires a string or null value.'
171
            );
172
        }
173
174 16
        $inputAttributes = $this->getInputAttributes();
175
176 16
        return Html::input($this->getInputType(), $this->getInputName(), $value, $inputAttributes)->render();
177
    }
178
179
    abstract protected function getInputType(): string;
180
181 16
    protected function prepareContainerAttributes(array &$attributes): void
182
    {
183 16
        if ($this->hasFormModelAndAttribute()) {
184 16
            $this->addValidationClassToAttributes(
185
                $attributes,
186 16
                $this->getFormModel(),
187 16
                $this->getFormAttributeName(),
188
            );
189
        }
190
    }
191
192 16
    protected function prepareInputAttributes(array &$attributes): void
193
    {
194 16
        if ($this->hasFormModelAndAttribute()) {
195 16
            $this->addInputValidationClassToAttributes(
196
                $attributes,
197 16
                $this->getFormModel(),
198 16
                $this->getFormAttributeName(),
199
            );
200
        }
201
    }
202
}
203