Passed
Pull Request — master (#192)
by Alexander
28:18 queued 25:44
created

Label   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 105
ccs 42
cts 42
cp 1
rs 10
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A content() 0 5 1
B run() 0 31 10
A tagAttributes() 0 5 1
A useInputIdAttribute() 0 5 1
A setForAttribute() 0 5 1
A encode() 0 5 1
A forId() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Part;
6
7
use Stringable;
8
use Yiisoft\Form\Field\Base\FormAttributeTrait;
9
use Yiisoft\Html\Html;
10
use Yiisoft\Widget\Widget;
11
12
/**
13
 * Represents label for a form field.
14
 */
15
final class Label extends Widget
16
{
17
    use FormAttributeTrait;
18
19
    private array $tagAttributes = [];
20
21
    private bool $setForAttribute = true;
22
23
    private ?string $forId = null;
24
    private bool $useInputIdAttribute = true;
25
26
    private string|Stringable|null $content = null;
27
28
    private bool $encode = true;
29
30
    /**
31
     * @return static
32
     */
33 2
    public function tagAttributes(array $attributes): self
34
    {
35 2
        $new = clone $this;
36 2
        $new->tagAttributes = $attributes;
37 2
        return $new;
38
    }
39
40 5
    public function setForAttribute(bool $value): self
41
    {
42 5
        $new = clone $this;
43 5
        $new->setForAttribute = $value;
44 5
        return $new;
45
    }
46
47
    /**
48
     * @return static
49
     */
50 6
    public function forId(?string $id): self
51
    {
52 6
        $new = clone $this;
53 6
        $new->forId = $id;
54 6
        return $new;
55
    }
56
57
    /**
58
     * @return static
59
     */
60 21
    public function useInputIdAttribute(bool $value): self
61
    {
62 21
        $new = clone $this;
63 21
        $new->useInputIdAttribute = $value;
64 21
        return $new;
65
    }
66
67
    /**
68
     * @return static
69
     */
70 9
    public function content(string|Stringable|null $content): self
71
    {
72 9
        $new = clone $this;
73 9
        $new->content = $content;
74 9
        return $new;
75
    }
76
77
    /**
78
     * Whether content should be HTML-encoded.
79
     *
80
     * @return static
81
     */
82 2
    public function encode(bool $value): self
83
    {
84 2
        $new = clone $this;
85 2
        $new->encode = $value;
86 2
        return $new;
87
    }
88
89 218
    protected function run(): string
90
    {
91 218
        $useModel = $this->hasFormModelAndAttribute();
92
93 218
        $content = $useModel
94 139
            ? $this->content ?? $this->getAttributeLabel()
95 81
            : (string) $this->content;
96
97 218
        if ($content === '') {
98 81
            return '';
99
        }
100
101 139
        $tagAttributes = $this->tagAttributes;
102
103 139
        if ($this->setForAttribute && !isset($tagAttributes['for'])) {
104 135
            $id = $this->forId;
105 135
            if ($useModel && $id === null && $this->useInputIdAttribute) {
106 110
                $id = $this->getInputId();
107
            }
108 135
            if ($id !== null) {
109 115
                $tagAttributes['for'] = $id;
110
            }
111
        }
112
113 139
        $tag = Html::label($content)->attributes($tagAttributes);
114
115 139
        if (!$this->encode) {
116 1
            $tag = $tag->encode(false);
117
        }
118
119 139
        return $tag->render();
120
    }
121
}
122