Passed
Pull Request — master (#35)
by Wilmer
02:14
created

Button::withTagName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap5;
6
7
use Yiisoft\Html\Html;
8
9
/**
10
 * Button renders a bootstrap button.
11
 *
12
 * For example,
13
 *
14
 * ```php
15
 * echo Button::widget()
16
 *     ->withLabel('Action')
17
 *     ->withOptions(['class' => 'btn-lg']);
18
 * ```
19
 */
20
final class Button extends Widget
21
{
22
    private string $tagName = 'button';
23
    private string $label = 'Button';
24
    private bool $encodeLabels = true;
25
    private bool $encodeTags = false;
26
    private array $options = [];
27
28 24
    public function run(): string
29
    {
30 24
        if (!isset($this->options['id'])) {
31 15
            $this->options['id'] = "{$this->getId()}-button";
32
        }
33
34
        /** @psalm-suppress InvalidArgument */
35 24
        Html::addCssClass($this->options, ['widget' => 'btn']);
36
37 24
        if ($this->encodeTags === false) {
38 23
            $this->options = array_merge($this->options, ['encode' => false]);
39
        }
40
41 24
        return Html::tag(
42 24
            $this->tagName,
43 24
            $this->encodeLabels ? Html::encode($this->label) : $this->label,
44 24
            $this->options
45
        );
46
    }
47
48
    /**
49
     * Whether the label should be HTML-encoded.
50
     *
51
     * @param bool $value
52
     *
53
     * @return $this
54
     */
55 20
    public function withoutEncodeLabels(bool $value = false): self
56
    {
57 20
        $new = clone $this;
58 20
        $new->encodeLabels = $value;
59
60 20
        return $new;
61
    }
62
63
    /**
64
     * The button label
65
     *
66
     * @param string $value
67
     *
68
     * @return $this
69
     */
70 24
    public function withLabel(string $value): self
71
    {
72 24
        $new = clone $this;
73 24
        $new->label = $value;
74
75 24
        return $new;
76
    }
77
78
    /**
79
     * The HTML attributes for the widget container tag. The following special options are recognized.
80
     *
81
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
82
     *
83
     * @param array $value
84
     *
85
     * @return $this
86
     */
87 20
    public function withOptions(array $value): self
88
    {
89 20
        $new = clone $this;
90 20
        $new->options = $value;
91
92 20
        return $new;
93
    }
94
95
    /**
96
     * The tag to use to render the button.
97
     *
98
     * @param string $value
99
     *
100
     * @return $this
101
     */
102 11
    public function withTagName(string $value): self
103
    {
104 11
        $new = clone $this;
105 11
        $new->tagName = $value;
106
107 11
        return $new;
108
    }
109
110
    /**
111
     * Allows you to enable or disable the encoding tags html.
112
     *
113
     * @param bool $value
114
     *
115
     * @return self
116
     */
117 1
    public function withEncodeTags(bool $value = true): self
118
    {
119 1
        $new = clone $this;
120 1
        $new->encodeTags = $value;
121
122 1
        return $new;
123
    }
124
}
125