Passed
Pull Request — master (#160)
by Wilmer
02:39
created

Label   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 94.87%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 130
ccs 37
cts 39
cp 0.9487
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A for() 0 6 1
A getFormModel() 0 7 2
A attributes() 0 5 1
A encode() 0 5 1
A getAttribute() 0 7 2
A label() 0 5 1
A run() 0 17 4
A forId() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget\FieldPart;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\FormModelInterface;
9
use Yiisoft\Form\Helper\HtmlForm;
10
use Yiisoft\Html\Tag\Label as LabelTag;
11
use Yiisoft\Widget\Widget;
12
13
/**
14
 * Generates a label tag for the given form attribute.
15
 *
16
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/label.html
17
 */
18
final class Label extends Widget
19
{
20
    private string $attribute = '';
21
    private array $attributes = [];
22
    private bool $encode = false;
23
    private ?string $label = '';
24
    private ?FormModelInterface $formModel = null;
25
26
    /**
27
     * The HTML attributes. The following special options are recognized.
28
     *
29
     * @param array $values Attribute values indexed by attribute names.
30
     *
31
     * @return static
32
     *
33
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
34
     */
35 319
    public function attributes(array $values): self
36
    {
37 319
        $new = clone $this;
38 319
        $new->attributes = array_merge($new->attributes, $values);
39 319
        return $new;
40
    }
41
42
    /**
43
     * Whether content should be HTML-encoded.
44
     *
45
     * @param bool $value
46
     *
47
     * @return static
48
     */
49 320
    public function encode(bool $value): self
50
    {
51 320
        $new = clone $this;
52 320
        $new->encode = $value;
53 320
        return $new;
54
    }
55
56
    /**
57
     * @return static
58
     */
59 323
    public function for(FormModelInterface $formModel, string $attribute): self
60
    {
61 323
        $new = clone $this;
62 323
        $new->formModel = $formModel;
63 323
        $new->attribute = $attribute;
64 323
        return $new;
65
    }
66
67
    /**
68
     * The id of a labelable form-related element in the same document as the tag label element.
69
     *
70
     * The first element in the document with an id matching the value of the for attribute is the labeled control for
71
     * this label element, if it is a labelable element.
72
     *
73
     * @param string|null $value The id of a labelable form-related element in the same document as the tag label
74
     * element. If null, the attribute will be removed.
75
     *
76
     * @return static
77
     *
78
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/label.html#label.attrs.for
79
     */
80 2
    public function forId(?string $value): self
81
    {
82 2
        $new = clone $this;
83 2
        $new->attributes['for'] = $value;
84 2
        return $new;
85
    }
86
87
    /**
88
     * This specifies the label to be displayed.
89
     *
90
     * @param string|null $value The label to be displayed.
91
     *
92
     * @return static
93
     *
94
     * Note that this will NOT be encoded.
95
     * - If this is not set, {@see \Yiisoft\Form\FormModel::getAttributeLabel() will be called to get the label for
96
     * display (after encoding).
97
     */
98 322
    public function label(?string $value): self
99
    {
100 322
        $new = clone $this;
101 322
        $new->label = $value;
102 322
        return $new;
103
    }
104
105
    /**
106
     * @return string the generated label tag.
107
     */
108 323
    protected function run(): string
109
    {
110 323
        $attributes = $this->attributes;
111 323
        $label = $this->label;
112
113 323
        if ($label === '') {
114 315
            $label = HtmlForm::getAttributeLabel($this->getFormModel(), $this->getAttribute());
115
        }
116
117
        /** @var string */
118 323
        if (!array_key_exists('for', $attributes)) {
119 3
            $attributes['for'] = HtmlForm::getInputId($this->getFormModel(), $this->getAttribute());
120
        }
121
122 323
        return $label !== null ?
123 321
            LabelTag::tag()->attributes($attributes)->content($label)->encode($this->encode)->render()
124 323
            : '';
125
    }
126
127 317
    private function getAttribute(): string
128
    {
129 317
        if ($this->attribute === '') {
130
            throw new InvalidArgumentException('Attribute is not set.');
131
        }
132
133 317
        return $this->attribute;
134
    }
135
136
    /**
137
     * Return FormModelInterface object.
138
     *
139
     * @return FormModelInterface
140
     */
141 317
    private function getFormModel(): FormModelInterface
142
    {
143 317
        if ($this->formModel === null) {
144
            throw new InvalidArgumentException('Form model is not set.');
145
        }
146
147 317
        return $this->formModel;
148
    }
149
}
150