Test Failed
Pull Request — master (#159)
by Sergei
03:08
created

Hint   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 77
rs 10
c 1
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A attributes() 0 5 1
A content() 0 5 1
A tag() 0 9 2
A run() 0 9 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 $attributes = [];
22
23
    /**
24
     * @var string|Stringable|null
25
     */
26
    private $content = null;
27
28
    private bool $encode = true;
29
30
    /**
31
     * Set the container tag name for the hint.
32
     *
33
     * @param string $tag Container tag name. Set to empty value to render error messages without container tag.
34
     *
35
     * @return static
36
     */
37
    public function tag(string $tag): self
38
    {
39
        if ($tag === '') {
40
            throw new InvalidArgumentException('Tag name cannot be empty.');
41
        }
42
43
        $new = clone $this;
44
        $new->tag = $tag;
45
        return $new;
46
    }
47
48
    public function attributes(array $attributes): self
49
    {
50
        $new = clone $this;
51
        $new->attributes = $attributes;
52
        return $new;
53
    }
54
55
    /**
56
     * @param string|Stringable|null $content
57
     *
58
     * @return static
59
     */
60
    public function content($content): self
61
    {
62
        $new = clone $this;
63
        $new->content = $content;
64
        return $new;
65
    }
66
67
    /**
68
     * Whether content should be HTML-encoded.
69
     *
70
     * @param bool $value
71
     *
72
     * @return static
73
     */
74
    public function encode(bool $value): self
75
    {
76
        $new = clone $this;
77
        $new->encode = $value;
78
        return $new;
79
    }
80
81
    protected function run(): string
82
    {
83
        return Html::tag(
84
            $this->tag,
85
            $this->content ?? $this->getAttributeHint(),
86
            $this->attributes
87
        )
88
            ->encode($this->encode)
89
            ->render();
90
    }
91
}
92