Passed
Pull Request — master (#160)
by Wilmer
02:47
created

WidgetAttributes::for()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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