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

WidgetAttributes::getFormModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
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 370
    public function encode(bool $value): self
27
    {
28 370
        $new = clone $this;
29 370
        $new->encode = $value;
30 370
        return $new;
31
    }
32
33
    /**
34
     * @return static
35
     */
36 699
    public function for(FormModelInterface $formModel, string $attribute): self
37
    {
38 699
        $new = clone $this;
39 699
        $new->formModel = $formModel;
40 699
        $new->attribute = $attribute;
41 699
        return $new;
42
    }
43
44 696
    protected function getAttribute(): string
45
    {
46 696
        if ($this->attribute === '') {
47
            throw new InvalidArgumentException('Attribute is not set.');
48
        }
49
50 696
        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 360
    public function getAttributePlaceHolder(): string
79
    {
80 360
        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 664
    protected function getAttributeValue()
89
    {
90 664
        return HtmlForm::getAttributeValue($this->getFormModel(), $this->getAttribute());
91
    }
92
93 406
    protected function getEncode(): bool
94
    {
95 406
        return $this->encode;
96
    }
97
98
    /**
99
     * Return FormModelInterface object.
100
     *
101
     * @return FormModelInterface
102
     */
103 696
    protected function getFormModel(): FormModelInterface
104
    {
105 696
        if ($this->formModel === null) {
106
            throw new InvalidArgumentException('Form model is not set.');
107
        }
108
109 696
        return $this->formModel;
110
    }
111
112
    /**
113
     * Generate input id attribute.
114
     */
115 658
    public function getInputId(): string
116
    {
117 658
        return HtmlForm::getInputId($this->getFormModel(), $this->getAttribute());
118
    }
119
120
    /**
121
     * Generate input name attribute.
122
     *
123
     * @return string
124
     */
125 630
    public function getInputName(): string
126
    {
127 630
        return HtmlForm::getInputName($this->getFormModel(), $this->getAttribute());
128
    }
129
130
    /**
131
     * Return if there is a validation error in the attribute.
132
     */
133 7
    public function hasError(): bool
134
    {
135 7
        return HtmlFormErrors::hasErrors($this->getFormModel(), $this->getAttribute());
136
    }
137
138
    /**
139
     * Return if the field was validated.
140
     *
141
     * @return bool
142
     */
143 4
    public function isValidated(): bool
144
    {
145 4
        return $this->getFormModel()->isValidated();
146
    }
147
}
148