Passed
Pull Request — master (#160)
by Wilmer
10:36
created

Password::minlength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Helper\HtmlForm;
9
use Yiisoft\Form\Widget\Attribute\GlobalAttributes;
10
use Yiisoft\Html\Tag\Input;
11
12
/**
13
 * The input element with a type attribute whose value is "password" represents a one-line plain-text edit control for
14
 * entering a password.
15
 *
16
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password
17
 */
18
final class Password extends AbstractWidget
19
{
20
    use GlobalAttributes;
21
22
    /**
23
     * The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into
24
     * a tag input.
25
     *
26
     * If no maxlength is specified, or an invalid value is specified, the tag input has no maximum length.
27
     *
28
     * @param int $value
29
     *
30
     * @return static
31
     *
32
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.maxlength
33
     */
34 3
    public function maxlength(int $value): self
35
    {
36 3
        $new = clone $this;
37 3
        $new->attributes['maxlength'] = $value;
38 3
        return $new;
39
    }
40
41
    /**
42
     * The minimum number of characters (as UTF-16 code units) the user can enter into the text input.
43
     *
44
     * This must be a non-negative integer value smaller than or equal to the value specified by maxlength.
45
     * If no minlength is specified, or an invalid value is specified, the text input has no minimum length.
46
     *
47
     * @param int $value
48
     *
49
     * @return static
50
     *
51
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength
52
     */
53 3
    public function minlength(int $value): self
54
    {
55 3
        $new = clone $this;
56 3
        $new->attributes['minlength'] = $value;
57 3
        return $new;
58
    }
59
60
    /**
61
     * The pattern attribute, when specified, is a regular expression that the input's value must match in order for
62
     * the value to pass constraint validation. It must be a valid JavaScript regular expression, as used by the
63
     * RegExp type.
64
     *
65
     * @param string $value
66
     *
67
     * @return static
68
     *
69
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.pattern
70
     */
71 3
    public function pattern(string $value): self
72
    {
73 3
        $new = clone $this;
74 3
        $new->attributes['pattern'] = $value;
75 3
        return $new;
76
    }
77
78
    /**
79
     * It allows defining placeholder.
80
     *
81
     * @param string $value
82
     *
83
     * @return static
84
     *
85
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.placeholder
86
     */
87 3
    public function placeholder(string $value): self
88
    {
89 3
        $new = clone $this;
90 3
        $new->attributes['placeholder'] = $value;
91 3
        return $new;
92
    }
93
94
    /**
95
     * A Boolean attribute which, if present, means this field cannot be edited by the user.
96
     * Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value
97
     * property.
98
     *
99
     * @param bool $value
100
     *
101
     * @return static
102
     *
103
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.readonly
104
     */
105 3
    public function readOnly(bool $value = true): self
106
    {
107 3
        $new = clone $this;
108 3
        $new->attributes['readonly'] = $value;
109 3
        return $new;
110
    }
111
112
    /**
113
     * The number of options meant to be shown by the control represented by its element.
114
     *
115
     * @param int $size
116
     *
117
     * @return static
118
     *
119
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.size
120
     */
121 3
    public function size(int $size): self
122
    {
123 3
        $new = clone $this;
124 3
        $new->attributes['size'] = $size;
125 3
        return $new;
126
    }
127
128
    /**
129
     * Generates a password input tag for the given form attribute.
130
     *
131
     * @return string the generated input tag,
132
     */
133 29
    protected function run(): string
134
    {
135 29
        $new = clone $this;
136
137
        /** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.value */
138 29
        $value = HtmlForm::getAttributeValue($new->getFormModel(), $new->getAttribute());
139
140 29
        if (!is_string($value) && null !== $value) {
141 3
            throw new InvalidArgumentException('Password widget must be a string or null value.');
142
        }
143
144 26
        return Input::password($new->getName(), $value === '' ? null : $value)
145 26
            ->attributes($new->attributes)
146 26
            ->id($new->getId())
147 26
            ->render();
148
    }
149
}
150