Passed
Pull Request — master (#192)
by Sergei
02:16
created

Text::maxlength()   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 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\Field;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Field\Base\InputField;
9
use Yiisoft\Form\Field\Base\PlaceholderTrait;
10
use Yiisoft\Form\Field\Base\ReadonlyTrait;
11
use Yiisoft\Form\Field\Base\RequiredTrait;
12
use Yiisoft\Html\Html;
13
14
use function is_string;
15
16
/**
17
 * Generates a text input tag for the given form attribute.
18
 *
19
 * @link https://html.spec.whatwg.org/multipage/input.html#text-(type=text)-state-and-search-state-(type=search)
20
 */
21
final class Text extends InputField
22
{
23
    use PlaceholderTrait;
24
    use ReadonlyTrait;
25
    use RequiredTrait;
26
27
    /**
28
     * Name of form control to use for sending the element's directionality in form submission
29
     *
30
     * @param string $value Any string that is not empty.
31
     *
32
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-dirname
33
     */
34 3
    public function dirname(string $value): self
35
    {
36 3
        if (empty($value)) {
37 1
            throw new InvalidArgumentException('The value cannot be empty.');
38
        }
39
40 2
        $new = clone $this;
41 2
        $new->inputTagAttributes['dirname'] = $value;
42 2
        return $new;
43
    }
44
45
    /**
46
     * Maximum length of value.
47
     *
48
     * @param int $value A limit on the number of characters a user can input.
49
     *
50
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-maxlength
51
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength
52
     */
53 2
    public function maxlength(int $value): self
54
    {
55 2
        $new = clone $this;
56 2
        $new->inputTagAttributes['maxlength'] = $value;
57 2
        return $new;
58
    }
59
60
    /**
61
     * Minimum length of value.
62
     *
63
     * @param int $value A lower bound on the number of characters a user can input.
64
     *
65
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength
66
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-minlength
67
     */
68 2
    public function minlength(int $value): self
69
    {
70 2
        $new = clone $this;
71 2
        $new->inputTagAttributes['minlength'] = $value;
72 2
        return $new;
73
    }
74
75
    /**
76
     * Pattern to be matched by the form control's value.
77
     *
78
     * @param string $value A regular expression against which the control's value.
79
     *
80
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-pattern
81
     */
82 2
    public function pattern(string $value): self
83
    {
84 2
        $new = clone $this;
85 2
        $new->inputTagAttributes['pattern'] = $value;
86 2
        return $new;
87
    }
88
89
    /**
90
     * The size of the control.
91
     *
92
     * @param int $value The number of characters that allow the user to see while editing the element's value.
93
     *
94
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-size
95
     */
96 2
    public function size(int $value): self
97
    {
98 2
        $new = clone $this;
99 2
        $new->inputTagAttributes['size'] = $value;
100 2
        return $new;
101
    }
102
103 34
    protected function generateInput(): string
104
    {
105 34
        $value = $this->getAttributeValue();
106
107 34
        if (!is_string($value) && $value !== null) {
108 1
            throw new InvalidArgumentException('Text widget must be a string or null value.');
109
        }
110
111 33
        $tagAttributes = $this->getInputTagAttributes();
112
113
        /** @psalm-suppress MixedArgumentTypeCoercion */
114 33
        return Html::textInput($this->getInputName(), $value, $tagAttributes)->render();
115
    }
116
}
117