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

Hint::run()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 19
ccs 13
cts 13
cp 1
crap 4
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Helper\HtmlForm;
9
use Yiisoft\Html\Tag\CustomTag;
10
11
/**
12
 * The widget for hint form.
13
 *
14
 * @psalm-suppress MissingConstructor
15
 */
16
final class Hint extends AbstractWidget
17
{
18
    private ?string $hint = '';
19
    private string $tag = 'div';
20
21
    /**
22
     * Set hint text.
23
     *
24
     * @param string|null $value
25
     *
26
     * @return static
27
     */
28 8
    public function hint(?string $value): self
29
    {
30 8
        $new = clone $this;
31 8
        $new->hint = $value;
32 8
        return $new;
33
    }
34
35
    /**
36
     * Set the container tag name for the hint.
37
     *
38
     * @param string $value Container tag name. Set to empty value to render error messages without container tag.
39
     *
40
     * @return static
41
     */
42 7
    public function tag(string $value): self
43
    {
44 7
        $new = clone $this;
45 7
        $new->tag = $value;
46 7
        return $new;
47
    }
48
49
    /**
50
     * Generates a hint tag for the given form attribute.
51
     *
52
     * @return string the generated hint tag.
53
     */
54 181
    protected function run(): string
55
    {
56 181
        $new = clone $this;
57
58 181
        if ($new->tag === '') {
59 3
            throw new InvalidArgumentException('Tag name cannot be empty.');
60
        }
61
62 178
        if ($new->hint === '') {
63 171
            $new->hint = HtmlForm::getAttributeHint($new->getFormModel(), $new->getAttribute());
64
        }
65
66 178
        return (!empty($new->hint))
67 85
            ? CustomTag::name($new->tag)
68 85
                ->attributes($new->attributes)
69 85
                ->content($new->hint)
70 85
                ->encode($new->getEncode())
71 85
                ->render()
72 178
            : '';
73
    }
74
}
75