Test Failed
Pull Request — master (#159)
by Alexander
02:49
created

InputText   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 33
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateInput() 0 14 3
A inputTagAttributes() 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\AbstractField;
9
use Yiisoft\Form\Field\Base\PlaceholderTrait;
10
use Yiisoft\Html\Html;
11
12
use function is_string;
13
14
/**
15
 * @psalm-import-type HtmlAttributes from Html
16
 */
17
final class InputText extends AbstractField
18
{
19
    use PlaceholderTrait;
20
21
    /**
22
     * @psalm-var HtmlAttributes
23
     */
24
    private array $inputTagAttributes = [];
25
26
    /**
27
     * @psalm-param HtmlAttributes $attributes
28
     */
29
    public function inputTagAttributes(array $attributes): self
30
    {
31
        $new = clone $this;
32
        $new->inputTagAttributes = $attributes;
33
        return $new;
34
    }
35
36
    protected function generateInput(): string
37
    {
38
        $value = $this->getAttributeValue();
39
40
        if (!is_string($value) && $value !== null) {
41
            throw new InvalidArgumentException('Text widget must be a string or null value.');
42
        }
43
44
        $tag = Html::textInput($this->getInputName(), $value, $this->inputTagAttributes);
45
46
        $tag = $this->prepareIdInInputTag($tag);
47
        $tag = $this->preparePlaceholderInInputTag($tag);
48
49
        return $tag->render();
50
    }
51
}
52