Passed
Push — master ( e6861f...bdfe92 )
by Sergei
03:23
created

Hint::getThemeConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\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 FormAttributeTrait;
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 5
    public function attributes(array $attributes): self
48
    {
49 5
        $new = clone $this;
50 5
        $new->attributes = $attributes;
51 5
        return $new;
52
    }
53
54 6
    public function addAttributes(array $attributes): self
55
    {
56 6
        $new = clone $this;
57 6
        $new->attributes = array_merge($this->attributes, $attributes);
58 6
        return $new;
59
    }
60
61
    /**
62
     * Set tag ID.
63
     *
64
     * @param string|null $id Tag ID.
65
     */
66 2
    public function id(?string $id): self
67
    {
68 2
        $new = clone $this;
69 2
        $new->attributes['id'] = $id;
70 2
        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 9
    public function class(?string ...$class): self
79
    {
80 9
        $new = clone $this;
81 9
        $new->attributes['class'] = array_filter($class, static fn ($c) => $c !== null);
82 9
        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 10
    public function addClass(?string ...$class): self
91
    {
92 10
        $new = clone $this;
93 10
        Html::addCssClass(
94 10
            $new->attributes,
95 10
            array_filter($class, static fn ($c) => $c !== null),
96 10
        );
97 10
        return $new;
98
    }
99
100 12
    public function content(string|Stringable|null $content): self
101
    {
102 12
        $new = clone $this;
103 12
        $new->content = $content;
104 12
        return $new;
105
    }
106
107
    /**
108
     * Whether content should be HTML-encoded.
109
     */
110 2
    public function encode(bool $value): self
111
    {
112 2
        $new = clone $this;
113 2
        $new->encode = $value;
114 2
        return $new;
115
    }
116
117 520
    public function render(): string
118
    {
119 520
        $content = $this->hasFormModelAndAttribute()
120 409
            ? $this->content ?? $this->getFormAttributeHint()
121 113
            : (string) $this->content;
122
123 520
        return $content === ''
124 438
            ? ''
125 520
            : Html::tag($this->tag, $content, $this->attributes)
126 520
                ->encode($this->encode)
127 520
                ->render();
128
    }
129
130 522
    protected static function getThemeConfig(?string $theme): array
131
    {
132 522
        return ThemeContainer::getTheme($theme)?->getHintConfig() ?? [];
133
    }
134
}
135