Passed
Pull Request — master (#192)
by Sergei
02:16
created

FormAttributeTrait::getAttributePlaceholder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
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
    /**
17
     * @return static
18
     */
19 78
    final public function attribute(FormModelInterface $formModel, string $attribute): self
20
    {
21 78
        $new = clone $this;
22 78
        $new->formModel = $formModel;
23 78
        $new->attribute = $attribute;
24 78
        return $new;
25
    }
26
27 70
    final protected function getFormModel(): FormModelInterface
28
    {
29 70
        if ($this->formModel === null) {
30 1
            throw new InvalidArgumentException('Form model is not set.');
31
        }
32
33 69
        return $this->formModel;
34
    }
35
36 48
    final protected function getAttributeName(): string
37
    {
38 48
        return HtmlForm::getAttributeName($this->getFormModel(), $this->attribute);
39
    }
40
41 35
    final protected function getAttributeValue(): mixed
42
    {
43 35
        return HtmlForm::getAttributeValue($this->getFormModel(), $this->attribute);
44
    }
45
46 40
    final protected function getAttributeLabel(): string
47
    {
48 40
        return HtmlForm::getAttributeLabel($this->getFormModel(), $this->attribute);
49
    }
50
51 41
    final protected function getAttributeHint(): string
52
    {
53 41
        return HtmlForm::getAttributeHint($this->getFormModel(), $this->attribute);
54
    }
55
56 28
    final protected function getAttributePlaceholder(): ?string
57
    {
58 28
        $placeholder = $this->getFormModel()->getAttributePlaceholder($this->getAttributeName());
59 28
        return $placeholder === '' ? null : $placeholder;
60
    }
61
62 37
    final protected function getInputId(): string
63
    {
64 37
        return HtmlForm::getInputId($this->getFormModel(), $this->attribute);
65
    }
66
67 48
    final protected function getFirstError(): ?string
68
    {
69 48
        return $this->getFormModel()->getFormErrors()->getFirstError($this->getAttributeName());
70
    }
71
}
72