Passed
Push — master ( ef6f5d...60f4bc )
by Alexander
02:52
created

WidgetAttributes::getAttributePlaceHolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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 ?FormModelInterface $formModel = null;
17
18
    /**
19
     * @return static
20
     */
21 700
    public function for(FormModelInterface $formModel, string $attribute): self
22
    {
23 700
        $new = clone $this;
24 700
        $new->formModel = $formModel;
25 700
        $new->attribute = $attribute;
26 700
        return $new;
27
    }
28
29 697
    protected function getAttribute(): string
30
    {
31 697
        if ($this->attribute === '') {
32
            throw new InvalidArgumentException('Attribute is not set.');
33
        }
34
35 697
        return $this->attribute;
36
    }
37
38
    /**
39
     * Generate hint attribute.
40
     *
41
     * @return string
42
     */
43
    protected function getAttributeHint(): string
44
    {
45
        return HtmlForm::getAttributeHint($this->getFormModel(), $this->getAttribute());
46
    }
47
48
    /**
49
     * Generate label attribute.
50
     *
51
     * @return string
52
     */
53 62
    protected function getAttributeLabel(): string
54
    {
55 62
        return HtmlForm::getAttributeLabel($this->getFormModel(), $this->getAttribute());
56
    }
57
58
    /**
59
     * Generate placeholder attribute.
60
     *
61
     * @return string
62
     */
63 361
    public function getAttributePlaceHolder(): string
64
    {
65 361
        return HtmlForm::getAttributePlaceHolder($this->getFormModel(), $this->getAttribute());
66
    }
67
68
    /**
69
     * Return value of attribute.
70
     *
71
     * @return bool|float|int|iterable|object|string|Stringable|null
72
     */
73 665
    protected function getAttributeValue()
74
    {
75 665
        return HtmlForm::getAttributeValue($this->getFormModel(), $this->getAttribute());
76
    }
77
78
    /**
79
     * Return FormModelInterface object.
80
     *
81
     * @return FormModelInterface
82
     */
83 697
    protected function getFormModel(): FormModelInterface
84
    {
85 697
        if ($this->formModel === null) {
86
            throw new InvalidArgumentException('Form model is not set.');
87
        }
88
89 697
        return $this->formModel;
90
    }
91
92
    /**
93
     * Generate input id attribute.
94
     */
95 659
    public function getInputId(): string
96
    {
97 659
        return HtmlForm::getInputId($this->getFormModel(), $this->getAttribute());
98
    }
99
100
    /**
101
     * Generate input name attribute.
102
     *
103
     * @return string
104
     */
105 631
    public function getInputName(): string
106
    {
107 631
        return HtmlForm::getInputName($this->getFormModel(), $this->getAttribute());
108
    }
109
110
    /**
111
     * Return if there is a validation error in the attribute.
112
     */
113 7
    public function hasError(): bool
114
    {
115 7
        return HtmlFormErrors::hasErrors($this->getFormModel(), $this->getAttribute());
116
    }
117
118
    /**
119
     * Return if the field was validated.
120
     *
121
     * @return bool
122
     */
123 5
    public function isValidated(): bool
124
    {
125 5
        return $this->getFormModel()->isValidated();
126
    }
127
}
128