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

Telephone::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 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
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 "tel" represents a one-line plain-text edit control for
14
 * entering a telephone number.
15
 *
16
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.tel.html#input.tel
17
 */
18
final class Telephone 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 Positive integer.
29
     *
30
     * @return static
31
     *
32
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.tel.html#input.tel.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.tel.html#input.tel.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.tel.html#input.tel.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
     * The readonly attribute is a boolean attribute that controls whether the user can edit the form control.
96
     * When specified, the element is not mutable.
97
     *
98
     * @return static
99
     *
100
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.tel.html#input.tel.attrs.readonly
101
     */
102 3
    public function readonly(): self
103
    {
104 3
        $new = clone $this;
105 3
        $new->attributes['readonly'] = true;
106 3
        return $new;
107
    }
108
109
    /**
110
     * The height of the text input.
111
     *
112
     * Default value is 4.
113
     *
114
     * @param int $value
115
     *
116
     * @return static
117
     *
118
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.tel.html#input.tel.attrs.size
119
     */
120 3
    public function size(int $value = 4): self
121
    {
122 3
        $new = clone $this;
123 3
        $new->attributes['size'] = $value;
124 3
        return $new;
125
    }
126
127
    /**
128
     * @return string the generated input tag.
129
     */
130 26
    protected function run(): string
131
    {
132 26
        $new = clone $this;
133
134
        /** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.tel.html#input.tel.attrs.value */
135 26
        $value = HtmlForm::getAttributeValue($new->getFormModel(), $new->getAttribute());
136
137 26
        if (!is_string($value) && !is_int($value) && null !== $value) {
138 3
            throw new InvalidArgumentException('Telephone widget must be a string, numeric or null.');
139
        }
140
141 23
        return Input::tag()
142 23
            ->type('tel')
143 23
            ->attributes($new->attributes)
144 23
            ->id($new->getId())
145 23
            ->name($new->getName())
146 23
            ->value($value === '' ? null : $value)
147 23
            ->render();
148
    }
149
}
150