Passed
Push — master ( e416ac...a7a28e )
by Alexander
31:14 queued 28:02
created

Hint::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\FormModelInterface;
9
use Yiisoft\Form\Helper\HtmlForm;
10
use Yiisoft\Html\Tag\CustomTag;
11
use Yiisoft\Widget\Widget;
12
13
/**
14
 * The widget for hint form.
15
 *
16
 * @psalm-suppress MissingConstructor
17
 */
18
final class Hint extends Widget
19
{
20
    private array $attributes = [];
21
    private string $attribute = '';
22
    private bool $encode = true;
23
    private FormModelInterface $formModel;
24
    private ?string $hint = '';
25
    private string $tag = 'div';
26
27
    /**
28
     * Specify a form, its attribute and a list HTML attributes for the hint generated.
29
     *
30
     * @param FormModelInterface $formModel Form instance.
31
     * @param string $attribute Form model's property name this widget is rendered for.
32
     * @param array $attributes HTML attributes for the widget container tag.
33
     *
34
     * @return static
35
     *
36
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
37
     */
38 149
    public function config(FormModelInterface $formModel, string $attribute, array $attributes = []): self
39
    {
40 149
        $new = clone $this;
41 149
        $new->formModel = $formModel;
42 149
        $new->attribute = $attribute;
43 149
        $new->attributes = $attributes;
44 149
        return $new;
45
    }
46
47
    /**
48
     * Whether content should be HTML-encoded.
49
     *
50
     * @param bool $value
51
     *
52
     * @return static
53
     */
54 146
    public function encode(bool $value): self
55
    {
56 146
        $new = clone $this;
57 146
        $new->encode = $value;
58 146
        return $new;
59
    }
60
61
    /**
62
     * Set hint text.
63
     *
64
     * @param string|null $value
65
     *
66
     * @return static
67
     */
68 147
    public function hint(?string $value): self
69
    {
70 147
        $new = clone $this;
71 147
        $new->hint = $value;
72 147
        return $new;
73
    }
74
75
    /**
76
     * Set the container tag name for the hint.
77
     *
78
     * @param string $value Container tag name. Set to empty value to render error messages without container tag.
79
     *
80
     * @return static
81
     */
82 2
    public function tag(string $value): self
83
    {
84 2
        $new = clone $this;
85 2
        $new->tag = $value;
86 2
        return $new;
87
    }
88
89
    /**
90
     * Generates a hint tag for the given form attribute.
91
     *
92
     * @return string the generated hint tag.
93
     */
94 149
    protected function run(): string
95
    {
96 149
        $new = clone $this;
97
98 149
        if ($new->hint !== null && $new->hint === '') {
99 144
            $new->hint = HtmlForm::getAttributeHint($new->formModel, $new->attribute);
100
        }
101
102 149
        if ($new->tag === '') {
103
            throw new InvalidArgumentException('Tag name cannot be empty.');
104
        }
105
106 149
        return (!empty($new->hint))
107 73
            ? CustomTag::name($new->tag)
108 73
                ->attributes($new->attributes)
109 73
                ->content($new->hint)
110 73
                ->encode($new->encode)
111 73
                ->render()
112 149
            : '';
113
    }
114
}
115