Button   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 80
ccs 21
cts 21
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 14 3
A tagName() 0 5 1
A encodeLabels() 0 5 1
A label() 0 5 1
A options() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap4;
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
 *     ->label('Action')
17
 *     ->options(['class' => 'btn-lg']);
18
 * ```
19
 */
20
class Button extends Widget
21
{
22
    private string $tagName = 'button';
23
    private string $label = 'Button';
24
    private bool $encodeLabels = true;
25
    private array $options = [];
26
27 9
    protected function run(): string
28
    {
29 9
        if (!isset($this->options['id'])) {
30 7
            $this->options['id'] = "{$this->getId()}-button";
31
        }
32
33 9
        Html::addCssClass($this->options, ['widget' => 'btn']);
34
35 9
        $this->registerPlugin('button', $this->options);
36
37 9
        return Html::tag(
38 9
            $this->tagName,
39 9
            $this->encodeLabels ? Html::encode($this->label) : $this->label,
40 9
            $this->options
41
        );
42
    }
43
44
    /**
45
     * Whether the label should be HTML-encoded.
46
     *
47
     * @param bool $value
48
     *
49
     * @return $this
50
     */
51 9
    public function encodeLabels(bool $value): self
52
    {
53 9
        $this->encodeLabels = $value;
54
55 9
        return $this;
56
    }
57
58
    /**
59
     * The button label
60
     *
61
     * @param string $value
62
     *
63
     * @return $this
64
     */
65 9
    public function label(string $value): self
66
    {
67 9
        $this->label = $value;
68
69 9
        return $this;
70
    }
71
72
    /**
73
     * The HTML attributes for the widget container tag. The following special options are recognized.
74
     *
75
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
76
     *
77
     * @param array $value
78
     *
79
     * @return $this
80
     */
81 9
    public function options(array $value): self
82
    {
83 9
        $this->options = $value;
84
85 9
        return $this;
86
    }
87
88
    /**
89
     * The tag to use to render the button.
90
     *
91
     * @param string $value
92
     *
93
     * @return $this
94
     */
95 3
    public function tagName(string $value): self
96
    {
97 3
        $this->tagName = $value;
98
99 3
        return $this;
100
    }
101
}
102