Passed
Pull Request — master (#155)
by Wilmer
11:04
created

Password::readOnly()   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\InputAttributes;
10
use Yiisoft\Form\Widget\Attribute\ModelAttributes;
11
use Yiisoft\Html\Tag\Input;
12
use Yiisoft\Widget\Widget;
13
14
/**
15
 * The input element with a type attribute whose value is "password" represents a one-line plain-text edit control for
16
 * entering a password.
17
 *
18
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password
19
 */
20
final class Password extends Widget
21
{
22
    use InputAttributes;
23
    use ModelAttributes;
24
25
    /**
26
     * The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into
27
     * a tag input.
28
     *
29
     * If no maxlength is specified, or an invalid value is specified, the tag input has no maximum length.
30
     *
31
     * @param int $value
32
     *
33
     * @return static
34
     *
35
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.maxlength
36
     */
37 2
    public function maxlength(int $value): self
38
    {
39 2
        $new = clone $this;
40 2
        $new->attributes['maxlength'] = $value;
41 2
        return $new;
42
    }
43
44
    /**
45
     * The minimum number of characters (as UTF-16 code units) the user can enter into the text input.
46
     *
47
     * This must be a non-negative integer value smaller than or equal to the value specified by maxlength.
48
     * If no minlength is specified, or an invalid value is specified, the text input has no minimum length.
49
     *
50
     * @param int $value
51
     *
52
     * @return static
53
     *
54
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength
55
     */
56 2
    public function minlength(int $value): self
57
    {
58 2
        $new = clone $this;
59 2
        $new->attributes['minlength'] = $value;
60 2
        return $new;
61
    }
62
63
    /**
64
     * The pattern attribute, when specified, is a regular expression that the input's value must match in order for
65
     * the value to pass constraint validation. It must be a valid JavaScript regular expression, as used by the
66
     * RegExp type.
67
     *
68
     * @param string $value
69
     *
70
     * @return static
71
     *
72
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.pattern
73
     */
74 2
    public function pattern(string $value): self
75
    {
76 2
        $new = clone $this;
77 2
        $new->attributes['pattern'] = $value;
78 2
        return $new;
79
    }
80
81
    /**
82
     * It allows defining placeholder.
83
     *
84
     * @param string $value
85
     *
86
     * @return static
87
     *
88
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.placeholder
89
     */
90 2
    public function placeholder(string $value): self
91
    {
92 2
        $new = clone $this;
93 2
        $new->attributes['placeholder'] = $value;
94 2
        return $new;
95
    }
96
97
    /**
98
     * Generates a password input tag for the given form attribute.
99
     *
100
     * @return string the generated input tag,
101
     */
102 19
    protected function run(): string
103
    {
104 19
        $new = clone $this;
105
106
        /** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.value */
107 19
        $value = HtmlForm::getAttributeValue($new->getFormModel(), $new->attribute);
108
109 19
        if (!is_string($value) && null !== $value) {
110 2
            throw new InvalidArgumentException('Password widget must be a string or null value.');
111
        }
112
113 17
        $name = HtmlForm::getInputName($new->getFormModel(), $new->attribute);
114
115 17
        return Input::password($name, $value === '' ? null : $value)
116 17
            ->attributes($new->attributes)
117 17
            ->id($new->getId())
118 17
            ->render();
119
    }
120
}
121