Passed
Pull Request — master (#192)
by Alexander
02:31
created

Hint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 75
ccs 25
cts 25
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A content() 0 5 1
A tag() 0 9 2
A run() 0 9 2
A tagAttributes() 0 5 1
A encode() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Part;
6
7
use InvalidArgumentException;
8
use Stringable;
9
use Yiisoft\Form\Field\Base\FormAttributeTrait;
10
use Yiisoft\Html\Html;
11
use Yiisoft\Widget\Widget;
12
13
final class Hint extends Widget
14
{
15
    use FormAttributeTrait;
16
17
    /**
18
     * @psalm-var non-empty-string
19
     */
20
    private string $tag = 'div';
21
    private array $tagAttributes = [];
22
23
    private string|Stringable|null $content = null;
24
25
    private bool $encode = true;
26
27
    /**
28
     * Set the container tag name for the hint.
29
     *
30
     * @param string $tag Container tag name.
31
     *
32
     * @return static
33
     */
34 5
    public function tag(string $tag): self
35
    {
36 5
        if ($tag === '') {
37 1
            throw new InvalidArgumentException('Tag name cannot be empty.');
38
        }
39
40 4
        $new = clone $this;
41 4
        $new->tag = $tag;
42 4
        return $new;
43
    }
44
45
    /**
46
     * @return static
47
     */
48 4
    public function tagAttributes(array $attributes): self
49
    {
50 4
        $new = clone $this;
51 4
        $new->tagAttributes = $attributes;
52 4
        return $new;
53
    }
54
55
    /**
56
     * @return static
57
     */
58 6
    public function content(string|Stringable|null $content): self
59
    {
60 6
        $new = clone $this;
61 6
        $new->content = $content;
62 6
        return $new;
63
    }
64
65
    /**
66
     * Whether content should be HTML-encoded.
67
     *
68
     * @param bool $value
69
     *
70
     * @return static
71
     */
72 2
    public function encode(bool $value): self
73
    {
74 2
        $new = clone $this;
75 2
        $new->encode = $value;
76 2
        return $new;
77
    }
78
79 71
    protected function run(): string
80
    {
81 71
        $content = $this->content ?? $this->getAttributeHint();
82
83 70
        return $content === ''
84 40
            ? ''
85 30
            : Html::tag($this->tag, $content, $this->tagAttributes)
86 30
                ->encode($this->encode)
87 70
                ->render();
88
    }
89
}
90