Passed
Push — master ( ef6f5d...60f4bc )
by Alexander
02:52
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\Widget\Attribute\InputAttributes;
9
use Yiisoft\Form\Widget\Attribute\PlaceholderInterface;
10
use Yiisoft\Form\Widget\Validator\HasLengthInterface;
11
use Yiisoft\Form\Widget\Validator\MatchRegularInterface;
12
use Yiisoft\Html\Tag\Input;
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 InputAttributes implements HasLengthInterface, MatchRegularInterface, PlaceholderInterface
21
{
22 3
    public function maxlength(int $value): self
23
    {
24 3
        $new = clone $this;
25 3
        $new->attributes['maxlength'] = $value;
26 3
        return $new;
27
    }
28
29 3
    public function minlength(int $value): self
30
    {
31 3
        $new = clone $this;
32 3
        $new->attributes['minlength'] = $value;
33 3
        return $new;
34
    }
35
36 3
    public function pattern(string $value): self
37
    {
38 3
        $new = clone $this;
39 3
        $new->attributes['pattern'] = $value;
40 3
        return $new;
41
    }
42
43
    /**
44
     * It allows defining placeholder.
45
     *
46
     * @param string $value
47
     *
48
     * @return static
49
     *
50
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.placeholder
51
     */
52 2
    public function placeholder(string $value): self
53
    {
54 2
        $new = clone $this;
55 2
        $new->attributes['placeholder'] = $value;
56 2
        return $new;
57
    }
58
59
    /**
60
     * The number of options meant to be shown by the control represented by its element.
61
     *
62
     * @param int $size
63
     *
64
     * @return static
65
     *
66
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.password.html#input.password.attrs.size
67
     */
68 2
    public function size(int $size): self
69
    {
70 2
        $new = clone $this;
71 2
        $new->attributes['size'] = $size;
72 2
        return $new;
73
    }
74
75
    /**
76
     * Generates a password input tag for the given form attribute.
77
     *
78
     * @return string the generated input tag,
79
     */
80 43
    protected function run(): string
81
    {
82 43
        $attributes = $this->build($this->attributes);
83
84
        /** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.value */
85 43
        $value = $attributes['value'] ?? $this->getAttributeValue();
86 43
        unset($attributes['value']);
87
88 43
        if (null !== $value && !is_string($value)) {
89 2
            throw new InvalidArgumentException('Password widget must be a string or null value.');
90
        }
91
92 41
        return Input::tag()->type('password')->attributes($attributes)->value($value === '' ? null : $value)->render();
93
    }
94
}
95