Passed
Pull Request — master (#192)
by Sergei
13:59
created

Hint::content()   A

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