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

Password   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 101
ccs 5
cts 30
cp 0.1666
rs 10
c 1
b 0
f 0
wmc 9

7 Methods

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