Passed
Pull Request — master (#192)
by Alexander
06:02 queued 02:54
created

Email   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 14.71%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 115
ccs 5
cts 34
cp 0.1471
rs 10
c 2
b 0
f 0
wmc 10

8 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 11 3
A pattern() 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\AbstractInputField;
9
use Yiisoft\Form\Field\Base\PlaceholderTrait;
10
use Yiisoft\Html\Html;
11
12
use function is_string;
13
14
final class Email extends AbstractInputField
15
{
16
    use PlaceholderTrait;
17
18
    /**
19
     * Maximum length of value.
20
     *
21
     * @param int $value A limit on the number of characters a user can input.
22
     *
23
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-maxlength
24
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength
25
     */
26
    public function maxlength(int $value): self
27
    {
28
        $new = clone $this;
29
        $new->inputTagAttributes['maxlength'] = $value;
30
        return $new;
31
    }
32
33
    /**
34
     * Minimum length of value.
35
     *
36
     * @param int $value A lower bound on the number of characters a user can input.
37
     *
38
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength
39
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-minlength
40
     */
41
    public function minlength(int $value): self
42
    {
43
        $new = clone $this;
44
        $new->inputTagAttributes['minlength'] = $value;
45
        return $new;
46
    }
47
48
    /**
49
     * Allow to specify more than one value.
50
     *
51
     * @param bool $value Whether the user is to be allowed to specify more than one value.
52
     *
53
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-multiple
54
     */
55
    public function multiple(bool $value = true): self
56
    {
57
        $new = clone $this;
58
        $new->inputTagAttributes['multiple'] = $value;
59
        return $new;
60
    }
61
62
    /**
63
     * Pattern to be matched by the form control's value.
64
     *
65
     * @param string $value A regular expression against which the control's value.
66
     *
67
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-pattern
68
     */
69
    public function pattern(string $value): self
70
    {
71
        $new = clone $this;
72
        $new->inputTagAttributes['pattern'] = $value;
73
        return $new;
74
    }
75
76
    /**
77
     * A boolean attribute that controls whether or not the user can edit the form control.
78
     *
79
     * @param bool $value Whether to allow the value to be edited by the user.
80
     *
81
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
82
     */
83
    public function readonly(bool $value = true): self
84
    {
85
        $new = clone $this;
86
        $new->inputTagAttributes['readonly'] = $value;
87
        return $new;
88
    }
89
90
    /**
91
     * A boolean attribute. When specified, the element is required.
92
     *
93
     * @param bool $value Whether the control is required for form submission.
94
     *
95
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required
96
     */
97
    public function required(bool $value = true): self
98
    {
99
        $new = clone $this;
100
        $new->inputTagAttributes['required'] = $value;
101
        return $new;
102
    }
103
104
    /**
105
     * The size of the control.
106
     *
107
     * @param int $value The number of characters that allow the user to see while editing the element's value.
108
     *
109
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-size
110
     */
111
    public function size(int $value): self
112
    {
113
        $new = clone $this;
114
        $new->inputTagAttributes['size'] = $value;
115
        return $new;
116
    }
117
118 1
    protected function generateInput(): string
119
    {
120 1
        $value = $this->getAttributeValue();
121
122 1
        if (!is_string($value) && $value !== null) {
123
            throw new InvalidArgumentException('Email widget must be a string or null value.');
124
        }
125
126 1
        $tagAttributes = $this->getInputTagAttributes();
127
128 1
        return Html::input('email', $this->getInputName(), $value, $tagAttributes)->render();
129
    }
130
}
131