Passed
Push — master ( f54d79...d46c99 )
by Dmitrijs
01:55
created

Button::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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 @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 = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type array of property $clientOptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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) {
1 ignored issue
show
introduced by
The condition $this->label !== false is always true.
Loading history...
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() . $label, $this->options);
131
    }
132
133
    /**
134
     * Renders an icon.
135
     *
136
     * @return string the rendered icon
137
     * @throws \yii\base\InvalidConfigException if icon name is not specified
138
     *
139
     * @uses http://www.yiiframework.com/doc-2.0/yii-helpers-basearrayhelper.html#getValue()-detail
140
     * @see Icon::run
141
     */
142
    protected function renderIcon()
143
    {
144
        if (!$this->icon) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $this->icon of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
145
            return '';
146
        } else {
147
            $name = ArrayHelper::getValue($this->icon, 'name', null);
148
            $position = ArrayHelper::getValue($this->icon, 'position', []);
149
            $options = ArrayHelper::getValue($this->icon, 'options', []);
150
151
            if (!empty($position)) {
152
                Html::addCssClass($options, $position);
153
            }
154
            
155
            return Html::icon($name, $options);
156
        }
157
    }
158
}
159