Passed
Pull Request — master (#90)
by
unknown
13:09
created

Button::run()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 0
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RedCatGirl\YiiBootstrap386;
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
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 20
    protected function run(): string
29
    {
30 20
        if (!isset($this->options['id'])) {
31 12
            $this->options['id'] = "{$this->getId()}-button";
32
        }
33
34
        /** @psalm-suppress InvalidArgument */
35 20
        Html::addCssClass($this->options, ['widget' => 'btn']);
36
37 20
        return Html::tag(
38 20
            $this->tagName,
39 20
            $this->encodeLabels ? Html::encode($this->label) : $this->label,
40 20
            $this->options
41
        )
42 20
            ->encode($this->encodeTags)
43 20
            ->render();
44
    }
45
46
    /**
47
     * When tags Labels HTML should not be encoded.
48
     *
49
     * @return self
50
     */
51 4
    public function withoutEncodeLabels(): self
52
    {
53 4
        $new = clone $this;
54 4
        $new->encodeLabels = false;
55
56 4
        return $new;
57
    }
58
59
    /**
60
     * The button label
61
     *
62
     * @param string $value
63
     *
64
     * @return self
65
     */
66 20
    public function label(string $value): self
67
    {
68 20
        $new = clone $this;
69 20
        $new->label = $value;
70
71 20
        return $new;
72
    }
73
74
    /**
75
     * The HTML attributes for the widget container tag. The following special options are recognized.
76
     *
77
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
78
     *
79
     * @param array $value
80
     *
81
     * @return self
82
     */
83 17
    public function options(array $value): self
84
    {
85 17
        $new = clone $this;
86 17
        $new->options = $value;
87
88 17
        return $new;
89
    }
90
91
    /**
92
     * The tag to use to render the button.
93
     *
94
     * @param string $value
95
     *
96
     * @return self
97
     */
98 10
    public function tagName(string $value): self
99
    {
100 10
        $new = clone $this;
101 10
        $new->tagName = $value;
102
103 10
        return $new;
104
    }
105
}
106