Passed
Pull Request — master (#160)
by Wilmer
03:13
created

Text::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 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
 * Generates a text input tag for the given form attribute.
16
 *
17
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text
18
 */
19
final class Text extends InputAttributes implements HasLengthInterface, MatchRegularInterface, PlaceholderInterface
20
{
21
    /**
22
     * Enables submission of a value for the directionality of the element, and gives the name of the field that
23
     * contains that value.
24
     *
25
     * @param string $value Any string that is not empty.
26
     *
27
     * @return static
28
     *
29
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.dirname
30
     */
31 5
    public function dirname(string $value): self
32
    {
33 5
        if (empty($value)) {
34 2
            throw new InvalidArgumentException('The value cannot be empty.');
35
        }
36
37 3
        $new = clone $this;
38 3
        $new->attributes['dirname'] = $value;
39 3
        return $new;
40
    }
41
42 3
    public function maxlength(int $value): self
43
    {
44 3
        $new = clone $this;
45 3
        $new->attributes['maxlength'] = $value;
46 3
        return $new;
47
    }
48
49 2
    public function minlength(int $value): self
50
    {
51 2
        $new = clone $this;
52 2
        $new->attributes['minlength'] = $value;
53 2
        return $new;
54
    }
55
56
    /**
57
     * It allows defining placeholder.
58
     *
59
     * @param string $value
60
     *
61
     * @return static
62
     *
63
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.placeholder
64
     */
65 2
    public function placeholder(string $value): self
66
    {
67 2
        $new = clone $this;
68 2
        $new->attributes['placeholder'] = $value;
69 2
        return $new;
70
    }
71
72 3
    public function pattern(string $value): self
73
    {
74 3
        $new = clone $this;
75 3
        $new->attributes['pattern'] = $value;
76 3
        return $new;
77
    }
78
79
    /**
80
     * A Boolean attribute which, if present, means this field cannot be edited by the user.
81
     * Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value
82
     * property.
83
     *
84
     * @param bool $value
85
     *
86
     * @return static
87
     *
88
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.readonly
89
     */
90 2
    public function readonly(bool $value = true): self
91
    {
92 2
        $new = clone $this;
93 2
        $new->attributes['readonly'] = $value;
94 2
        return $new;
95
    }
96
97
    /**
98
     * The height of the input with multiple is true.
99
     *
100
     * @param int $value
101
     *
102
     * @return static
103
     *
104
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.size
105
     */
106 3
    public function size(int $value): self
107
    {
108 3
        $new = clone $this;
109 3
        $new->attributes['size'] = $value;
110 3
        return $new;
111
    }
112
113
    /**
114
     * @return string the generated input tag.
115
     */
116 81
    protected function run(): string
117
    {
118 81
        $attributes = $this->build($this->attributes);
119
120
        /** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.value */
121 81
        $value = $attributes['value'] ?? $this->getAttributeValue();
122 81
        unset($attributes['value']);
123
124 81
        if (null !== $value && !is_string($value)) {
125 2
            throw new InvalidArgumentException('Text widget must be a string or null value.');
126
        }
127
128 79
        return Input::tag()->type('text')->attributes($attributes)->value($value === '' ? null : $value)->render();
129
    }
130
}
131