Passed
Pull Request — master (#146)
by Wilmer
02:42
created

Label::label()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget;
6
7
use Yiisoft\Form\FormModelInterface;
8
use Yiisoft\Form\Helper\HtmlForm;
9
use Yiisoft\Html\Tag\Label as LabelTag;
10
use Yiisoft\Widget\Widget;
11
12
/**
13
 * Generates a label tag for the given form attribute.
14
 *
15
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/label.html
16
 *
17
 * @psalm-suppress MissingConstructor
18
 */
19
final class Label extends Widget
20
{
21
    private array $attributes = [];
22
    private string $attribute = '';
23
    private ?string $for = '';
24
    private bool $encode = true;
25
    private FormModelInterface $formModel;
26
    private ?string $label = '';
27
28
    /**
29
     * Specify a form, its attribute and a list HTML attributes for the label generated.
30
     *
31
     * @param FormModelInterface $formModel Form.
32
     * @param string $attribute Form model property this widget is rendered for.
33
     * @param array $attributes The HTML attributes for the widget container tag.
34
     *
35
     * @return static
36
     *
37
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
38
     */
39 145
    public function config(FormModelInterface $formModel, string $attribute, array $attributes = []): self
40
    {
41 145
        $new = clone $this;
42 145
        $new->formModel = $formModel;
43 145
        $new->attribute = $attribute;
44 145
        $new->attributes = $attributes;
45 145
        return $new;
46
    }
47
48
    /**
49
     * Whether content should be HTML-encoded.
50
     *
51
     * @param bool $value
52
     *
53
     * @return static
54
     */
55 142
    public function encode(bool $value): self
56
    {
57 142
        $new = clone $this;
58 142
        $new->encode = $value;
59 142
        return $new;
60
    }
61
62
    /**
63
     * The id of a labelable form-related element in the same document as the tag label element.
64
     *
65
     * The first element in the document with an id matching the value of the for attribute is the labeled control for
66
     * this label element, if it is a labelable element.
67
     *
68
     * @param string|null $value The id of a labelable form-related element in the same document as the tag label
69
     * element. If null, the attribute will be removed.
70
     *
71
     * @return static
72
     *
73
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/label.html#label.attrs.for
74
     */
75 143
    public function for(?string $value): self
76
    {
77 143
        $new = clone $this;
78 143
        $new->for = $value;
79 143
        return $new;
80
    }
81
82
    /**
83
     * This specifies the label to be displayed.
84
     *
85
     * @param string $value The label to be displayed.
86
     *
87
     * @return static
88
     *
89
     * Note that this will NOT be encoded.
90
     * - If this is not set, {@see \Yiisoft\Form\FormModel::getAttributeLabel() will be called to get the label for
91
     * display (after encoding).
92
     */
93 143
    public function label(?string $value): self
94
    {
95 143
        $new = clone $this;
96 143
        $new->label = $value;
97 143
        return $new;
98
    }
99
100
    /**
101
     * @return string the generated label tag.
102
     */
103 145
    protected function run(): string
104
    {
105 145
        $new = clone $this;
106
107 145
        if ($new->label === '') {
108 136
            $new->label = HtmlForm::getAttributeLabel($new->formModel, $new->attribute);
109
        }
110
111 145
        if ($new->for === '') {
112 141
            $new->for = HtmlForm::getInputId($new->formModel, $new->attribute);
113
        }
114
115 145
        return $new->label !== null
116 142
            ? LabelTag::tag()
117 142
                ->attributes($new->attributes)
118 142
                ->content($new->label)
119 142
                ->encode($new->encode)
120 142
                ->forId($new->for)
121 142
                ->render()
122 145
            : '';
123
    }
124
}
125