Passed
Pull Request — master (#166)
by Wilmer
02:22
created

WidgetAttributes::hasError()   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 Stringable;
8
use Yiisoft\Form\Exception\AttributeNotSetException;
9
use Yiisoft\Form\Exception\FormModelNotSetException;
10
use Yiisoft\Form\FormModelInterface;
11
use Yiisoft\Form\Helper\HtmlForm;
12
use Yiisoft\Form\Helper\HtmlFormErrors;
13
14
abstract class WidgetAttributes extends GlobalAttributes
15
{
16
    private string $attribute = '';
17
    private ?FormModelInterface $formModel = null;
18
19
    /**
20
     * @return static
21
     */
22 704
    public function for(FormModelInterface $formModel, string $attribute): self
23
    {
24 704
        $new = clone $this;
25 704
        $new->formModel = $formModel;
26 704
        $new->attribute = $attribute;
27 704
        return $new;
28
    }
29
30 701
    protected function getAttribute(): string
31
    {
32 701
        if ($this->attribute === '') {
33 1
            throw new AttributeNotSetException();
34
        }
35
36 700
        return $this->attribute;
37
    }
38
39
    /**
40
     * Generate hint attribute.
41
     *
42
     * @return string
43
     */
44
    protected function getAttributeHint(): string
45
    {
46
        return HtmlForm::getAttributeHint($this->getFormModel(), $this->getAttribute());
47
    }
48
49
    /**
50
     * Generate label attribute.
51
     *
52
     * @return string
53
     */
54 62
    protected function getAttributeLabel(): string
55
    {
56 62
        return HtmlForm::getAttributeLabel($this->getFormModel(), $this->getAttribute());
57
    }
58
59
    /**
60
     * Generate placeholder attribute.
61
     *
62
     * @return string
63
     */
64 364
    public function getAttributePlaceHolder(): string
65
    {
66 364
        return HtmlForm::getAttributePlaceHolder($this->getFormModel(), $this->getAttribute());
67
    }
68
69
    /**
70
     * Return value of attribute.
71
     *
72
     * @return bool|float|int|iterable|object|string|Stringable|null
73
     */
74 666
    protected function getAttributeValue()
75
    {
76 666
        return HtmlForm::getAttributeValue($this->getFormModel(), $this->getAttribute());
77
    }
78
79
    /**
80
     * Return FormModelInterface object.
81
     *
82
     * @return FormModelInterface
83
     */
84 701
    protected function getFormModel(): FormModelInterface
85
    {
86 701
        if ($this->formModel === null) {
87 1
            throw new FormModelNotSetException();
88
        }
89
90 700
        return $this->formModel;
91
    }
92
93
    /**
94
     * Generate input id attribute.
95
     */
96 662
    public function getInputId(): string
97
    {
98 662
        return HtmlForm::getInputId($this->getFormModel(), $this->getAttribute());
99
    }
100
101
    /**
102
     * Generate input name attribute.
103
     *
104
     * @return string
105
     */
106 634
    public function getInputName(): string
107
    {
108 634
        return HtmlForm::getInputName($this->getFormModel(), $this->getAttribute());
109
    }
110
111
    /**
112
     * Return if there is a validation error in the attribute.
113
     */
114 7
    public function hasError(): bool
115
    {
116 7
        return HtmlFormErrors::hasErrors($this->getFormModel(), $this->getAttribute());
117
    }
118
119
    /**
120
     * Return if the field was validated.
121
     *
122
     * @return bool
123
     */
124 5
    public function isValidated(): bool
125
    {
126 5
        return $this->getFormModel()->isValidated();
127
    }
128
}
129