Hint::attributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

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
ccs 4
cts 4
cp 1
crap 1
rs 10
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\InputData\InputDataTrait;
10
use Yiisoft\Form\ThemeContainer;
11
use Yiisoft\Html\Html;
12
use Yiisoft\Widget\Widget;
13
14
/**
15
 * Represents hint for a form field.
16
 */
17
final class Hint extends Widget
18
{
19
    use InputDataTrait;
20
21
    /**
22
     * @psalm-var non-empty-string
23
     */
24
    private string $tag = 'div';
25
    private array $attributes = [];
26
27
    private string|Stringable|null $content = null;
28
29
    private bool $encode = true;
30
31
    /**
32
     * Set the container tag name for the hint.
33
     *
34
     * @param string $tag Container tag name.
35
     */
36 5
    public function tag(string $tag): self
37
    {
38 5
        if ($tag === '') {
39 1
            throw new InvalidArgumentException('Tag name cannot be empty.');
40
        }
41
42 4
        $new = clone $this;
43 4
        $new->tag = $tag;
44 4
        return $new;
45
    }
46
47 6
    public function attributes(array $attributes): self
48
    {
49 6
        $new = clone $this;
50 6
        $new->attributes = $attributes;
51 6
        return $new;
52
    }
53
54 7
    public function addAttributes(array $attributes): self
55
    {
56 7
        $new = clone $this;
57 7
        $new->attributes = array_merge($this->attributes, $attributes);
58 7
        return $new;
59
    }
60
61
    /**
62
     * Set tag ID.
63
     *
64
     * @param string|null $id Tag ID.
65
     */
66 3
    public function id(?string $id): self
67
    {
68 3
        $new = clone $this;
69 3
        $new->attributes['id'] = $id;
70 3
        return $new;
71
    }
72
73
    /**
74
     * Replace tag CSS classes with a new set of classes.
75
     *
76
     * @param string|null ...$class One or many CSS classes.
77
     */
78 15
    public function class(?string ...$class): self
79
    {
80 15
        $new = clone $this;
81 15
        $new->attributes['class'] = array_filter($class, static fn ($c) => $c !== null);
82 15
        return $new;
83
    }
84
85
    /**
86
     * Add one or more CSS classes to the tag.
87
     *
88
     * @param string|null ...$class One or many CSS classes.
89
     */
90 11
    public function addClass(?string ...$class): self
91
    {
92 11
        $new = clone $this;
93 11
        Html::addCssClass($new->attributes, $class);
94 11
        return $new;
95
    }
96
97 46
    public function content(string|Stringable|null $content): self
98
    {
99 46
        $new = clone $this;
100 46
        $new->content = $content;
101 46
        return $new;
102
    }
103
104
    /**
105
     * Whether content should be HTML-encoded.
106
     */
107 2
    public function encode(bool $value): self
108
    {
109 2
        $new = clone $this;
110 2
        $new->encode = $value;
111 2
        return $new;
112
    }
113
114 670
    public function render(): string
115
    {
116 670
        $content = $this->content ?? $this->getInputData()->getHint();
117
118 670
        return empty($content)
119 606
            ? ''
120 670
            : Html::tag($this->tag, $content, $this->attributes)
121 670
                ->encode($this->encode)
122 670
                ->render();
123
    }
124
125 672
    protected static function getThemeConfig(?string $theme): array
126
    {
127 672
        return ThemeContainer::getTheme($theme)?->getHintConfig() ?? [];
128
    }
129
}
130