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

Email   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 14.58%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 155
ccs 7
cts 48
cp 0.1458
rs 10
c 1
b 0
f 0
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A size() 0 5 1
A required() 0 5 1
A readonly() 0 5 1
A minlength() 0 5 1
A multiple() 0 5 1
A generateInput() 0 14 3
A pattern() 0 5 1
A ariaLabel() 0 5 1
A ariaDescribedBy() 0 5 1
A form() 0 5 1
A maxlength() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Field\Base\AbstractField;
9
10
use Yiisoft\Form\Field\Base\PlaceholderTrait;
11
use Yiisoft\Html\Html;
12
13
use function is_string;
14
15
final class Email extends AbstractField
16
{
17
    use PlaceholderTrait;
18
19
    /**
20
     * A boolean attribute that controls whether or not the user can edit the form control.
21
     *
22
     * @param bool $value Whether to allow the value to be edited by the user.
23
     *
24
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
25
     */
26
    public function readonly(bool $value = true): self
27
    {
28
        $new = clone $this;
29
        $new->inputTagAttributes['readonly'] = $value;
30
        return $new;
31
    }
32
33
    /**
34
     * A boolean attribute. When specified, the element is required.
35
     *
36
     * @param bool $value Whether the control is required for form submission.
37
     *
38
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required
39
     */
40
    public function required(bool $value = true): self
41
    {
42
        $new = clone $this;
43
        $new->inputTagAttributes['required'] = $value;
44
        return $new;
45
    }
46
47
    /**
48
     * Allow to specify more than one value.
49
     *
50
     * @param bool $value Whether the user is to be allowed to specify more than one value.
51
     *
52
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-multiple
53
     */
54
    public function multiple(bool $value = true): self
55
    {
56
        $new = clone $this;
57
        $new->inputTagAttributes['multiple'] = $value;
58
        return $new;
59
    }
60
61
    /**
62
     * Maximum length of value.
63
     *
64
     * @param int $value A limit on the number of characters a user can input.
65
     *
66
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-maxlength
67
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength
68
     */
69
    public function maxlength(int $value): self
70
    {
71
        $new = clone $this;
72
        $new->inputTagAttributes['maxlength'] = $value;
73
        return $new;
74
    }
75
76
    /**
77
     * Minimum length of value.
78
     *
79
     * @param int $value A lower bound on the number of characters a user can input.
80
     *
81
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength
82
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-minlength
83
     */
84
    public function minlength(int $value): self
85
    {
86
        $new = clone $this;
87
        $new->inputTagAttributes['minlength'] = $value;
88
        return $new;
89
    }
90
91
    /**
92
     * Pattern to be matched by the form control's value.
93
     *
94
     * @param string $value A regular expression against which the control's value.
95
     *
96
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-pattern
97
     */
98
    public function pattern(string $value): self
99
    {
100
        $new = clone $this;
101
        $new->inputTagAttributes['pattern'] = $value;
102
        return $new;
103
    }
104
105
    /**
106
     * The size of the control.
107
     *
108
     * @param int $value The number of characters that allow the user to see while editing the element's value.
109
     *
110
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-size
111
     */
112
    public function size(int $value): self
113
    {
114
        $new = clone $this;
115
        $new->inputTagAttributes['size'] = $value;
116
        return $new;
117
    }
118
119
    /**
120
     * Identifies the element (or elements) that describes the object.
121
     *
122
     * @link https://w3c.github.io/aria/#aria-describedby
123
     */
124
    public function ariaDescribedBy(string $value): self
125
    {
126
        $new = clone $this;
127
        $new->inputTagAttributes['aria-describedby'] = $value;
128
        return $new;
129
    }
130
131
    /**
132
     * Defines a string value that labels the current element.
133
     *
134
     * @link https://w3c.github.io/aria/#aria-label
135
     */
136
    public function ariaLabel(string $value): self
137
    {
138
        $new = clone $this;
139
        $new->inputTagAttributes['aria-label'] = $value;
140
        return $new;
141
    }
142
143
    /**
144
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the ID
145
     * attribute of a form element in the same document.
146
     *
147
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
148
     */
149
    public function form(string $value): self
150
    {
151
        $new = clone $this;
152
        $new->inputTagAttributes['form'] = $value;
153
        return $new;
154
    }
155
156 1
    protected function generateInput(): string
157
    {
158 1
        $value = $this->getAttributeValue();
159
160 1
        if (!is_string($value) && $value !== null) {
161
            throw new InvalidArgumentException('Email widget must be a string or null value.');
162
        }
163
164 1
        $tagAttributes = $this->getInputTagAttributes();
165
166
        /** @psalm-suppress MixedArgumentTypeCoercion */
167 1
        return Html::input('email', $this->getInputName(), $value)
168 1
            ->attributes($tagAttributes)
169 1
            ->render();
170
    }
171
}
172