Test Failed
Pull Request — master (#160)
by Wilmer
02:22
created

WidgetAttributes   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 135
rs 10
wmc 15

13 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidated() 0 3 1
A getFormModel() 0 7 2
A encode() 0 5 1
A getAttributeLabel() 0 3 1
A getInputId() 0 3 1
A getEncode() 0 3 1
A for() 0 6 1
A getAttributeValue() 0 3 1
A getAttributeHint() 0 3 1
A getInputName() 0 3 1
A getAttributePlaceHolder() 0 3 1
A getAttribute() 0 7 2
A hasError() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget\Attribute;
6
7
use InvalidArgumentException;
8
use Stringable;
9
use Yiisoft\Form\FormModelInterface;
10
use Yiisoft\Form\Helper\HtmlForm;
11
use Yiisoft\Form\Helper\HtmlFormErrors;
12
use Yiisoft\Widget\Widget;
13
14
abstract class WidgetAttributes extends Widget
15
{
16
    use GlobalAttributes;
17
18
    private string $attribute = '';
19
    private bool $encode = true;
20
    private ?FormModelInterface $formModel = null;
21
22
    /**
23
     * Whether content should be HTML-encoded.
24
     *
25
     * @param bool $value
26
     *
27
     * @return static
28
     */
29
    public function encode(bool $value): self
30
    {
31
        $new = clone $this;
32
        $new->encode = $value;
33
        return $new;
34
    }
35
36
    /**
37
     * @return static
38
     */
39
    public function for(FormModelInterface $formModel, string $attribute): self
40
    {
41
        $new = clone $this;
42
        $new->formModel = $formModel;
43
        $new->attribute = $attribute;
44
        return $new;
45
    }
46
47
    protected function getAttribute(): string
48
    {
49
        if ($this->attribute === '') {
50
            throw new InvalidArgumentException('Attribute is not set.');
51
        }
52
53
        return $this->attribute;
54
    }
55
56
    /**
57
     * Generate hint attribute.
58
     *
59
     * @return string
60
     */
61
    protected function getAttributeHint(): string
62
    {
63
        return HtmlForm::getAttributeHint($this->getFormModel(), $this->getAttribute());
64
    }
65
66
    /**
67
     * Generate label attribute.
68
     *
69
     * @return string
70
     */
71
    protected function getAttributeLabel(): string
72
    {
73
        return HtmlForm::getAttributeLabel($this->getFormModel(), $this->getAttribute());
74
    }
75
76
    /**
77
     * Generate placeholder attribute.
78
     *
79
     * @return string
80
     */
81
    public function getAttributePlaceHolder(): string
82
    {
83
        return HtmlForm::getAttributePlaceHolder($this->getFormModel(), $this->getAttribute());
84
    }
85
86
    /**
87
     * Return value of attribute.
88
     *
89
     * @return bool|float|int|iterable|object|string|Stringable|null
90
     */
91
    protected function getAttributeValue()
92
    {
93
        return HtmlForm::getAttributeValue($this->getFormModel(), $this->getAttribute());
94
    }
95
96
    protected function getEncode(): bool
97
    {
98
        return $this->encode;
99
    }
100
101
    /**
102
     * Return FormModelInterface object.
103
     *
104
     * @return FormModelInterface
105
     */
106
    protected function getFormModel(): FormModelInterface
107
    {
108
        if ($this->formModel === null) {
109
            throw new InvalidArgumentException('Form model is not set.');
110
        }
111
112
        return $this->formModel;
113
    }
114
115
    /**
116
     * Generate input id attribute.
117
     */
118
    public function getInputId(): string
119
    {
120
        return HtmlForm::getInputId($this->getFormModel(), $this->getAttribute());
121
    }
122
123
    /**
124
     * Generate input name attribute.
125
     *
126
     * @return string
127
     */
128
    public function getInputName(): string
129
    {
130
        return HtmlForm::getInputName($this->getFormModel(), $this->getAttribute());
131
    }
132
133
    /**
134
     * Return if there is a validation error in the attribute.
135
     */
136
    public function hasError(): bool
137
    {
138
        return HtmlFormErrors::hasErrors($this->getFormModel(), $this->getAttribute());
139
    }
140
141
    /**
142
     * Return if the field was validated.
143
     *
144
     * @return bool
145
     */
146
    public function isValidated(): bool
147
    {
148
        return $this->getFormModel()->isValidated();
149
    }
150
}
151