Passed
Pull Request — master (#160)
by Wilmer
10:36
created

Label::encode()   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 0
Metric Value
cc 1
eloc 3
c 0
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\Arrays\ArrayHelper;
8
use Yiisoft\Form\Helper\HtmlForm;
9
use Yiisoft\Html\Tag\Label as LabelTag;
10
11
/**
12
 * Generates a label tag for the given form attribute.
13
 *
14
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/label.html
15
 *
16
 * @psalm-suppress MissingConstructor
17
 */
18
final class Label extends AbstractWidget
19
{
20
    private ?string $label = '';
21
22
    /**
23
     * The id of a labelable form-related element in the same document as the tag label element.
24
     *
25
     * The first element in the document with an id matching the value of the for attribute is the labeled control for
26
     * this label element, if it is a labelable element.
27
     *
28
     * @param string|null $value The id of a labelable form-related element in the same document as the tag label
29
     * element. If null, the attribute will be removed.
30
     *
31
     * @return static
32
     *
33
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/label.html#label.attrs.for
34
     */
35 3
    public function forId(?string $value): self
36
    {
37 3
        $new = clone $this;
38 3
        $new->attributes['for'] = $value;
39 3
        return $new;
40
    }
41
42
    /**
43
     * This specifies the label to be displayed.
44
     *
45
     * @param string|null $value The label to be displayed.
46
     *
47
     * @return static
48
     *
49
     * Note that this will NOT be encoded.
50
     * - If this is not set, {@see \Yiisoft\Form\FormModel::getAttributeLabel() will be called to get the label for
51
     * display (after encoding).
52
     */
53 12
    public function label(?string $value): self
54
    {
55 12
        $new = clone $this;
56 12
        $new->label = $value;
57 12
        return $new;
58
    }
59
60
    /**
61
     * @return string the generated label tag.
62
     */
63 166
    protected function run(): string
64
    {
65 166
        $new = clone $this;
66
67 166
        if ($new->label === '') {
68 155
            $new->label = HtmlForm::getAttributeLabel($new->getFormModel(), $new->getAttribute());
69
        }
70
71
        /** @var string */
72 166
        $forId = ArrayHelper::remove(
73 166
            $new->attributes,
74 166
            'for',
75 166
            HtmlForm::getInputId($new->getFormModel(), $new->getAttribute())
76
        );
77
78 166
        return $new->label !== null
79 163
            ? LabelTag::tag()
80 163
                ->attributes($new->attributes)
81 163
                ->content($new->label)
82 163
                ->encode($new->getEncode())
83 163
                ->forId($forId)
84 163
                ->render()
85 166
            : '';
86
    }
87
}
88