Passed
Pull Request — master (#192)
by Alexander
02:31
created

AbstractDateTimeField::generateInput()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 2
nop 0
dl 0
loc 17
ccs 7
cts 9
cp 0.7778
crap 3.0987
rs 9.9666
c 1
b 0
f 0
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\Html\Html;
10
11
use function is_string;
12
13
abstract class AbstractDateTimeField extends AbstractField
14
{
15
    /**
16
     * The latest acceptable date.
17
     */
18 1
    public function max(?string $date): self
19
    {
20 1
        $new = clone $this;
21 1
        $new->inputTagAttributes['max'] = $date;
22 1
        return $new;
23
    }
24
25
    /**
26
     * The earliest acceptable date.
27
     */
28 1
    public function min(?string $date): self
29
    {
30 1
        $new = clone $this;
31 1
        $new->inputTagAttributes['min'] = $date;
32 1
        return $new;
33
    }
34
35
    /**
36
     * A boolean attribute that controls whether or not the user can edit the form control.
37
     *
38
     * @param bool $value Whether to allow the value to be edited by the user.
39
     *
40
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
41
     */
42
    public function readonly(bool $value = true): self
43
    {
44
        $new = clone $this;
45
        $new->inputTagAttributes['readonly'] = $value;
46
        return $new;
47
    }
48
49
    /**
50
     * A boolean attribute. When specified, the element is required.
51
     *
52
     * @param bool $value Whether the control is required for form submission.
53
     *
54
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required
55
     */
56
    public function required(bool $value = true): self
57
    {
58
        $new = clone $this;
59
        $new->inputTagAttributes['required'] = $value;
60
        return $new;
61
    }
62
63
    /**
64
     * Identifies the element (or elements) that describes the object.
65
     *
66
     * @link https://w3c.github.io/aria/#aria-describedby
67
     */
68
    public function ariaDescribedBy(string $value): self
69
    {
70
        $new = clone $this;
71
        $new->inputTagAttributes['aria-describedby'] = $value;
72
        return $new;
73
    }
74
75
    /**
76
     * Defines a string value that labels the current element.
77
     *
78
     * @link https://w3c.github.io/aria/#aria-label
79
     */
80
    public function ariaLabel(string $value): self
81
    {
82
        $new = clone $this;
83
        $new->inputTagAttributes['aria-label'] = $value;
84
        return $new;
85
    }
86
87
    /**
88
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the ID
89
     * attribute of a form element in the same document.
90
     *
91
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
92
     */
93
    public function form(string $value): self
94
    {
95
        $new = clone $this;
96
        $new->inputTagAttributes['form'] = $value;
97
        return $new;
98
    }
99
100 4
    final protected function generateInput(): string
101
    {
102 4
        $value = $this->getAttributeValue();
103
104 4
        if (!is_string($value) && $value !== null) {
105
            throw new InvalidArgumentException(
106
                (new ReflectionClass($this))->getShortName() .
107
                ' widget must be a string or null value.'
108
            );
109
        }
110
111 4
        $tagAttributes = $this->getInputTagAttributes();
112
113
        /** @psalm-suppress MixedArgumentTypeCoercion */
114 4
        return Html::input($this->getInputType(), $this->getInputName(), $value)
115 4
            ->attributes($tagAttributes)
116 4
            ->render();
117
    }
118
119
    abstract protected function getInputType(): string;
120
}
121