Passed
Pull Request — master (#192)
by Alexander
03:02
created

FormAttributeTrait::getAttributeHint()   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
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\FormModelInterface;
9
use Yiisoft\Form\Helper\HtmlForm;
10
11
trait FormAttributeTrait
12
{
13
    private ?FormModelInterface $formModel = null;
14
    private string $attribute = '';
15
16 360
    final public function attribute(FormModelInterface $formModel, string $attribute): static
17
    {
18 360
        $new = clone $this;
19 360
        $new->formModel = $formModel;
20 360
        $new->attribute = $attribute;
21 360
        return $new;
22
    }
23
24 348
    final protected function getFormModel(): FormModelInterface
25
    {
26 348
        if ($this->formModel === null) {
27 1
            throw new InvalidArgumentException('Form model is not set.');
28
        }
29
30 347
        return $this->formModel;
31
    }
32
33 401
    final protected function hasFormModelAndAttribute(): bool
34
    {
35 401
        return $this->formModel !== null && $this->attribute !== '';
36
    }
37
38 311
    final protected function getAttributeName(): string
39
    {
40 311
        return HtmlForm::getAttributeName($this->getFormModel(), $this->attribute);
41
    }
42
43 318
    final protected function getAttributeValue(): mixed
44
    {
45 318
        return HtmlForm::getAttributeValue($this->getFormModel(), $this->attribute);
46
    }
47
48 133
    final protected function getAttributeLabel(): string
49
    {
50 133
        return HtmlForm::getAttributeLabel($this->getFormModel(), $this->attribute);
51
    }
52
53 309
    final protected function getAttributeHint(): string
54
    {
55 309
        return HtmlForm::getAttributeHint($this->getFormModel(), $this->attribute);
56
    }
57
58 162
    final protected function getAttributePlaceholder(): ?string
59
    {
60 162
        $placeholder = $this->getFormModel()->getAttributePlaceholder($this->getAttributeName());
61 162
        return $placeholder === '' ? null : $placeholder;
62
    }
63
64 243
    final protected function getInputId(): string
65
    {
66 243
        return HtmlForm::getInputId($this->getFormModel(), $this->attribute);
67
    }
68
69 311
    final protected function getFirstError(): ?string
70
    {
71 311
        return $this->getFormModel()->getFormErrors()->getFirstError($this->getAttributeName());
72
    }
73
}
74