Passed
Push — master ( c202e1...34af4b )
by Alexander
20:59 queued 18:00
created

Label   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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