Test Failed
Pull Request — master (#160)
by Wilmer
02:22
created

Label::encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

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
rs 10
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
    public function attributes(array $values): self
36
    {
37
        $new = clone $this;
38
        $new->attributes = array_merge($new->attributes, $values);
39
        return $new;
40
    }
41
42
    /**
43
     * Whether content should be HTML-encoded.
44
     *
45
     * @param bool $value
46
     *
47
     * @return static
48
     */
49
    public function encode(bool $value): self
50
    {
51
        $new = clone $this;
52
        $new->encode = $value;
53
        return $new;
54
    }
55
56
    /**
57
     * @return static
58
     */
59
    public function for(FormModelInterface $formModel, string $attribute): self
60
    {
61
        $new = clone $this;
62
        $new->formModel = $formModel;
63
        $new->attribute = $attribute;
64
        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
    public function forId(?string $value): self
81
    {
82
        $new = clone $this;
83
        $new->attributes['for'] = $value;
84
        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
    public function label(?string $value): self
99
    {
100
        $new = clone $this;
101
        $new->label = $value;
102
        return $new;
103
    }
104
105
    /**
106
     * @return string the generated label tag.
107
     */
108
    protected function run(): string
109
    {
110
        $attributes = $this->attributes;
111
        $label = $this->label;
112
113
        if ($label === '') {
114
            $label = HtmlForm::getAttributeLabel($this->getFormModel(), $this->getAttribute());
115
        }
116
117
        /** @var string */
118
        if (!array_key_exists('for', $attributes)) {
119
            $attributes['for'] = HtmlForm::getInputId($this->getFormModel(), $this->getAttribute());
120
        }
121
122
        return $label !== null ?
123
            LabelTag::tag()->attributes($attributes)->content($label)->encode($this->encode)->render()
124
            : '';
125
    }
126
127
    private function getAttribute(): string
128
    {
129
        if ($this->attribute === '') {
130
            throw new InvalidArgumentException('Attribute is not set.');
131
        }
132
133
        return $this->attribute;
134
    }
135
136
    /**
137
     * Return FormModelInterface object.
138
     *
139
     * @return FormModelInterface
140
     */
141
    private function getFormModel(): FormModelInterface
142
    {
143
        if ($this->formModel === null) {
144
            throw new InvalidArgumentException('Form model is not set.');
145
        }
146
147
        return $this->formModel;
148
    }
149
}
150