Passed
Push — master ( 4c4f3f...4700f4 )
by Sergei
02:48
created

FormAttributeTrait::getFormAttributeLabel()   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\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 $formAttribute = '';
15
16 474
    final public function formAttribute(FormModelInterface $model, string $attribute): static
17
    {
18 474
        $new = clone $this;
19 474
        $new->formModel = $model;
20 474
        $new->formAttribute = $attribute;
21 474
        return $new;
22
    }
23
24 462
    final protected function getFormModel(): FormModelInterface
25
    {
26 462
        if ($this->formModel === null) {
27 1
            throw new InvalidArgumentException('Form model is not set.');
28
        }
29
30 461
        return $this->formModel;
31
    }
32
33 550
    final protected function hasFormModelAndAttribute(): bool
34
    {
35 550
        return $this->formModel !== null && $this->formAttribute !== '';
36
    }
37
38 383
    final protected function getFormAttributeName(): string
39
    {
40 383
        return HtmlForm::getAttributeName($this->getFormModel(), $this->formAttribute);
41
    }
42
43 371
    final protected function getFormAttributeValue(): mixed
44
    {
45 371
        return HtmlForm::getAttributeValue($this->getFormModel(), $this->formAttribute);
46
    }
47
48 193
    final protected function getFormAttributeLabel(): string
49
    {
50 193
        return HtmlForm::getAttributeLabel($this->getFormModel(), $this->formAttribute);
51
    }
52
53 381
    final protected function getFormAttributeHint(): string
54
    {
55 381
        return HtmlForm::getAttributeHint($this->getFormModel(), $this->formAttribute);
56
    }
57
58 166
    final protected function getFormAttributePlaceholder(): ?string
59
    {
60 166
        $placeholder = $this->getFormModel()->getAttributePlaceholder($this->getFormAttributeName());
61 166
        return $placeholder === '' ? null : $placeholder;
62
    }
63
64 339
    final protected function getInputId(): string
65
    {
66 339
        return HtmlForm::getInputId($this->getFormModel(), $this->formAttribute);
67
    }
68
69 383
    final protected function getFirstError(): ?string
70
    {
71 383
        return $this->getFormModel()->getFormErrors()->getFirstError($this->getFormAttributeName());
72
    }
73
}
74