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

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