Button   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 27
c 2
b 0
f 0
dl 0
loc 100
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
B run() 0 28 7
1
<?php
2
/**
3
 * @link https://github.com/DMGPage/yii2-materialize
4
 * @copyright Copyright (c) 2018 Dmitrijs Reinmanis
5
 * @license https://github.com/DMGPage/yii2-materialize/blob/master/LICENSE
6
 */
7
8
namespace dmgpage\yii2materialize\widgets;
9
10
use dmgpage\yii2materialize\helpers\Html;
11
use yii\helpers\ArrayHelper;
12
13
/**
14
 * Button renders a Materialize button.
15
 *
16
 * For example,
17
 *
18
 * ```php
19
 * echo Button::widget([
20
 *     'waves' => Waves::LIGHT,
21
 *     'icon' => [
22
 *         'name' => 'alarm',
23
 *         'position' => Position::LEFT,
24
 *         'options' =>  ['class' => 'red'],
25
 *     ]
26
 * ]);
27
 * ```
28
 * @see https://materializecss.com/buttons.html
29
 * @package widgets
30
 */
31
class Button extends Widget
32
{
33
    /**
34
     * @var string the tag to use to render the button
35
     */
36
    public $tagName = 'button';
37
38
    /**
39
     * @var string the button label. Set to "false", if you do not want a label text to be rendered
40
     */
41
    public $label = 'Button';
42
43
    /**
44
     * @var bool whether the label should be HTML-encoded.
45
     */
46
    public $encodeLabel = true;
47
48
    /**
49
     * @var array the options for the optional icon.
50
     *
51
     * An example of how to specify an icon:
52
     *
53
     * ```php
54
     * [
55
     *     'name' => 'alarm',
56
     *     'position' => Position::LEFT,
57
     *     'options' =>  ['class' => 'red'],
58
     * ]
59
     * ```
60
     *
61
     * @see \dmgpage\yii2materialize\helpers\Html::icon()
62
     */
63
    public $icon = [];
64
65
    /**
66
     * @var string|array one or several wave effects
67
     */
68
    public $waves;
69
70
    /**
71
     * @var string the type of button to be rendered. Default is 'btn'
72
     * @see \dmgpage\yii2materialize\helpers\Type
73
     */
74
    public $type;
75
76
    /**
77
     * @var string the size (large or small) of button to be rendered
78
     * @see \dmgpage\yii2materialize\helpers\Size
79
     */
80
    public $size;
81
82
    /**
83
     * @var boolean whether the button shall be disabled.
84
     */
85
    public $disabled = false;
86
87
    /**
88
     * Initializes the widget.
89
     * If you override this method, make sure you call the parent implementation first.
90
     */
91
    public function init()
92
    {
93
        parent::init();
94
        $this->clientOptions = [];
95
    }
96
97
    /**
98
     * Renders the widget.
99
     *
100
     * @return string the result of widget execution to be outputted.
101
     * @uses [[renderIcon]]
102
     */
103
    public function run()
104
    {
105
        if ($this->label !== false) {
106
            $label = $this->encodeLabel ? Html::encode($this->label) : $this->label;
107
        } else {
108
            $label = '';
109
        }
110
111
        if (!empty($this->type)) {
112
            Html::addCssClass($this->options, "btn-$this->type");
113
        } else {
114
            Html::addCssClass($this->options, 'btn');
115
        }
116
117
        if (!empty($this->size)) {
118
            Html::addCssClass($this->options, "btn-$this->size");
119
        }
120
121
        if ($this->disabled) {
122
            Html::addCssClass($this->options, 'disabled');
123
        }
124
125
        if (!empty($this->waves)) {
126
            $this->options = Html::addWaves($this->waves, $this->options);
127
        }
128
129
        $this->registerPlugin('button');
130
        return Html::tag($this->tagName, $this->renderIcon($this->icon) . $label, $this->options);
131
    }
132
}
133