Passed
Push — master ( b856e3...01442b )
by Dmitrijs
02:12
created

MaterializeWidgetTrait::renderIcon()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 1
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\assets\MaterializePluginAsset;
11
use dmgpage\yii2materialize\helpers\Html;
12
use yii\helpers\ArrayHelper;
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * MaterializeWidgetTrait is the trait, which provides basic for all Materialize widgets features.
17
 *
18
 * Note: class, which uses this trait must declare public field named `options` with the array default value:
19
 *
20
 * ```php
21
 * class MyWidget extends \yii\base\Widget
22
 * {
23
 *     use MaterializeWidgetTrait;
24
 *
25
 *     public $options = [];
26
 * }
27
 * ```
28
 *
29
 * This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict.
30
 *
31
 * @package widgets
32
 */
33
trait MaterializeWidgetTrait
34
{
35
    /**
36
     * @var array the options for the underlying Materialize JS plugin.
37
     * Please refer to the corresponding Materialize plugin Web page for possible options.
38
     * For example, [this page](https://materializecss.com/modals.html) shows
39
     * how to use the "Modal" plugin and the supported options.
40
     *
41
     * @see http://materializecss.com/
42
     */
43
    public $clientOptions = [];
44
45
    /**
46
     * @var array the event handlers for the underlying Materialize JS plugin.
47
     * Please refer to the corresponding Materialize plugin Web page for possible events.
48
     * For example, [this page](https://materializecss.com/modals.html) shows
49
     * how to use the "Modal" plugin and the supported events.
50
     *
51
     * @see http://materializecss.com/
52
     */
53
    public $clientEvents = [];
54
55
    /**
56
     * Initializes the widget.
57
     * This method will register the Materialize asset bundle. If you override this method,
58
     * make sure you call the parent implementation first.
59
     */
60
    public function init()
61
    {
62
        parent::init();
63
64
        if (!isset($this->options['id'])) {
65
            $this->options['id'] = $this->getId();
66
        }
67
    }
68
69
    /**
70
     * Registers a specific Materialize plugin and the related events
71
     * @param string $name the name of the Materialize plugin
72
     *
73
     * @uses [[MaterializePluginAsset::register()]]
74
     * @uses [[registerClientEvents()]]
75
     */
76
    protected function registerPlugin($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
    protected function registerPlugin(/** @scrutinizer ignore-unused */ $name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        $view = $this->getView();
79
        MaterializePluginAsset::register($view);
80
81
        if (!empty($this->clientOptions)) {
82
            //...
83
        }
84
85
        $this->registerClientEvents();
86
    }
87
88
    /**
89
     * Registers JS event handlers that are listed in [[clientEvents]].
90
     */
91
    protected function registerClientEvents()
92
    {
93
        if (!empty($this->clientEvents)) {
94
            //...
95
        }
96
    }
97
98
    /**
99
     * Renders an icon.
100
     *
101
     * @param string|array $icon the options for the optional icon.
102
     * @return string the rendered icon
103
     * @throws InvalidConfigException if icon name is not specified
104
     *
105
     * @uses ArrayHelper::getValue
106
     * @see Html::icon
107
     */
108
    protected function renderIcon($icon)
109
    {
110
        $html = '';
111
112
        if (!empty($icon)) {
113
            if (is_array($icon) && isset($icon['name'])) {
114
                $iconName = ArrayHelper::getValue($icon, 'name', null);
115
            } elseif (is_string($icon)) {
116
                $iconName = $icon;
117
            } else {
118
                throw new InvalidConfigException('The icon name must be specified.');
119
            }
120
121
            $iconOptions = ArrayHelper::getValue($icon, 'options', []);
122
            $html = Html::icon($iconName, $iconOptions);
123
        }
124
125
        return $html;
126
    }
127
128
    /**
129
     * Returns the ID of the widget.
130
     *
131
     * @param bool $autoGenerate whether to generate an ID if it is not set previously
132
     * @return string ID of the widget.
133
     * @see \yii\base\Widget::getId()
134
     */
135
    abstract public function getId($autoGenerate = true);
136
137
    /**
138
     * @return \yii\web\View the view object that can be used to render views or view files.
139
     * @see \yii\base\Widget::getView()
140
     */
141
    abstract public function getView();
142
}
143