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

Hint::run()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 9
nop 0
dl 0
loc 19
ccs 13
cts 13
cp 1
crap 5
rs 9.5555
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\CustomTag;
11
use Yiisoft\Widget\Widget;
12
13
/**
14
 * The widget for hint form.
15
 *
16
 * @psalm-suppress MissingConstructor
17
 */
18
final class Hint extends Widget
19
{
20
    private string $attribute = '';
21
    private array $attributes = [];
22
    private bool $encode = false;
23
    private ?string $hint = '';
24
    private string $tag = 'div';
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 334
    public function attributes(array $values): self
37
    {
38 334
        $new = clone $this;
39 334
        $new->attributes = array_merge($new->attributes, $values);
40 334
        return $new;
41
    }
42
43
    /**
44
     * Whether content should be HTML-encoded.
45
     *
46
     * @param bool $value
47
     *
48
     * @return static
49
     */
50 336
    public function encode(bool $value): self
51
    {
52 336
        $new = clone $this;
53 336
        $new->encode = $value;
54 336
        return $new;
55
    }
56
57
    /**
58
     * @return static
59
     */
60 339
    public function for(FormModelInterface $formModel, string $attribute): self
61
    {
62 339
        $new = clone $this;
63 339
        $new->formModel = $formModel;
64 339
        $new->attribute = $attribute;
65 339
        return $new;
66
    }
67
68
    /**
69
     * Set the ID of the widget.
70
     *
71
     * @param string|null $id
72
     *
73
     * @return static
74
     *
75
     * @link https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute
76
     */
77
    public function id(?string $id): self
78
    {
79
        $new = clone $this;
80
        $new->attributes['id'] = $id;
81
        return $new;
82
    }
83
84
    /**
85
     * Set hint text.
86
     *
87
     * @param string|null $value
88
     *
89
     * @return static
90
     */
91 337
    public function hint(?string $value): self
92
    {
93 337
        $new = clone $this;
94 337
        $new->hint = $value;
95 337
        return $new;
96
    }
97
98
    /**
99
     * Set the container tag name for the hint.
100
     *
101
     * @param string $value Container tag name. Set to empty value to render error messages without container tag.
102
     *
103
     * @return static
104
     */
105 337
    public function tag(string $value): self
106
    {
107 337
        $new = clone $this;
108 337
        $new->tag = $value;
109 337
        return $new;
110
    }
111
112
    /**
113
     * Generates a hint tag for the given form attribute.
114
     *
115
     * @return string the generated hint tag.
116
     */
117 339
    protected function run(): string
118
    {
119 339
        $hint = $this->hint;
120
121 339
        if ($this->tag === '') {
122 1
            throw new InvalidArgumentException('Tag name cannot be empty.');
123
        }
124
125 338
        if ($hint === '') {
126 331
            $hint = HtmlForm::getAttributeHint($this->getFormModel(), $this->getAttribute());
127
        }
128
129 338
        return $hint !== null && $hint !== ''
130 8
            ? CustomTag::name($this->tag)
131 8
                ->attributes($this->attributes)
132 8
                ->content($hint)
133 8
                ->encode($this->encode)
134 8
                ->render()
135 338
            : '';
136
    }
137
138 331
    private function getAttribute(): string
139
    {
140 331
        if ($this->attribute === '') {
141
            throw new InvalidArgumentException('Attribute is not set.');
142
        }
143
144 331
        return $this->attribute;
145
    }
146
147 331
    private function getFormModel(): FormModelInterface
148
    {
149 331
        if ($this->formModel === null) {
150
            throw new InvalidArgumentException('Form model is not set.');
151
        }
152
153 331
        return $this->formModel;
154
    }
155
}
156